Gradle清理并复制JAR文件

时间:2018-03-21 17:42:50

标签: java gradle shadowjar

我正在用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()    
    }

}

2 个答案:

答案 0 :(得分:2)

clean {
    file("myApp-app/${rootProject.name}.jar").delete()
}

这将在每次评估时删除文件,这不是您想要的。将其更改为:

clean {
    delete "myApp-app/${rootProject.name}.jar"
}

这会配置clean任务并添加要在执行时删除的JAR。

答案 1 :(得分:1)

@nickb关于clean任务是正确的,但您还需要修复copyJar任务。在配置阶段调用copy { ... }方法,因此每次调用gradle。简单地删除方法并使用Copy任务类型的配置方法:

task copyJar(type: Copy) {
    from "build/libs/${rootProject.name}.jar"
    into "myApp-app"
}

同样的问题适用于clean关闭中的allprojects任务。只需将file('out').deleteDir()替换为delete 'out'即可。在documentation中查看有关配置阶段执行阶段之间差异的更多信息。

相关问题