Not Clean Generated Sources

Hey,

My question: How do I tell gradle not to clean the output of my copy task?

My problem: I have a SubModule (ProtoJava) that contains generated sources.
ProtoJava is required by Logic. When I run a junit tests in Logic, ProtoJava is somehow cleaned.
But because the tests requires ProtoJava the test can not be build.

The Test output:

Testing started at 14:24 ...
> Task :core:ProtoJava:compileJava NO-SOURCE
> Task :core:Logic:compileJava FAILED

So a solution I am looking for is eigther to not clean ProtoJava’s source when building tests
or to exclude ProtoJava’s source from that clean.

Project structure (simplified):

root
└ core
   ├ Logic
   ├ Protocol
   ├ ProtoJava
   └ ProtoJs

Protocol is generating the source files for ProtoJava.
The core/Protocol/build.gradle is:

apply(plugin: 'base')

task deleteBuildFiles() {
    project.delete(files('build/generated/source/proto/main/java'))
    project.delete(files('build/generated/source/proto/main/js'))
}

task createBuildDirs() {
    dependsOn(deleteBuildFiles)
    project.mkdir('build/generated/source/proto/main/java')
    project.mkdir('build/generated/source/proto/main/js')
}

task deleteProjectFiles() {
    project.delete(files('../ProtoJava/src/main/java'))
    project.delete(files('../ProtoJs/src/main/js'))
}

task createProjectDirs() {
    dependsOn(deleteProjectFiles)
    project.mkdir('../ProtoJava/src/main/java')
    project.mkdir('../ProtoJs/src/main/js')
}

task generate(type: Exec) {

    dependsOn(createBuildDirs)

    workingDir 'src/main/proto'

    commandLine 'protoc'
    args '--js_out=../../../build/generated/source/proto/main/js',
            '--java_out=../../../build/generated/source/proto/main/java',
            '--proto_path=.',
            'Admin.proto',
            'Barkeeper.proto',
            'Barowner.proto',
            'Customer.proto',
            'Person.proto',
            'Proto.proto'
}

task updateLib(type: Copy) {

    dependsOn(createProjectDirs)
    dependsOn(generate)

    into('..')

    into('ProtoJs/src/main/js') {
        from('build/generated/source/proto/main/js')
    }
    into('ProtoJava/src/main/java') {
        from('build/generated/source/proto/main/java')
    }
}

The core/ProtoJava/build.gradle is:

apply(plugin: 'java-library')

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    maven {
        url('https://plugins.gradle.org/m2/')
    }
}
dependencies {
    implementation('com.google.protobuf:protobuf-java:3.10.0')
}

The core/Logic/build.gradle is:

plugins {
    id('java-library')
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

test {
    useJUnitPlatform()
}

repositories {
    maven {
        url('https://plugins.gradle.org/m2/')
    }
}
dependencies {
    implementation('com.google.protobuf:protobuf-java:3.10.0')
    implementation(project(':core:ProtoJava'))

    testImplementation('com.google.protobuf:protobuf-java:3.10.0')
    testImplementation(project(':core:ProtoJava'))

    testImplementation('org.junit.jupiter:junit-jupiter-api:5.3.1')
    testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}

As I am new to gradle I am happy about every comment.
Thanks for every help!

It looks like you are doing “work” in Gradle’s configuration phase when it should be done in the execution phase. I can see four of your tasks suffering from the same problem

See build phases

Eg:

task deleteProjectFiles() {
    project.delete(files('../ProtoJava/src/main/java'))
    project.delete(files('../ProtoJs/src/main/js'))
}

Should be

task deleteProjectFiles() {
    doLast {
        project.delete(files('../ProtoJava/src/main/java'))
        project.delete(files('../ProtoJs/src/main/js'))
    } 
}