0

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

摇篮 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-xyzjar 文件。

谢谢。

4

2 回答 2

2

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

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

于 2015-07-14T21:30:34.880 回答
0

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

链接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 列表变量)。

如果需要,您还可以使用它来推送到依赖项部分以进行编译、运行时、testRuntime 或创建 custom_zip.zip 文件。

例如:

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

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

于 2015-07-16T22:37:42.193 回答