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

标签 gradle download artifactory artifact gradle-2

我使用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下载了所有 Artifactory 。

我正在一个项目中,我需要为单个groupID:artifactID获取 Artifactory 的所有版本(在依赖项中可用的所有版本),即我想要以下.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版中有最新/最优秀的代码,并且如果正在使用此库的项目需要它,为什么我需要OLDER版本,只需说我想要my.company.com:cool_library:1.2 .0并完成它,或者获取您想要编译的任何单个版本。在我的情况下,DEV团队以一种在此处定义cool_library-1.0.0.jar的方式编写了应用程序代码,但在其他地方定义了cool_library-1.2.0.jar。基本上,我需要开发人员在build.gradle文件中定义的所有内容。

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

谢谢。

最佳答案

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

其要旨是使用ant.retrieve将文件从所需版本的外部存储库中拉出到目录中,然后按需使用它们。

这不是完整的脚本,但我希望它能起作用。 Ant将使用 Ivy 缓存目录进行缓存,并且包含所有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任务进行迭代,但如果您不追究消息来源,那您应该没事。

如果采用此策略,仍然有一些工作要做,但是它将为您想要的任何依赖项下载所有文件,并且您可以根据自己的需要关闭并压缩所有文件。以上仅适用于直接上传到本地 Ivy 仓库。

关于gradle - Gradle-下载单个groupID的所有版本jar :artifactID from Artifactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31415278/

相关文章:

android - 在使用构建类型和产品风格时如何包含 LeakCanary?

git - 狂欢 :/gradlew: No such file or directory (Windows)

android - 如何下载带有所有依赖项的 Artifact ?

c - 伯克利套接字的网络连接速度慢

java - 从 Maven Artifactory 中提取所有 Artifact ,而不仅仅是 JAR Artifact

npm - 为什么当 curl 可以 200 时 npm 说 404

gradle - 将所有依赖项从一种Gradle配置复制到另一种

Gradle 找不到不存在的前缀版本传递依赖

image - 是否有替代 image_picker_saver 依赖项的方法

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