我正在使用 Gradle 构建一个 Java 应用程序,我想将最终的 jar 文件传输到另一个文件夹中。我想复制每个build
上的文件并删除每个clean
上的文件。
不幸的是,我只能完成其中一项任务,而不能同时完成两项任务。当我激活任务copyJar
时,它成功复制了 JAR。当我包含clean
任务时,不会复制 JAR,如果那里有文件,则会将其删除。就好像有什么任务在召唤clean
.
有什么解决办法吗?
plugins {
id 'java'
id 'base'
id 'com.github.johnrengelman.shadow' version '2.0.2'
}
dependencies {
compile project(":core")
compile project("fs-api-reader")
compile project(":common")
}
task copyJar(type: Copy) {
copy {
from "build/libs/${rootProject.name}.jar"
into "myApp-app"
}
}
clean {
file("myApp-app/${rootProject.name}.jar").delete()
}
copyJar.dependsOn(build)
allprojects {
apply plugin: 'java'
apply plugin: 'base'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
compile 'org.slf4j:slf4j-api:1.7.12'
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '0.9.26'
}
sourceSets {
test {
java.srcDir 'src/test/java'
}
integration {
java.srcDir 'src/test/integration/java'
resources.srcDir 'src/test/resources'
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
}
}
configurations {
integrationCompile.extendsFrom testCompile
integrationRuntime.extendsFrom testRuntime
}
task integration(type: Test, description: 'Runs the integration tests.', group: 'Verification') {
testClassesDirs = sourceSets.integration.output.classesDirs
classpath = sourceSets.integration.runtimeClasspath
}
test {
reports.html.enabled = true
}
clean {
file('out').deleteDir()
}
}
clean {
file("myApp-app/${rootProject.name}.jar").delete()
}
这将每次删除评估时的文件,这不是您想要的。将其更改为:
clean {
delete "myApp-app/${rootProject.name}.jar"
}
这将配置清理任务并添加要在执行时删除的 JAR。
clean
任务是正确的,但你还需要修复你的copyJar
任务。copy { ... }
方法在配置阶段调用,因此每次都会调用 gradkle。只需删除该方法并使用Copy
任务类型的配置方法:
task copyJar(type: Copy) {
from "build/libs/${rootProject.name}.jar"
into "myApp-app"
}
同样的问题适用于allprojects
闭包中的clean
任务。只需将file('out').deleteDir()
替换为 delete 'out'
即可。在文档中查看有关配置阶段和执行阶段之间差异的更多信息。