-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Fix dependency reporting #136209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix dependency reporting #136209
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.gradle.internal; | ||
|
||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.InvalidUserDataException; | ||
import org.gradle.api.provider.MapProperty; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.Optional; | ||
|
||
import java.util.Map; | ||
|
||
public abstract class AbstractDependenciesTask extends DefaultTask { | ||
|
||
@Input | ||
@Optional | ||
public abstract MapProperty<String, String> getMappings(); | ||
|
||
/** | ||
* Add a mapping from a regex pattern for the jar name, to a prefix to find | ||
* the LICENSE and NOTICE file for that jar. | ||
*/ | ||
public void mapping(Map<String, String> props) { | ||
String from = props.get("from"); | ||
if (from == null) { | ||
throw new InvalidUserDataException("Missing \"from\" setting for license name mapping"); | ||
} | ||
String to = props.get("to"); | ||
if (to == null) { | ||
throw new InvalidUserDataException("Missing \"to\" setting for license name mapping"); | ||
} | ||
if (props.size() > 2) { | ||
throw new InvalidUserDataException("Unknown properties for mapping on dependencyLicenses: " + props.keySet()); | ||
} | ||
getMappings().put(from, to); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,19 +17,21 @@ | |
import org.gradle.api.file.ConfigurableFileCollection; | ||
import org.gradle.api.file.DirectoryProperty; | ||
import org.gradle.api.file.ProjectLayout; | ||
import org.gradle.api.internal.ConventionTask; | ||
import org.gradle.api.model.ObjectFactory; | ||
import org.gradle.api.provider.MapProperty; | ||
import org.gradle.api.provider.Property; | ||
import org.gradle.api.provider.Provider; | ||
import org.gradle.api.provider.ProviderFactory; | ||
import org.gradle.api.tasks.CacheableTask; | ||
import org.gradle.api.tasks.Classpath; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.InputDirectory; | ||
import org.gradle.api.tasks.InputFiles; | ||
import org.gradle.api.tasks.Internal; | ||
import org.gradle.api.tasks.Optional; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.PathSensitive; | ||
import org.gradle.api.tasks.PathSensitivity; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.gradle.internal.component.external.model.ModuleComponentArtifactIdentifier; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
@@ -55,7 +57,8 @@ | |
* <li>license: <a href="https://spdx.org/licenses/">SPDX license</a> identifier, custom license or UNKNOWN.</li> | ||
* </ul> | ||
*/ | ||
public abstract class DependenciesInfoTask extends ConventionTask { | ||
@CacheableTask | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we removed the convention mapping we can make that task cacheable |
||
public abstract class DependenciesInfoTask extends AbstractDependenciesTask { | ||
|
||
@Inject | ||
public abstract ProviderFactory getProviderFactory(); | ||
|
@@ -86,16 +89,17 @@ public Provider<Set<ModuleComponentIdentifier>> getCompileOnlyModules() { | |
* artifact transforms that might be applied and fail due to missing task dependency to jar | ||
* generating tasks. | ||
* */ | ||
@InputFiles | ||
@Classpath | ||
abstract ConfigurableFileCollection getClasspath(); | ||
|
||
private Provider<Set<ModuleComponentIdentifier>> mapToModuleComponentIdentifiers(ArtifactCollection artifacts) { | ||
return getProviderFactory().provider( | ||
() -> artifacts.getArtifacts() | ||
.stream() | ||
.map(r -> r.getId()) | ||
.filter(id -> id instanceof ModuleComponentIdentifier) | ||
.map(id -> (ModuleComponentIdentifier) id) | ||
.filter(mcaId -> mcaId instanceof ModuleComponentArtifactIdentifier) | ||
.map(mcaId -> (ModuleComponentArtifactIdentifier) mcaId) | ||
.map(it -> it.getComponentIdentifier()) | ||
Comment on lines
+101
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I understand correctly this part was the root cause of missing dependencies in the report? Did Gradle change something in the identifiers hierarchy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exactly |
||
.collect(Collectors.toSet()) | ||
); | ||
} | ||
|
@@ -111,6 +115,7 @@ private Provider<Set<ModuleComponentIdentifier>> mapToModuleComponentIdentifiers | |
* Directory to read license files | ||
*/ | ||
@Optional | ||
@PathSensitive(PathSensitivity.RELATIVE) | ||
@InputDirectory | ||
public File getLicensesDir() { | ||
File asFile = licensesDir.get().getAsFile(); | ||
|
@@ -143,7 +148,6 @@ public DependenciesInfoTask(ProjectLayout projectLayout, ObjectFactory objectFac | |
|
||
@TaskAction | ||
public void generateDependenciesInfo() throws IOException { | ||
|
||
final Set<String> compileOnlyIds = getCompileOnlyModules().map( | ||
set -> set.stream() | ||
.map(id -> id.getModuleIdentifier().getGroup() + ":" + id.getModuleIdentifier().getName() + ":" + id.getVersion()) | ||
|
@@ -166,18 +170,13 @@ public void generateDependenciesInfo() throws IOException { | |
final String url = createURL(dep.getGroup(), moduleName, dep.getVersion()); | ||
final String dependencyName = DependencyLicensesTask.getDependencyName(mappings, moduleName); | ||
getLogger().info("mapped dependency " + dep.getGroup() + ":" + moduleName + " to " + dependencyName + " for license info"); | ||
|
||
final String licenseType = getLicenseType(dep.getGroup(), dependencyName); | ||
output.append(dep.getGroup() + ":" + moduleName + "," + dep.getVersion() + "," + url + "," + licenseType + "\n"); | ||
} | ||
|
||
Files.writeString(outputFile.toPath(), output.toString(), StandardOpenOption.CREATE); | ||
} | ||
|
||
@Input | ||
@Optional | ||
public abstract MapProperty<String, String> getMappings(); | ||
|
||
/** | ||
* Create an URL on <a href="https://repo1.maven.org/maven2/">Maven Central</a> | ||
* based on dependency coordinates. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,11 @@ tasks.register("generateDependenciesReport", ConcatFilesTask) { | |
'https://oss-dependencies.elastic.co/red-hat-universal-base-image-minimal/9/ubi-minimal-9-source.tar.gz' | ||
] | ||
additionalLines << rhelUbiFields.join(',') | ||
doLast { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just another safety net to capture suspicious change in behaviour |
||
if(target.text.readLines().size() < 100) { | ||
throw new GradleException("Suspiciously low number of dependencies. Double check.") | ||
} | ||
} | ||
} | ||
|
||
/***************************************************************************** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only care about the direct dependencies here