Skip to content

JCP is not Gradle clean aware #43

Open

Description

@Jolanrensen

Version 7.0.5, Gradle 7.4

Currently, the default target of the preprocess task is "build/java-comment-preprocessor/preprocess". This is inside the build folder, which gets deleted entirely when running the clean task. However, if I run preprocess again after a clean, I get > Task :core:preprocess UP-TO-DATE, after which, the preprocess folder won't be regenerated anymore.
Currently, my solution is to enable clearTarget and move the target outside of the build folder so it survives a clean and clears itself. This is not ideal, however.

Activity

  1. raydac commented on Jun 28, 2022

    @raydac
    Owner

    could you try construction, may be it can help

    clean {
        delete 'preprocessed-path'
    }
  2. self-assigned this
    on Jun 28, 2022
  3. raydac commented on Jun 28, 2022

    @raydac
    Owner

    also I saw some examples where guys recommend to use construction for tasks

    outputs.upToDateWhen { false }
  4. Jolanrensen commented on Jun 28, 2022

    @Jolanrensen
    Author

    I think outputs.upToDateWhen { false } works!
    The java-comment-preprocessor folder gets regenerated after every clean.
    I'm not sure everything works yet since I've got some problems with Kotlin sources thinking they are redeclarations, but I still have some things to try out :).
    Thanks!

  5. Jolanrensen commented on Jun 28, 2022

    @Jolanrensen
    Author

    Okay nice! It works :) thanks!

    For future reference, I'll put some examples for projects with Kotlin and Scala code (using gradle kts):

    val kotlinMainSources = kotlin.sourceSets.main.get().kotlin.sourceDirectories
    
    val preprocessMain by tasks.creating(JcpTask::class) {
        sources.set(kotlinMainSources)
        clearTarget.set(true)
        fileExtensions.set(listOf("kt"))
        vars.set(Versions.versionMap)
        outputs.upToDateWhen { target.get().exists() }
    }
    
    tasks.compileKotlin {
        dependsOn(preprocessMain)
        outputs.upToDateWhen {
            preprocessMain.outcomingFiles.files.isEmpty()
        }
    
        doFirst {
            kotlin {
                sourceSets {
                    main {
                        kotlin.setSrcDirs(listOf(preprocessMain.target.get()))
                    }
                }
            }
        }
    
        doLast {
            kotlin {
                sourceSets {
                    main {
                        kotlin.setSrcDirs(kotlinMainSources)
                    }
                }
            }
        }
    }
    val scalaMainSources = sourceSets.main.get().scala.sourceDirectories
    
    val preprocessMain by tasks.creating(JcpTask::class)  {
        sources.set(scalaMainSources)
        clearTarget.set(true)
        fileExtensions.set(listOf("scala"))
        vars.set(Versions.versionMap)
        outputs.upToDateWhen { target.get().exists() }
    }
    
    tasks.compileScala {
        dependsOn(preprocessMain)
        outputs.upToDateWhen { 
            preprocessMain.outcomingFiles.files.isEmpty()
        }
    
        doFirst {
            scala {
                sourceSets {
                    main {
                        scala.setSrcDirs(listOf(preprocessMain.target.get()))
                    }
                }
            }
        }
    
        doLast {
            scala {
                sourceSets {
                    main {
                        scala.setSrcDirs(scalaMainSources)
                    }
                }
            }
        }
    }

    Edit: Updated so IntelliJ plays nice by only temporarily changing the source folders.
    Edit2: Updated again to make intellij not do incremental code changes without jcp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions