我有一个自定义的编译任务。
task compileSpeedTest(type: JavaCompile) {
classpath = files('build')
source = fileTree('src/test/java/speed')
destinationDir = file('bin')
}
在执行之前,Gradle不会尝试下载依赖项。 我找不到任何尝试这样做的任务名称,以便将其添加到dependsOn列表中。
我有一个自定义的编译任务。
task compileSpeedTest(type: JavaCompile) {
classpath = files('build')
source = fileTree('src/test/java/speed')
destinationDir = file('bin')
}
在执行之前,Gradle不会尝试下载依赖项。 我找不到任何尝试这样做的任务名称,以便将其添加到dependsOn列表中。
如果您确实需要将Java依赖项下载到文件夹中,那么这是可能的。
例如:
apply plugin: 'java'
dependencies {
runtime group: 'com.netflix.exhibitor', name: 'exhibitor-standalone', version: '1.5.2'
runtime group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.6'
}
repositories { mavenCentral() }
task getDeps(type: Copy) {
from sourceSets.main.runtimeClasspath
into 'runtime/'
}
执行 gradle getDeps
命令时,将依赖项(及其依赖项)下载到 runtime
文件夹中。
Caused by: java.lang.NoClassDefFoundError: org/gradle/api/internal/plugins/DefaultConvention
的错误。在Gitlab上使用Gradle的用户https://github.com/gradle/gradle/issues/11769表示,Intellij必须有实际版本才能使其正常工作。 - JeSamvn dependency:resolve
,这个命令非常自解释。 - Dark Star1我发现这个回答https://dev59.com/wlYN5IYBdhLWcg3wqpmH#47107135也非常有帮助:
gradle dependencies
将列出依赖项并在附带效果中下载它们。
一项稍微轻松的任务,不会不必要地复制文件到目录中:
task downloadDependencies(type: Exec) {
configurations.testRuntime.files
commandLine 'echo', 'Downloaded all dependencies'
}
已更新至Kotlin和Gradle 6.2.0,并添加了buildscript依赖项解析:
fun Configuration.isDeprecated() = this is DeprecatableConfiguration && resolutionAlternatives != null
fun ConfigurationContainer.resolveAll() = this
.filter { it.isCanBeResolved && !it.isDeprecated() }
.forEach { it.resolve() }
tasks.register("downloadDependencies") {
doLast {
configurations.resolveAll()
buildscript.configurations.resolveAll()
}
}
inputs
和dependsOn
指令(包括Copy
任务的from
)将强制Gradle构建所有上游工件。当您只想下载依赖项时(而不一定要准备上游工件)时,这是一个完美的解决方案。谢谢。 - Mike HilltestRuntime
应该改为runtimeClasspath
。 - moffeltje这个版本是在Robert Elliot的基础上构建的,但我并不完全确定它的功效。
// There are a few dependencies added by one of the Scala plugins that this cannot reach.
task downloadDependencies {
description "Pre-downloads *most* dependencies"
doLast {
configurations.getAsMap().each { name, config ->
println "Retrieving dependencies for $name"
try {
config.files
} catch (e) {
project.logger.info e.message // some cannot be resolved, silentlyish skip them
}
}
}
}
我试图将其放入配置中而非操作中(通过删除 doLast),但这样做会破坏锌。我绕过了它,但最终结果与有或没有都是相同的。因此,我将其保留为显式状态。它似乎足够工作以减少稍后需要下载的依赖项,但在我的情况下无法消除它们。我认为其中一个Scala插件稍后添加了依赖项。
task getDeps(type: Copy) {
from configurations.runtime
into 'runtime/'
}
在 Robert Elliot 的回答基础上,进一步说明。如果出于某些原因,有人希望将 Gradle 缓存中的依赖项下载并复制到本地仓库(例如 Maven 的默认位置 ~/.m2/repository
)中:
task downloadDependencies(type: Exec) {
configurations.implementation.files + configurations.runtimeOnly.files
finalizedBy "cacheToMavenLocal"
commandLine "echo", "Downloaded all dependencies and copied to mavenLocal"
}
task cacheToMavenLocal(type: Copy) {
from new File(gradle.gradleUserHomeDir, "caches/modules-2/files-2.1")
into repositories.mavenLocal().url
eachFile {
List<String> parts = it.path.split("/")
it.path = [parts[0].replace(".","/"), parts[1], parts[2], parts[4]].join("/")
}
includeEmptyDirs false
}
任务cacheToMavenLocal
是从@Adrodoc55在Gradle论坛上的答案中复制并调整而来的
从问题中很难确定您想要做什么。我猜测您想要在Java插件提供的编译任务之外添加一个额外的编译任务。
最简单的方法可能是指定一个名为“speedTest”的新sourceSet
。这将生成一个名为“speedTest”的configuration
,您可以在其中使用dependencies
块来指定依赖项。它还将为您生成一个名为compileSpeedTestJava
的任务。
有关示例,请参阅defining new source sets和Java plugin documentation。
总的来说,似乎您对Gradle的依赖管理方式存在一些错误的假设。我建议您再次阅读用户指南中的“依赖管理”章节 :)
没有下载依赖项的任务;它们是按需下载的。要了解如何使用Gradle管理依赖项,请参阅Gradle用户指南中的“第8章 依赖项管理基础知识”。
unzipDepenencies
任务,简单的dependsOn: configurations.compile
似乎行不通。如果我只是调用该任务,则如果它们不存在,则不会首先检索依赖项。换句话说:Gradle如何知道何时存在对依赖项的需求? - sschuberth