diff --git a/build.gradle b/build.gradle index a04ebec..5ddfcf7 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ plugins { } group = "com.github.jk1" -version = "1.1" +version = "1.2" sourceCompatibility = 1.6 targetCompatibility = 1.6 diff --git a/src/main/groovy/com/github/jk1/license/LicenseReportExtension.groovy b/src/main/groovy/com/github/jk1/license/LicenseReportExtension.groovy index a308523..ac6b455 100644 --- a/src/main/groovy/com/github/jk1/license/LicenseReportExtension.groovy +++ b/src/main/groovy/com/github/jk1/license/LicenseReportExtension.groovy @@ -69,16 +69,4 @@ class LicenseReportExtension { excludes.contains("$module.moduleGroup:$module.moduleName") } - // configuration snapshot for the up-to-date check - String getSnapshot() { - StringBuilder builder = new StringBuilder() - projects.each { builder.append(it.name) } - renderers.each { builder.append(it.class.name) } - importers.each { builder.append(it.class.name) } - filters.each { builder.append(it.class.name) } - configurations.each { builder.append(it) } - excludeGroups.each { builder.append(it) } - excludes.each { builder.append(it) } - return builder.toString() - } } diff --git a/src/main/groovy/com/github/jk1/license/ReportTask.groovy b/src/main/groovy/com/github/jk1/license/ReportTask.groovy index fac54fd..4b80ffc 100644 --- a/src/main/groovy/com/github/jk1/license/ReportTask.groovy +++ b/src/main/groovy/com/github/jk1/license/ReportTask.groovy @@ -17,23 +17,50 @@ package com.github.jk1.license import com.github.jk1.license.reader.ProjectReader import org.gradle.api.DefaultTask +import org.gradle.api.file.FileCollection import org.gradle.api.logging.Logger import org.gradle.api.logging.Logging +import org.gradle.api.tasks.CacheableTask import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.TaskAction +@CacheableTask class ReportTask extends DefaultTask { private Logger LOGGER = Logging.getLogger(ReportTask.class) + @InputFiles + FileCollection getClasspath() { + ProjectReader.findConfigured(getProject()).inject(project.files(), { FileCollection memo, eachConfiguration -> + memo + eachConfiguration + }) + } + @Input - String getConfigurationSnapshot(){ - return getProject().licenseReport.snapshot + String getConfigurationSnapshot() { + LicenseReportExtension licenseReport = getProject().licenseReport + def snapshot = [] + snapshot << 'projects' + snapshot += licenseReport.projects.collect { it.path } + snapshot << 'renderers' + snapshot += licenseReport.renderers.collect { it.class.name } + snapshot << 'importers' + snapshot += licenseReport.importers.collect { it.class.name } + snapshot << 'filters' + snapshot += licenseReport.filters.collect { it.class.name } + snapshot << 'configurations ' + snapshot += licenseReport.configurations + snapshot << 'exclude' + snapshot += licenseReport.excludeGroups + snapshot << 'excludes' + snapshot += licenseReport.excludes + snapshot.join("!") } @OutputDirectory - File getOutputFolder(){ + File getOutputFolder() { return new File(getProject().licenseReport.outputDir) } diff --git a/src/main/groovy/com/github/jk1/license/reader/ProjectReader.groovy b/src/main/groovy/com/github/jk1/license/reader/ProjectReader.groovy index 0a573fc..d79e99b 100644 --- a/src/main/groovy/com/github/jk1/license/reader/ProjectReader.groovy +++ b/src/main/groovy/com/github/jk1/license/reader/ProjectReader.groovy @@ -72,7 +72,7 @@ class ProjectReader { } } - private static Set findConfigured(Project project) { + static Set findConfigured(Project project) { project.configurations.findAll { config -> config.name in project.licenseReport.configurations } } diff --git a/src/test/groovy/com/github/jk1/license/ReportTaskTestSpec.groovy b/src/test/groovy/com/github/jk1/license/ReportTaskTestSpec.groovy new file mode 100644 index 0000000..6cd68dc --- /dev/null +++ b/src/test/groovy/com/github/jk1/license/ReportTaskTestSpec.groovy @@ -0,0 +1,105 @@ +/* + * Copyright 2018 Evgeny Naumenko + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.jk1.license + +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.TaskOutcome +import org.junit.Rule +import org.junit.rules.TemporaryFolder +import spock.lang.Specification + +class ReportTaskTestSpec extends Specification { + @Rule + final TemporaryFolder testProjectDir = new TemporaryFolder() + + File buildFile + File localBuildCacheDirectory + + def setup() { + buildFile = testProjectDir.newFile('build.gradle') + localBuildCacheDirectory = testProjectDir.newFolder('.local-cache') + testProjectDir.newFile('settings.gradle') << """ + buildCache { + local { + directory '${localBuildCacheDirectory.toURI()}' + } + } + """ + + } + + def "should cache task outputs"() { + given: + buildFile << """ + plugins { + id 'com.github.jk1.dependency-license-report' + } + + repositories { + mavenCentral() + } + + apply plugin: 'java' + + dependencies { + compile "junit:junit:\${project.ext.junitVersion}" + } + """ + + when: + BuildResult result = GradleRunner.create() + .withPluginClasspath() + .withProjectDir(testProjectDir.getRoot()) + .withArguments('--build-cache', "generateLicenseReport", "-PjunitVersion=4.12") + .build() + + then: + result.task(':generateLicenseReport').outcome == TaskOutcome.SUCCESS + + when: + result = GradleRunner.create() + .withPluginClasspath() + .withProjectDir(testProjectDir.getRoot()) + .withArguments('--build-cache', "generateLicenseReport", "-PjunitVersion=4.12") + .build() + + then: + result.task(':generateLicenseReport').outcome == TaskOutcome.UP_TO_DATE + + when: + result = GradleRunner.create() + .withPluginClasspath() + .withProjectDir(testProjectDir.getRoot()) + .withArguments('--build-cache', "clean", "generateLicenseReport", "-PjunitVersion=4.12") + .build() + + then: + result.task(':generateLicenseReport').outcome == TaskOutcome.FROM_CACHE + + when: + result = GradleRunner.create() + .withPluginClasspath() + .withProjectDir(testProjectDir.getRoot()) + .withArguments('--build-cache', "generateLicenseReport", "-PjunitVersion=4.11") + .build() + + then: + result.task(':generateLicenseReport').outcome == TaskOutcome.SUCCESS + + } +}