How to pull artifacts from Artifactory to project classpath using gradle?

Simple-Solution picture Simple-Solution · Oct 16, 2013 · Viewed 31.2k times · Source

I'm new to gradle and Artifactory integration and so far I can publish artifacts from one workspace to another.

What I did was created a gradle sample project and now I would like to publish i.e. JUnit jar into Artifactory and then retrieve it to my project classpath as a dependency, so that I can run my project.

Project strructure

enter image description here

build.gradle

apply plugin: 'java'
   apply plugin: 'eclipse'
   //apply plugin: 'artifactory-publish'
   sourceCompatibility = 1.5
   version = '1.0'

jar 
{
    manifest 
     {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation Version': version
    }
}

buildscript 
{
    repositories 
    {
        maven 
        {
            url 'http://dl.bintray.com/jfrog/jfrog-jars' 
        }
        mavenCentral()
    }
    dependencies 
    {
        classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.1.0')
    }
}

//pull/retrieve artifacts(jar) from artifactory
repositories 
{
    ivy 
    {
        url = 'http://localhost:8081/artifactory/APM-jars'
         credentials
        {
            username = 'admin'
            password = 'password'
        }
    }
    mavenCentral()

dependencies {
     compile group: 'jakarta-regexp', name: 'jakarta-regexp', version: '1.+'
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}
}
 test {
    systemProperties 'property': 'value'
}

///publish/upload artifact (jar) into 3 different type of builds
uploadArchives 
{
    repositories 
    {
        //Just copy to a directory
    //flatDir 
        //{
            //dirs 'repos2'
        //}


        //Publish to an Ivy repository in Artifactory.
        ivy 
        {    
            url = 'http://localhost:8081/artifactory/test-ivy'
            credentials 
            {
                username = 'admin'
                password = 'password'
            }
        }
    //Publish to a Gradle repository in Artifactory.
    ivy 
    {
        url = 'http://localhost:8081/artifactory/test-gradle'
        credentials 
        {
            username = 'admin'
            password = 'password'
        }
        //layout 'gradle'
        layout 'pattern', 
        {
            artifact '[module]/[revision]/[artifact](.[ext])'
            ivy '[module]/[revision]/ivy.xml'
        }
       }
        //Publish to a Maven repository in Artifactory.
        ivy 
        {
            url = 'http://[host]:port/artifactory/test-maven'
            credentials 
        {
                username = 'admin'
                password = 'password'
            }
            layout 'maven'
        }
    }
}

Answer

javabeangrinder picture javabeangrinder · Mar 25, 2014

If you are only interested in getting resources from artifactory you can leave all the build.gradle-code you get from artifactory and just enter:

repositories {
    maven {
        url 'http://artifactory.domain/artifactory/libs-release'
    }
}

This approach assumes you have anonymous read access to you artifactory. Otherwise you also need to create the gradle.properties file containing:

HOST=localhost
REALM=Artifactory Realm
USER=admin
PASSWORD=password