diff --git a/oss-licenses-plugin/gradle/wrapper/gradle-wrapper.jar b/oss-licenses-plugin/gradle/wrapper/gradle-wrapper.jar index 1b33c55..d997cfc 100644 Binary files a/oss-licenses-plugin/gradle/wrapper/gradle-wrapper.jar and b/oss-licenses-plugin/gradle/wrapper/gradle-wrapper.jar differ diff --git a/oss-licenses-plugin/gradle/wrapper/gradle-wrapper.properties b/oss-licenses-plugin/gradle/wrapper/gradle-wrapper.properties index 37f78a6..dbc3ce4 100644 --- a/oss-licenses-plugin/gradle/wrapper/gradle-wrapper.properties +++ b/oss-licenses-plugin/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.0-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/oss-licenses-plugin/gradlew b/oss-licenses-plugin/gradlew index 23d15a9..0262dcb 100755 --- a/oss-licenses-plugin/gradlew +++ b/oss-licenses-plugin/gradlew @@ -1,7 +1,7 @@ #!/bin/sh # -# Copyright © 2015-2021 the original authors. +# Copyright © 2015 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -57,7 +57,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/b631911858264c0b6e4d6603d677ff5218766cee/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -114,7 +114,6 @@ case "$( uname )" in #( NONSTOP* ) nonstop=true ;; esac -CLASSPATH="\\\"\\\"" # Determine the Java command to use to start the JVM. @@ -172,7 +171,6 @@ fi # For Cygwin or MSYS, switch paths to Windows format before running java if "$cygwin" || "$msys" ; then APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) - CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) JAVACMD=$( cygpath --unix "$JAVACMD" ) @@ -212,7 +210,6 @@ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ - -classpath "$CLASSPATH" \ -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ "$@" diff --git a/oss-licenses-plugin/gradlew.bat b/oss-licenses-plugin/gradlew.bat index 5eed7ee..e509b2d 100644 --- a/oss-licenses-plugin/gradlew.bat +++ b/oss-licenses-plugin/gradlew.bat @@ -70,11 +70,10 @@ goto fail :execute @rem Setup the command line -set CLASSPATH= @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* :end @rem End local scope for the variables with windows NT shell diff --git a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/DependencyUtil.groovy b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/DependencyUtil.groovy index 0808b0b..fb7006d 100644 --- a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/DependencyUtil.groovy +++ b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/DependencyUtil.groovy @@ -20,7 +20,6 @@ import org.gradle.api.Project import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.component.ModuleComponentIdentifier import org.gradle.api.artifacts.result.ResolvedArtifactResult -import org.gradle.api.provider.Provider import org.gradle.maven.MavenModule import org.gradle.maven.MavenPomArtifact @@ -46,54 +45,52 @@ class DependencyUtil { * * @param project The Gradle project used to create the resolution query. * @param runtimeConfiguration The configuration whose dependencies should be resolved. - * @return A provider for a map of GAV coordinates to their resolved ArtifactFiles. + * @return A map of GAV coordinates to their resolved ArtifactFiles. */ - static Provider> resolveArtifacts(Project project, Configuration runtimeConfiguration) { - return project.provider { - // We create an ArtifactView to gather the component identifiers and library files. - // We specifically target external Maven dependencies (ModuleComponentIdentifiers). - def runtimeArtifactView = runtimeConfiguration.incoming.artifactView { - it.componentFilter { id -> id instanceof ModuleComponentIdentifier } - } - - def artifactsMap = [:] - - // 1. Gather library files directly from the view - runtimeArtifactView.artifacts.each { artifact -> - def id = artifact.id.componentIdentifier - if (id instanceof ModuleComponentIdentifier) { - String key = "${id.group}:${id.module}:${id.version}".toString() - artifactsMap[key] = new ArtifactFiles(null, artifact.file) - } + static Map resolveArtifacts(Project project, Configuration runtimeConfiguration) { + // We create an ArtifactView to gather the component identifiers and library files. + // We specifically target external Maven dependencies (ModuleComponentIdentifiers). + def runtimeArtifactView = runtimeConfiguration.incoming.artifactView { + it.componentFilter { id -> id instanceof ModuleComponentIdentifier } + } + + def artifactsMap = [:] + + // 1. Gather library files directly from the view + runtimeArtifactView.artifacts.each { artifact -> + def id = artifact.id.componentIdentifier + if (id instanceof ModuleComponentIdentifier) { + String key = "${id.group}:${id.module}:${id.version}".toString() + artifactsMap[key] = new ArtifactFiles(null, artifact.file) } + } - // 2. Fetch corresponding POM files using ArtifactResolutionQuery - def componentIds = runtimeArtifactView.artifacts.collect { it.id.componentIdentifier } - - if (!componentIds.isEmpty()) { - def result = project.dependencies.createArtifactResolutionQuery() - .forComponents(componentIds) - .withArtifacts(MavenModule, MavenPomArtifact) - .execute() + // 2. Fetch corresponding POM files using ArtifactResolutionQuery + def componentIds = runtimeArtifactView.artifacts.collect { it.id.componentIdentifier } + + if (!componentIds.isEmpty()) { + def result = project.dependencies.createArtifactResolutionQuery() + .forComponents(componentIds) + .withArtifacts(MavenModule, MavenPomArtifact) + .execute() - result.resolvedComponents.each { component -> - component.getArtifacts(MavenPomArtifact).each { artifact -> - if (artifact instanceof ResolvedArtifactResult) { - def id = component.id - String key = "${id.group}:${id.module}:${id.version}".toString() - - // Update the existing entry with the POM file - if (artifactsMap.containsKey(key)) { - artifactsMap[key].pomFile = artifact.file - } else { - artifactsMap[key] = new ArtifactFiles(artifact.file, null) - } + result.resolvedComponents.each { component -> + component.getArtifacts(MavenPomArtifact).each { artifact -> + if (artifact instanceof ResolvedArtifactResult) { + def id = component.id + String key = "${id.group}:${id.module}:${id.version}".toString() + + // Update the existing entry with the POM file + if (artifactsMap.containsKey(key)) { + artifactsMap[key].pomFile = artifact.file + } else { + artifactsMap[key] = new ArtifactFiles(artifact.file, null) } } } } - - return artifactsMap } + + return artifactsMap } } diff --git a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/OssLicensesPlugin.groovy b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/OssLicensesPlugin.groovy index 77e0c3a..df70fe9 100644 --- a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/OssLicensesPlugin.groovy +++ b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/OssLicensesPlugin.groovy @@ -58,10 +58,11 @@ class OssLicensesPlugin implements Plugin { // Task 1: Dependency Identification // This task reads the AGP METADATA_LIBRARY_DEPENDENCIES_REPORT protobuf. + def dependenciesJson = baseDir.map { it.file("dependencies.json") } TaskProvider dependencyTask = project.tasks.register( "${variant.name}OssDependencyTask", DependencyTask.class) { - it.dependenciesJson.set(baseDir.map { it.file("dependencies.json") }) + it.dependenciesJson.set(dependenciesJson) it.libraryDependenciesReport.set(variant.artifacts.get(SingleArtifact.METADATA_LIBRARY_DEPENDENCIES_REPORT.INSTANCE)) } project.logger.debug("Registered task ${dependencyTask.name}") @@ -72,12 +73,28 @@ class OssLicensesPlugin implements Plugin { "${variant.name}OssLicensesTask", LicensesTask.class) { it.dependenciesJson.set(dependencyTask.flatMap { it.dependenciesJson }) - it.artifactFiles.set(DependencyUtil.resolveArtifacts(project, variant.runtimeConfiguration)) + + it.artifactFiles.set(project.provider { + DependencyUtil.resolveArtifacts(project, variant.runtimeConfiguration) + }) } project.logger.debug("Registered task ${licenseTask.name}") // Register the LicensesTask output as a generated resource folder for AGP. variant.sources.res.addGeneratedSourceDirectory(licenseTask, LicensesTask::getGeneratedDirectory) + + // Task 3: Cleanup + // Ensures generated license files are deleted when running the clean task. + TaskProvider cleanupTask = project.tasks.register( + "${variant.name}OssLicensesCleanUp", + LicensesCleanUpTask.class) { + it.generatedDirectory.set(baseDir) + } + project.logger.debug("Registered task ${cleanupTask.name}") + + project.tasks.named("clean").configure { + it.dependsOn(cleanupTask) + } } } diff --git a/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/DependencyResolutionTest.java b/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/DependencyResolutionTest.java index d79def4..220eaab 100644 --- a/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/DependencyResolutionTest.java +++ b/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/DependencyResolutionTest.java @@ -92,7 +92,7 @@ public void testComplexDependencyGraphResolution() throws IOException { runtimeClasspath.resolve(); // Execute resolution logic - Map artifactFiles = DependencyUtil.resolveArtifacts(appProject, runtimeClasspath).get(); + Map artifactFiles = DependencyUtil.resolveArtifacts(appProject, runtimeClasspath); // Assertions // - Guava resolved to the higher version @@ -121,7 +121,7 @@ public void testPomResolution() throws IOException { Configuration runtimeClasspath = appProject.getConfigurations().getByName("runtimeClasspath"); runtimeClasspath.resolve(); - Map artifactFiles = DependencyUtil.resolveArtifacts(appProject, runtimeClasspath).get(); + Map artifactFiles = DependencyUtil.resolveArtifacts(appProject, runtimeClasspath); assertThat(artifactFiles).containsKey("com.google.guava:guava:33.0.0-jre"); assertThat(artifactFiles.get("com.google.guava:guava:33.0.0-jre").getPomFile()).isNotNull();