Java gradle - 生成后复制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30636905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gradle - copy file after its generation
提问by wazz
I try to build jar and after that copy it to another folder.
我尝试构建 jar,然后将其复制到另一个文件夹。
task createJar(type: Jar) {
archiveName = "GradleJarProject.jar"
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'me.test.Test'
}
baseName = project.name
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
task copyJarToBin {
copy {
from 'build/libs/GradleJarProject.jar'
into "d:/tmp"
}
}
task buildApp (dependsOn: [clean, createJar, copyJarToBin])
But I can't figure out one problem. copyJarToBin task try to copy old jar. If I delete /build folder in the project and run buildApp() task, task createJar() will generate .jar file, but copyJarToBin() won't find that .jar file.
但我想不出一个问题。copyJarToBin 任务尝试复制旧 jar。如果我删除项目中的 /build 文件夹并运行 buildApp() 任务,任务 createJar() 将生成 .jar 文件,但 copyJarToBin() 将找不到该 .jar 文件。
Could you help me?
Thanks.
你可以帮帮我吗?
谢谢。
采纳答案by Rene Groeschke
The culprit is your copyJarToBin
task. when doing
罪魁祸首是你的copyJarToBin
任务。做的时候
task copyJarToBin {
copy {
from 'build/libs/GradleJarProject.jar'
into "d:/tmp"
}
}
you copy the jar during the configuration time by using the copy
method. (see the gradle user guide at https://docs.gradle.org/current/userguide/userguide_single.html#sec:build_phasesto understand the build lifecycle)
You want to run the actual copy operation during the execution phase (the execution of the task).
您在配置期间使用copy
方法复制 jar 。(请参阅https://docs.gradle.org/current/userguide/userguide_single.html#sec:build_phases上的 gradle 用户指南以了解构建生命周期)您希望在执行阶段(执行任务)。
One way to solve that is to move the call of the copy
method into a doLast block:
解决这个问题的copy
一种方法是将方法的调用移动到 doLast 块中:
task copyJarToBin {
doLast {
copy {
from 'build/libs/GradleJarProject.jar'
into "d:/tmp"
}
}
}
The problem with this approach is that you won't benefit of gradles incremental build feature and copy that file every single time you execute the task even though the file hasn't changed.
这种方法的问题在于,即使文件没有更改,您也不会受益于 gradles 增量构建功能,也不会在每次执行任务时复制该文件。
A better and more idiomatic way of writing your copyJarToBin task is to change your task implementation to use the Copy
task type:
编写 copyJarToBin 任务的更好、更惯用的方法是更改任务实现以使用Copy
任务类型:
task copyJarToBin(type: Copy) {
from 'build/libs/GradleJarProject.jar'
into "d:/tmp"
}
We can even improve this snippet by taking advantage of gradle's autowiring feature. You can declare the output of one task as input to another. So instead of writing `build/libs/GradleJarProject.jar' you can simply do:
我们甚至可以利用 gradle 的自动装配功能来改进这个片段。您可以将一项任务的输出声明为另一项任务的输入。因此,您可以简单地执行以下操作,而不是编写“build/libs/GradleJarProject.jar”:
task copyJarToBin(type: Copy) {
from createJar // shortcut for createJar.outputs.files
into "d:/tmp"
}
Now you don't need to bother about task ordering as gradle know that the createJar
task must be executed before the copyJarToBin
task can be executed.
现在您无需担心任务排序,因为 gradle 知道createJar
必须先执行copyJarToBin
任务才能执行任务。
回答by Theresa Forster
You probably need to ensure they are run in the right order,
您可能需要确保它们以正确的顺序运行,
task copyJarToBin(type:Copy,dependsOn:[createJar]) {
copy {
from "${buildDir}/GradleJarProject.jar" // needs to be gstring
into "d:/tmp"
}
}
回答by Peter T.
I think the above answer is somehow old. Here is an answer using gradle 3.3
我认为上面的答案有点旧。这是使用 gradle 3.3 的答案
jar {
baseName = 'my-app-name'
version = '0.0.1'
}
task copyJar(type: Copy) {
from jar // here it automatically reads jar file produced from jar task
into 'destination-folder'
}
build.dependsOn copyJar
回答by Abhijit Patil
Just made few corrections to above Answers:
刚刚对上述答案做了一些更正:
jar {
baseName = "$artifactId"
version = '0.0.1'
}
task copyJar(type: Copy) {
from jar // copies output of file produced from jar task
into 'destination-folder'
}
build.finalizedBy copyJar