Is it possible to partial override clean task?

I have multi-project build. I collecting all my generated jar in one place by redefined the destinationDir :

tasks.withType(Jar) {
  //define directory where the archive is generated into
  destinationDir = "new/location"
  }

It’s working fine, but

the clean task doesn’t ‘know’ the new location. As result, the generated jar not cleaned by clean task. What is best solution for it? Can I partial override task? Or just create my new clean task?

Hello Cincy

you can configure additional custom locations to the clean task:

clean{
    delete "new/location"
}

hope that helps.

cheers,

René

2 Likes

:frowning:

I got:

as a task with that name already exists or

Could not find method clean()

I’m using ‘clean’ task from java plugin. I would like use all that exit in it plus cleaning new location. How I can do it?

can you show your whole build.gradle file? “task with that name already exists” is likely thrown because you create a new task instead of configuring an existing one. ensure that the baseplugin (or the java plugin) is applied before adding the script above.

build.gradle

project(':antBuild').ext.antBuildFlag = true
  allprojects {
   project.ext.libDir = "$rootDir/lib/"
}
  configure(allprojects.findAll { it.hasProperty("antBuildFlag") == false } ) {
 //applying only for project that using java plugin
 apply from: "$rootDir/dep.gradle"
   }
    subprojects {
 tasks.withType(Jar) {
  //define directory where the archive is generated into
  destinationDir = file(libDir)
  }
}
      project(':A') {
 dependencies {
  compile rootProject.files(
  '${libDir}/abc.jar')
     }
}
    project(':B') {
 dependencies {
  compile project(':A')
 }
}
    project(':antBuild') {
 ant.importBuild 'build.xml'
    task build {
  dependsOn 'runbuild1'
//from build.xml
 }
}

$rootDir/dep.gradle

apply plugin: 'java'
  sourceSets.main.java.exclude('**/not_include/**')
    def currentJvm = org.gradle.internal.jvm.Jvm.current()
def gradleVersion = gradle.gradleVersion
  jar.manifest.attributes("Implementation-Title": "Gradle, v" + gradleVersion, "Created-By": currentJvm)
      dependencies {
 compile rootProject.files(
   ...
  )
 }

changing your subproject section in your build.gradle file should do the trick:

...
subprojects {
    tasks.withType(Jar) {
        //define directory where the archive is generated into
        destinationDir = file(libDir)
        }
}
...

Not sure what is difference between the section you quieted and mine…It’s same In this situation the ‘clean’ task is cleaning only build folder, but not the jar file

It’s work if add it to configure - since ant project doesn’t have property ‘clean’ :

configure(allprojects.findAll { it.hasProperty("antBuildFlag") == false } ) {
 //applying only for project that using java plugin
        apply from: "$rootDir/dep.gradle"
          clean << {
   ....
  }
   }

Thank you for the help!