Skip to content

Commit

Permalink
Re-run the license task on the project configurations/dependencies ch…
Browse files Browse the repository at this point in the history
…anges (#213)

The idea is simple - as the license task depends on the Gradle build script itself
let's mark the build script as the task input.

A slight drawback of this approach is that the license task will re-run after every
build script change, i.e. it will be UP-TO-DATE less often. But changing the build
script should not happen that often compared to the code changes and Gradle would
need to re-configure the project anyway during the next build so re-running the license
task should be acceptable.

An alternative approach could be to declare *all* project properties that affect
the task outcome as inputs to the task.

Closes #212.
  • Loading branch information
sergeys-opera authored May 16, 2022
1 parent aa6dd58 commit cd18592
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import java.io.File
Expand All @@ -30,6 +31,10 @@ internal open class LicenseReportTask : BaseLicenseReportTask() { // tasks can't

@Input var assetDirs = emptyList<File>()
@Optional @Input var variantName: String? = null
// This input is used by the task indirectly via some project properties (such as "configurations" and "dependencies")
// that affect the task's outcome. When the mentioned project properties change the task should re-run the next time
// it is requested and should *not* be marked as UP-TO-DATE.
@InputFile var buildFile: File? = null

private val projects = mutableListOf<Model>()
private var pomConfiguration = "poms"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private fun Project.configureVariant(
.srcDirs
.toList()
it.variantName = variant.name
it.buildFile = buildFile
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ internal fun Project.isJavaProject(): Boolean {
* All of these plugins will apply the JavaPlugin(relies on JavaBasePlugin) or JavaPlatformPlugin.
*/
internal fun Project.configureJavaProject() {
tasks.register("licenseReport", LicenseReportTask::class.java)
tasks.register("licenseReport", LicenseReportTask::class.java) {
it.buildFile = buildFile
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,81 @@ final class LicensePluginAndroidSpec extends Specification {
taskName << ['licenseDebugReport', 'licenseReleaseReport']
}

@Unroll def 'not UP-TO-DATE when dependencies change'() {
given:
def originalBuildFile = """
buildscript {
dependencies {
classpath files($classpathString)
}
}
repositories {
maven {
url '${mavenRepoUrl}'
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.jaredsburrows.license'
android {
compileSdkVersion $compileSdkVersion
defaultConfig {
applicationId 'com.example'
}
}
dependencies {
implementation 'com.android.support:appcompat-v7:26.1.0'
}
"""
def modifiedBuildFile = """
buildscript {
dependencies {
classpath files($classpathString)
}
}
repositories {
maven {
url '${mavenRepoUrl}'
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.jaredsburrows.license'
android {
compileSdkVersion $compileSdkVersion
defaultConfig {
applicationId 'com.example'
}
}
dependencies {
implementation 'com.android.support:appcompat-v7:26.1.0'
// This is a new dependency
implementation 'com.android.support:design:26.1.0'
}
"""

when:
buildFile << originalBuildFile
def result1 = gradleWithCommand(testProjectDir.root, "${taskName}", '-s')
buildFile << modifiedBuildFile
def result2 = gradleWithCommand(testProjectDir.root, "${taskName}", '-s')

then:
result1.task(":${taskName}").outcome == SUCCESS
result2.task(":${taskName}").outcome == SUCCESS

where:
taskName << ['licenseDebugReport', 'licenseReleaseReport']
}

@Unroll def '#taskName with buildTypes'() {
given:
buildFile <<
Expand Down

0 comments on commit cd18592

Please sign in to comment.