我有一个 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
使用这些作为主要条目来迭代任务,但是如果你不追求资源,那么你应该没问题。
如果您采用此策略,仍有一些工作要做,但它会下载您想要的任何依赖项的所有文件,您可以根据需要将它们全部压缩以供您自己使用。以上仅适用于直接上传到本地常春藤回购。