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

Gradle-从Artifactory下载单个groupID:artifactID的所有版本JAR,gradle,download,artifactory,artifact,gradle-2,Gradle,Download,Artifactory,Artifact,Gradle 2,我使用Gradle构建一个项目(Java) Gradle2.3,Java7/8。效果很好 为了构建项目,我们定义了编译、测试(testRuntime)和运行时阶段需要的所有库,并在dependencies部分定义了所有这些库,如: dependencies { compile 'some_groupID:some_artifactID:some_version_x.y.z@ext_if_any' testRuntime 'SomeOtherOrSame_groupID:some

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

Gradle2.3,Java7/8。效果很好

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

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'

}
我们的应用程序/服务在运行build/test/IT测试等(运行时)时需要的所有库的依赖项部分中都有多个这样的条目

到目前为止,Gradle工作得很好,从Artifactory下载了所有工件

我正在进行一个项目,在这个项目中,我需要为单个groupID:artifactID获取工件的所有版本(在dependencies部分中提供的所有版本),即我需要以下.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_库)的重复类,也不会因为依赖项部分提到的不同版本而重复

有人会说,当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文件中定义的所有内容

如何创建一个自定义的_zip.zip文件,其中包含我在“compile”标题的依赖项部分中提到的所有酷的_library-x.y.z.jar文件


谢谢。

我有一个gradle项目,我用它将特定的依赖版本下载到一个目录中,然后上传到我们当地的IVY repo,这样我们就可以控制并拥有特定版本的软件,以防它们消失

其要点是使用ant.retrieve从外部repo中将所需版本的文件下拉到一个目录中,然后按照您的意愿处理它们

这不是完整的脚本,但我希望它足够工作了。Ant将使用ivy缓存目录进行缓存,包含所有jar文件的“repo”将位于
ivy repo
dir中。您可以调整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)
        }
    }
}
它允许您将所有版本转储到文本文件中,或在命令行中指定它们

这也会降低依赖性。它唯一没有做的事情是拉取src文件以获取依赖项(它对主命名归档文件有此功能),但我有一个shell脚本,它只是询问dirs以查找缺少的src,并用这些作为主条目迭代
retrieve
任务,但是如果您不查找源代码,那么应该没问题


如果您采用此策略,仍有一些工作要做,但它将下载您想要的任何依赖项的所有文件,并且您可以根据自己的消费需要将它们全部打包。以上内容只适用于直接上传到本地常春藤回购协议。

使用这个伟大的工具,这是可能的。阅读所有内容(了解不同的做事方式)

链接渐变下载任务

对于ex:在您的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()
          }
     }
}

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

谢谢标记。我找到了另一种更简单的方法,只使用Gradle,而不使用ivy或.xml。
./gradlew retrieve -Pmodules='a:b:1.0,x:y:1.1'
./gradlew retrieve -PmodFile='/path/to/modlist.txt'
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()
          }
     }
}
dependencies {
    compile  fileTree( dir: "build", include: '*.*' )
    runtime  fileTree( dir: "build/xxx", include: '*.*' )
}