Java Gradle:如何在编译后但在将文件打包到 Jar 之前添加自定义任务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24899385/
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: How to add a custom task after compilation but before packaging files into a Jar?
提问by Hongyi Li
My build.gradle is currently:
我的 build.gradle 目前是:
project(':rss-middletier') {
apply plugin: 'java'
dependencies {
compile project(':rss-core')
compile 'asm:asm-all:3.2'
compile 'com.sun.jersey:jersey-server:1.9.1'
compile group: 'org.javalite', name: 'activejdbc', version: '1.4.9'
}
jar {
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
manifest { attributes 'Main-Class':
'com.netflix.recipes.rss.server.MiddleTierServer' }
}
}
But rather than packaging these compiled classes into a jar directly, I'd like to instrument them first by running the following task:
但是,与其将这些编译后的类直接打包到 jar 中,我想先通过运行以下任务来检测它们:
task instrument(dependsOn: 'build', type: JavaExec) {
main = 'org.javalite.instrumentation.Main'
classpath = buildscript.configurations.classpath
classpath += project(':rss-middletier').sourceSets.main.runtimeClasspath
jvmArgs '-DoutputDirectory=' + project(':rss-middletier').sourceSets
.main.output.classesDir.getPath()
}
Only after I have instrumented these classes, I will then want to package them into a JAR file. Is there a way so that I can do this instrumentation before the packaging?
只有在我检测了这些类之后,我才会想要将它们打包到一个 JAR 文件中。有没有办法让我可以在包装之前做这个仪器?
Thanks a lot!!!
非常感谢!!!
采纳答案by Hongyi Li
Finally figured out the way to do it!
终于找到方法了!
task instrument(type: JavaExec) {
//your instrumentation task steps here
}
compileJava.doLast {
tasks.instrument.execute()
}
jar {
//whatever jar actions you need to do
}
Hope this can prevent others from being stuck on this problem for days :)
希望这可以防止其他人在这个问题上停留数天:)