繁体   English   中英

Gradle清理并复制JAR文件

[英]Gradle clean and copy JAR file

我正在用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。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM