【问题标题】:Gradle - Download all version jars of a single groupID:artifactID from ArtifactoryGradle - 从 Artifactory 下载单个 groupID:artifactID 的所有版本 jar
【发布时间】:2015-07-14 19:08:58
【问题描述】:

我使用 Gradle 构建项目 (Java)。

Gradle 2.3,Java7/8。效果很好。

为了构建项目,我们定义了在编译、测试 (testRuntime) 和运行时阶段所需的所有库,我们在依赖项部分定义所有这些库,例如:

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'

}

对于我们的应用/服务运行构建/测试/IT 测试等(在运行时)所需的所有库,我们在依赖项部分有多个此类条目。

到目前为止,Gradle 运行良好,可以从 Artifactory 下载所有工件。

我正在做一个项目,我需要为单个 groupID:artifactID 获取工件的所有版本(依赖项部分中所有可用的版本),即我想要以下 .jar(默认扩展名)并创建一个包含所有版本化 .jar 的 zip 文件:

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'

我知道如何在 Gradle 中创建 .zip 文件(这很简单),但 Gradle 不会从 Artifactory 或给定的二进制存储库系统中获取/下载所有 .jar(对于我上面列出的每个版本)。

它只获取最新的(即cool_service-1.2.0.jar)。 Gradle 做得很好,因为这样你就不会在来自同一个项目(cool_library)的类路径中有重复的类,并且由于它的依赖项部分提到的不同版本。

有人会说,当cool_library 1.2.0 版本中有最新/最好的代码时,为什么我需要旧版本,如果正在使用这个库的项目想要它,就说我想要my.company .com:cool_library:1.2.0 并完成它或获取您想要编译的任何单个版本。在我的例子中,开发团队编写了应用程序代码,他们在这里定义了cool_library-1.0.0.jar,但在其他地方定义了cool_library-1.2.0.jar。基本上,我需要开发人员在 build.gradle 文件中定义的所有内容。

如何创建一个 custom_zip.zip 文件,其中包含我在“编译”标题的依赖项部分提到的所有cool_library-x.y.z.jar 文件。

谢谢。

【问题讨论】:

    标签: gradle download artifactory artifact gradle-2


    【解决方案1】:

    我有一个 gradle 项目,用于将特定的依赖版本下载到一个目录中,然后我会将其上传到我们本地的 IVY 存储库,以便我们控制并拥有可用的特定版本的软件,以防它们消失。

    它的要点是使用 ant.retrieve 将文件从外部存储库中为您所追求的版本拉到一个目录中,然后按照您的意愿处理它们。

    这不是完整的脚本,但我希望它足够工作。 Ant 将使用 ivy-cache 目录作为缓存,而包含所有 jar 文件的“repo”将位于 ivy-repo 目录中。您可以调整 antRetrieve 任务以展平您的文件结构(更改模式变量),并根据需要删除 ivy/src 文件,但我向您展示我拥有的脚本:

    project.ext.REPO_DOWNLOADER_DIR = "ivy-repo"
    project.ext.IVY_SETTINGS = 'ivy-settings.xml' 
    project.ext.IVY_CACHE = file('ivy-cache')
    
    dependencies {
        compile ':ivy:2.3+'
    }
    
    def antRetrieve(dep) {
        // in each of the following patterns, the file will be downloaded with the [name] values filled in correctly,
        // e.g. from ant:retrieve(..."src,source"), the [type] will be either "src" or "source" depending on which was available to download.
        def jarPattern = "$REPO_DOWNLOADER_DIR/[organisation]/[revision]/[module]/[type]s/[artifact]-[revision].[ext]"
        def srcPattern = "$REPO_DOWNLOADER_DIR/[organisation]/[revision]/[module]/[type]s/[artifact]-[revision](-[classifier]).[ext]"
        def docPattern = "$REPO_DOWNLOADER_DIR/[organisation]/[revision]/[module]/[type]s/[artifact]-[revision](-[classifier]).[ext]"
        def ivyPattern = "$REPO_DOWNLOADER_DIR/[organisation]/[revision]/[module]/ivy.xml"
        def (org, module, rev) = dep.split(':')
    
        println "retrieving $org:$module:$rev"
    
        ant.retrieve(inline: "true", type: "jar,bundle", transitive: "true", pattern: "$jarPattern", ivypattern: "$ivyPattern", organisation: "$org", module: "$module", revision: "$rev", conf: "runtime,default")
        ant.retrieve(inline: "true", type: "src,source,sources", transitive: "true", pattern: "$srcPattern", organisation: "$org", module: "$module", revision: "$rev", conf: "sources")
        ant.retrieve(inline: "true", type: "doc,docs,javadoc,javadocs", transitive: "true", pattern: "$docPattern", organisation: "$org", module: "$module", revision: "$rev", conf: "javadoc")
    
    }
    
    task retrieve << {
        ant.taskdef(resource: 'org/apache/ivy/ant/antlib.xml', classpath: configurations.compile.asPath)
        ant.properties['repo.downloader.cache'] = IVY_CACHE
        ant.settings(file: IVY_SETTINGS)
    
        if (project.hasProperty('modules')) {
            def moduleList = project.modules.split(',')
            moduleList.each { module ->
                antRetrieve(module)
            }
        }
    
        if (project.hasProperty("modfile")) {
            def modulesFile = file(project.modfile)
            if (! modulesFile.exists()) {
                throw new GradleException("Could not find specified modules file: $modulesFile")
            }
            modulesFile.eachLine { module ->
                antRetrieve(module)
            }
        }
    }
    

    然后 ivy-settings.xml 文件设置您希望 ant 从中提取的外部存储库。添加您认为合适的任何其他内容(例如 clojars for clojure jars):

    <ivysettings>
        <settings defaultResolver="spring-chain" />
        <caches defaultCacheDir="${repo.downloader.cache}" />
        <resolvers>
            <chain name="spring-chain">
                <url name="com.springsource.repository.bundles.release">
                    <ivy pattern="http://repository.springsource.com/ivy/bundles/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
                    <artifact pattern="http://repository.springsource.com/ivy/bundles/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
                </url>
                <url name="com.springsource.repository.bundles.external">
                    <ivy pattern="http://repository.springsource.com/ivy/bundles/external/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
                    <artifact pattern="http://repository.springsource.com/ivy/bundles/external/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
                </url>
                <ibiblio name="ibiblio" m2compatible="true" />
                <ibiblio name="uk-maven" m2compatible="true" root="http://uk.maven.org/maven2"/>
            </chain>
        </resolvers>
    </ivysettings>
    

    您可以使用以下方法之一调用整个事物:

    ./gradlew retrieve -Pmodules='a:b:1.0,x:y:1.1'
    ./gradlew retrieve -PmodFile='/path/to/modlist.txt'
    

    允许您将所有版本转储到文本文件中,或在命令行中指定它们。

    这也会降低依赖关系。它唯一不做的就是拉取 src 文件以获取依赖项(它用于主命名存档),但我有一个 shell 脚本,它只是询问寻找丢失 src 的 dirs 并以那些作为 main 迭代 retrieve 任务条目,但如果你不追求资源,那么你应该没问题。

    如果您采用此策略,仍有一些工作要做,但它会下载您想要的任何依赖项的所有文件,您可以根据需要将它们全部压缩以供您自己使用。以上仅适用于直接上传到本地常春藤仓库。

    【讨论】:

    • 谢谢马克。我发现了另一种更简单的方法,只使用 Gradle 而没有 ivy 或 .xml 方式。
    【解决方案2】:

    使用这个伟大的工具,这是可能的。通读一遍(了解不同的做事方式)。

    链接Gradle 下载任务https://github.com/michel-kraemer/gradle-download-task

    例如:在您的 build.gradle 中,您将拥有:

    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()
              }
         }
    }
    


    现在,运行“gradle clean downloadArts”,它会在 buildbuild/xxx 文件夹中打印要下载的内容/行您将在其中找到所有下载的文件(您在 deps 列表变量中提到的 .jar/etc 扩展文件)。

    您还可以使用它来推送到编译、运行时、testRuntime 的依赖项部分,或者根据需要创建一个 custom_zip.zip 文件。

    例如:

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

    或者创建一个 custom_zip.zip 文件(其中包含所有下载的 .jar/扩展文件)。

    【讨论】:

      猜你喜欢
      • 2013-11-11
      • 2012-04-26
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      • 2015-12-04
      相关资源
      最近更新 更多