Exclude for copy task


(Leander Kurscheidt) #1

i have this task:

task plugin(type: Zip) {
   baseName = project.name
   version = project.version
   def allExcluded = [];
   from (configurations.compile) {
       exclude(allExcluded)
       into ('libs/')
   }
   from (sourceSets.main.output.classesDir) {
       into ('classes/')
   }
  .....

   doLast {
       def excluded = ["org.intellimate.izou:izou"];
       def recursiveAdd
       recursiveAdd = { dep ->
           allExcluded.add("${dep.module.id.name}-${dep.module.id.version}.jar".toString())
           dep.children.each { childResolvedDep ->
               if(dep in childResolvedDep.getParents() &&    childResolvedDep.getConfiguration() == 'compile'){
                   recursiveAdd(childResolvedDep);
               }
           }
       }

       configurations.compile.resolvedConfiguration.firstLevelModuleDependencies.each { dep ->
           if (excluded.contains("${dep.module.id.group}:${dep.module.id.name}".toString())) {
            recursiveAdd(dep)
           }
       }
       println allExcluded
    }
}

i prints [izou-1.16.5.jar,...yamlbeans-1.09.jar], so the list gets populated, but somehow it does not exclude the files. is my usage of doLast causing this?


(Robert Hencke) #2

Yes - to understand why this occurs, you need to understand the Gradle build lifecycle.

There are three phases to each Gradle build: initialization, then configuration, then execution.

doLast happens during the execution phase. The configuration block (everything between the curly braces in ```task plugin(type: Zip) { /* here */ }`` happens during the configuration phase.

Take a look at the build lifecycle here, specifically, the example for task testBoth.

What are you trying to do here? Why are you excluding these libraries? It might be there’s another way to solve your problem.