Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle clean and copy JAR file

I am building a java application with Gradle and I want to transfer the final jar file in another folder. I want to copy the file on each build and delete the file on each clean.

Unfortunately, I can do only one of the tasks and not both. When I have the task copyJar activated, it successfully copies the JAR. When I include the clean task, the JAR is not copied and if there is a file there it is deleted. It is as if there is some task that calls clean.

Any solutions?

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()    
    }

}
like image 348
orestis Avatar asked Jan 29 '23 10:01

orestis


2 Answers

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

This will delete the file on evaluation every time, which is not what you want. Change it to:

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

This configures the clean task and adds the JAR to be deleted on execution.

like image 85
nickb Avatar answered Feb 03 '23 13:02

nickb


@nickb is right about the clean task, but you also need to fix your copyJar task. The copy { ... } method is called during configuration phase, so everytime gradle is invoked. Simple remove the method and use the configuration methods of the Copy task type:

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

The same problem applies for the clean task in the allprojects closure. Simply replace file('out').deleteDir() with delete 'out'. Check out more information about the difference between configuration phase and execution phase in the documentation.

like image 40
Lukas Körfer Avatar answered Feb 03 '23 12:02

Lukas Körfer