Gradle - 从 Artifactory 下载单个 groupID:artifactID 的所有版本 jar

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

我用Gradle构建了一个项目(Java)。

Gradle2.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'

}

我们的 app/service 需要 运行 build/test/IT 测试等(在运行时)的所有库的依赖项部分中有多个这样的条目。

Gradle 目前运行良好并从 Artifactory 下载所有工件。

我正在做一个项目,我需要为单个 groupID:artifactID 获取工件的所有版本(依赖项部分中所有可用的版本),即我想要以下 .jar (默认扩展名)并创建一个包含所有版本化 .jars 的 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 不是 getting/downloading 所有的 .jar(对于我上面列出的每个版本) 来自 Artifactory 或来自给定的二进制存储库系统。

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

有人会说,当 latest/greatest 代码在 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 文件,其中包含我在 "compile" 标题的依赖项部分中提到的所有 cool_library-x.y.z.jar 文件。

谢谢。

我有一个 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 从中提取的外部存储库。添加您认为合适的任何其他内容(例如 clojure jar 的 clojars):

<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 脚本,它只询问 dirs 寻找丢失的 src 并迭代 retrieve 以这些作为主要条目的任务,但如果你不追求资源,那么你应该没问题。

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

使用这个很棒的工具,一切皆有可能。全部阅读(了解不同的做事方式)。

Link 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 文件夹,您可以在其中找到所有下载的文件(.jar/etc 您在 deps 列表变量中提到的扩展文件)。

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

例如:

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

或者创建一个custom_zip.zip文件(里面包含所有下载的.jar/extension文件)。