Adding Files to Custom Configuration

(Mike Damman) #1

I want to recreate the same functionality that java plugin does with a custom plugin. Below when I run :project-a:print gradle builds project-b first and attaches the jar to the configuration and then runs the print task for project-a.

allprojects {
    apply plugin: 'java'

   task('print') {
         dependsOn project.configurations.compile
        doLast {
             println project.configurations.compile.getFiles()
             println project.configurations.compile.artifacts
             println project.configurations.compile.dependencies
        }
   }
}

project(':project-a'){
      dependencies {
          compile project(':project-b')
      }
}

Output:

gradle :project-a:print --console=plain
> Task :project-b:compileJava UP-TO-DATE
> Task :project-b:processResources NO-SOURCE
> Task :project-b:classes UP-TO-DATE
> Task :project-b:jar UP-TO-DATE

> Task :project-a:print
[/Users/md011873/code/md011873/configuration-issue/project-b/build/libs/project-b.jar]
[]
[DefaultProjectDependency{dependencyProject='project ':project-b'', configuration='default'}]

BUILD SUCCESSFUL in 0s
3 actionable tasks: 1 executed, 2 up-to-date

My Custom Plugin

allprojects {
    apply plugin: 'base'

    configurations {
        custom
    }

    task compile (type: Compile) {
    }

    task('print') {
        dependsOn project.configurations.custom
        doLast {
            println project.configurations.custom.getFiles()
            println project.configurations.custom.artifacts
            println project.configurations.custom.dependencies
        }
    }
}

project(':project-a'){
    dependencies {
        custom project(':project-b')
    }
}

class Compile extends DefaultTask {
    @OutputDirectory
    final File binBuildDir = new File(project.buildDir, 'bin')

    @TaskAction
    void exec() {
        println "Creating files for ${project.name}"
        File jsonFile = new File(binBuildDir,'test.json')
        File binFile = new File(binBuildDir,'test.bin')

        jsonFile.createNewFile()
        binFile.createNewFile()
    }
}

Output:

gradle :project-a:print --console=plain

> Task :project-a:print
[]
[]
[DefaultProjectDependency{dependencyProject='project ':project-b'', configuration='default'}]

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

How do I associate the files created by the Compile task to the custom configuration? Also, how do I ensure that the compile task runs for project-b when :project-a:print runs like it does with the Java example.

(Mike Damman) #2

Am I approaching this the wrong way? How do I share files across subprojects?