18

我的 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' }
    }
}

但不是直接将这些编译的类打包到一个 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()
}

只有在我检测了这些类之后,我才会想要将它们打包到一个 JAR 文件中。有没有办法让我在包装前做这个仪器?

非常感谢!!!

4

2 回答 2

25

终于想出办法了!

task instrument(type: JavaExec) {
    //your instrumentation task steps here
}
compileJava.doLast {
    tasks.instrument.execute()
}
jar {
    //whatever jar actions you need to do
}

希望这可以防止其他人在这个问题上停留几天:)

于 2014-07-24T07:38:08.077 回答
0

这与问题中提出的内容有点无关。但是想通过思考来提及它可能对某人有所帮助。

我试图用CopyJar 任务之前的类型执行任务。

task copyFilesTask(type: Copy) {

   //do whatever you want in here
}

compileJava.dependsOn(copyFilesTask)

jar {
    // jar actions. example below
    //manifest {
    //    attributes 'Main-Class': '<target-class-name-here>'
    //}
}

我在任务之前完成了这项compileJava工作,因为我需要在创建 jar 存档时相关文件可用。

于 2021-06-03T06:32:17.013 回答