Search code examples
gradledownloadartifactoryartifactgradle-2

Gradle - Download all version jars of a single groupID:artifactID from Artifactory


I use Gradle to build a project (Java).

Gradle 2.3, Java7/8. Works great.

To build a project, we define what all libraries we need during compile, test (testRuntime) and runtime stages and we define all those libraries in dependencies section like:

dependencies {
    compile 'some_groupID:some_artifactID:some_version_x.y.z@ext_if_any'

    testRuntime 'SomeOtherOrSame_groupID:some_other_or_same_artifactID:some_version_x.y.z@ext_if_any'

    runtime 'SomeOtherOrSame_groupID:some_other_or_same_artifactID:some_version_x.y.z@ext_if_any'

}

We have multiple such entries in the dependencies section for all the libraries that our app/service would need for running build/test/IT tests etc (at runtime).

Gradle works good so far and downloads all the artifacts from Artifactory.

I'm working on a project where I need to get all the versions of an artifact (what all is available in the dependencies section) for a single groupID:artifactID only) i.e. I want the following .jar (default extension) and create a zip file containing all the versioned .jars in it:

compile 'my.company.com:cool_library:1.0.0'
compile 'my.company.com:cool_library:1.0.1'
compile 'my.company.com:cool_library:1.1.0'
compile 'my.company.com:cool_library:1.2.0'

runtime 'my.company.com:another_cool_library:1.0.0'
runtime 'my.company.com:another_cool_library:1.2.0'
runtime 'my.company.com:cool_library:1.0.1'

I know how to create a .zip file in Gradle (it's easy) but Gradle is NOT getting/downloading all the .jar (for every version as I have listed above) from Artifactory or from a given binary repository system.

It only gets the latest one (i.e. cool_service-1.2.0.jar). Gradle does this nice because, this way you won't have duplicate class in the class path coming from the same project (cool_library) and due it its different versions mentioned in the dependencies section.

One would say, why I would need OLDER versions when the latest/greatest code is there in version 1.2.0 of cool_library and if a project which is consuming this library wants it, just say I want my.company.com:cool_library:1.2.0 and be done with it or get whatever single version you want for compiling. In my case, DEV team have written application code in a way that they define cool_library-1.0.0.jar here, but cool_library-1.2.0.jar somewhere else. Basically, I need all what a Developer has defined in the build.gradle file.

How can I create a custom_zip.zip file which will have all the cool_library-x.y.z.jar files that I have mentioned in dependencies section for "compile" heading.

Thanks.


Solution

  • Using this great tool, it's possible. Read it all (for different ways to do things).

    Link Gradle Download Task: https://github.com/michel-kraemer/gradle-download-task

    For ex: In your build.gradle you'll have this:

    plugins {
        id "de.undercouch.download" version "1.2"
    }
    
    import de.undercouch.gradle.tasks.download.Download
    apply plugin: 'java'
    apply plugin: 'de.undercouch.download'
    
    
    repositories {
      maven {
        url "http://yourartifatory:1020/artifactory/virtual-repos"
      }
    }
    
    //Lets says, I want to download my.company.com:CoolServices:1.0 jar or any extension file
    //Then I can create a list and specify what all I need as:
    def deps = [
                [ "my/company/com", "CoolServices", "1.0", "jar" ],
                [ "my/company/com", "CoolServices", "1.1", "jar" ],
                [ "my/company/com", "CoolServices", "1.2", "jar" ]
               ]
    
    task downloadArts << {
         //Iterate over each dependencies as per the list
         deps.each { groupId, artifactId, version, extn ->
              download {
                  src "http://yourartifactory.fqdn.com:1020/artifactory/simple/some-physical-actual-repository-local/$groupId/$artifactId/$version/$artifactId-$version.$extn"
                  dest buildDir
                  //If you want the downloaded files inside build/xxx folder, create that xxx folder first outside of the clousure i.e. file("build/xxx").mkdir()
              }
         }
    }
    


    Now, run "gradle clean downloadArts" and it'll print what it's going to download / line and inside either build or build/xxx folder where you'll find all of your downloaded files (.jar/etc extensioned files that you mentioned in the deps list variable).

    You can also use it to push into dependencies section for compile, runtime, testRuntime OR create a custom_zip.zip file if required.

    For ex:

    dependencies {
        compile  fileTree( dir: "build", include: '*.*' )
        runtime  fileTree( dir: "build/xxx", include: '*.*' )
    }
    

    Or create a custom_zip.zip file (containing all the downloaded .jar/extension files in it).