Skip to content

Commit d383c50

Browse files
authored
Merge pull request #283 from CycloneDX/tool-goal
add effective goal into BOM tool name
2 parents 1039975 + 6132313 commit d383c50

File tree

7 files changed

+43
-31
lines changed

7 files changed

+43
-31
lines changed

src/it/makeAggregateBom/verify.groovy

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
void assertBomFiles(String path) {
1+
void assertBomFiles(String path, boolean aggregate) {
22
File bomFileXml = new File(basedir, path + ".xml")
33
File bomFileJson = new File(basedir, path + ".json")
44

55
assert bomFileXml.exists()
66
assert bomFileJson.exists()
7+
8+
String analysis = aggregate ? "makeAggregateBom" : "makeBom"
9+
assert bomFileXml.text.contains('<name>CycloneDX Maven plugin ' + analysis + '</name>')
10+
assert bomFileJson.text.contains('"name" : "CycloneDX Maven plugin ' + analysis + '"')
711
}
812

9-
assertBomFiles("target/bom") // aggregate
10-
assertBomFiles("api/target/bom")
11-
assertBomFiles("util/target/bom")
12-
assertBomFiles("impls/target/bom")
13-
assertBomFiles("impls/impl-A/target/bom")
14-
assertBomFiles("impls/impl-B/target/bom")
13+
assertBomFiles("target/bom", true) // aggregate
14+
assertBomFiles("api/target/bom", false)
15+
assertBomFiles("util/target/bom", false)
16+
assertBomFiles("impls/target/bom", false)
17+
assertBomFiles("impls/impl-A/target/bom", false)
18+
assertBomFiles("impls/impl-B/target/bom", false)
1519

1620
var buildLog = new File(basedir, "build.log").text
1721

src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,15 @@ protected Component convert(Artifact artifact) {
246246
return modelConverter.convert(artifact, schemaVersion(), includeLicenseText);
247247
}
248248

249-
protected abstract boolean analyze(Set<Component> components, Set<Dependency> dependencies) throws MojoExecutionException;
249+
/**
250+
* Analyze the project dependencies to fill the BOM components list and their dependencies.
251+
*
252+
* @param components the components set to fill
253+
* @param dependencies the dependencies set to fill
254+
* @return the name of the analysis done to store as a BOM, or {@code null} to not save result.
255+
* @throws MojoExecutionException something weird happened...
256+
*/
257+
protected abstract String analyze(Set<Component> components, Set<Dependency> dependencies) throws MojoExecutionException;
250258

251259
public void execute() throws MojoExecutionException {
252260
final boolean shouldSkip = Boolean.parseBoolean(System.getProperty("cyclonedx.skip", Boolean.toString(skip)));
@@ -259,20 +267,21 @@ public void execute() throws MojoExecutionException {
259267
final Set<Component> components = new LinkedHashSet<>();
260268
final Set<Dependency> dependencies = new LinkedHashSet<>();
261269

262-
if (analyze(components, dependencies)) {
263-
generateBom(components, dependencies);
270+
String analysis = analyze(components, dependencies);
271+
if (analysis != null) {
272+
generateBom(analysis, components, dependencies);
264273
}
265274
}
266275

267-
private void generateBom(Set<Component> components, Set<Dependency> dependencies) throws MojoExecutionException {
276+
private void generateBom(String analysis, Set<Component> components, Set<Dependency> dependencies) throws MojoExecutionException {
268277
try {
269278
getLog().info(MESSAGE_CREATING_BOM);
270279
final Bom bom = new Bom();
271280
if (schemaVersion().getVersion() >= 1.1 && includeBomSerialNumber) {
272281
bom.setSerialNumber("urn:uuid:" + UUID.randomUUID());
273282
}
274283
if (schemaVersion().getVersion() >= 1.2) {
275-
final Metadata metadata = modelConverter.convert(project, projectType, schemaVersion(), includeLicenseText);
284+
final Metadata metadata = modelConverter.convert(project, analysis, projectType, schemaVersion(), includeLicenseText);
276285
bom.setMetadata(metadata);
277286
}
278287
bom.setComponents(new ArrayList<>(components));

src/main/java/org/cyclonedx/maven/CycloneDxAggregateMojo.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.maven.plugins.annotations.ResolutionScope;
2727
import org.apache.maven.project.MavenProject;
2828
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
29+
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzerException;
2930
import org.cyclonedx.model.Component;
3031
import org.cyclonedx.model.Dependency;
3132

@@ -105,14 +106,14 @@ protected void logAdditionalParameters() {
105106
getLog().info("outputReactorProjects : " + outputReactorProjects);
106107
}
107108

108-
protected boolean analyze(final Set<Component> components, final Set<Dependency> dependencies) throws MojoExecutionException {
109+
protected String analyze(final Set<Component> components, final Set<Dependency> dependencies) throws MojoExecutionException {
109110
if (! getProject().isExecutionRoot()) {
110111
// non-root project: let parent class create a module-only BOM?
111112
if (outputReactorProjects) {
112113
return super.analyze(components, dependencies);
113114
}
114115
getLog().info("Skipping CycloneDX on non-execution root");
115-
return false;
116+
return null;
116117
}
117118

118119
// root project: analyze and aggregate all the modules
@@ -131,8 +132,8 @@ protected boolean analyze(final Set<Component> components, final Set<Dependency>
131132
try {
132133
ProjectDependencyAnalysis dependencyAnalysis = dependencyAnalyzer.analyze(mavenProject);
133134
dependencyAnalysisMap.put(mavenProject.getArtifactId(), dependencyAnalysis);
134-
} catch (Exception e) {
135-
getLog().debug(e);
135+
} catch (ProjectDependencyAnalyzerException pdae) {
136+
getLog().debug("Could not analyze " + mavenProject.getId(), pdae); // TODO should warn...
136137
}
137138
}
138139

@@ -192,7 +193,7 @@ protected boolean analyze(final Set<Component> components, final Set<Dependency>
192193
}
193194
}
194195
addMavenProjectsAsDependencies(reactorProjects, dependencies);
195-
return true;
196+
return "makeAggregateBom";
196197
}
197198

198199
private void addMavenProjectsAsDependencies(List<MavenProject> reactorProjects, Set<Dependency> dependencies) {

src/main/java/org/cyclonedx/maven/CycloneDxMojo.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
2828
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzer;
2929
import org.codehaus.plexus.PlexusContainer;
30+
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
3031
import org.cyclonedx.model.Component;
3132
import org.cyclonedx.model.Dependency;
3233
import java.util.LinkedHashSet;
@@ -69,18 +70,14 @@ public class CycloneDxMojo extends BaseCycloneDxMojo {
6970
* @throws MojoExecutionException in case of an error.
7071
*/
7172
protected ProjectDependencyAnalyzer createProjectDependencyAnalyzer() throws MojoExecutionException {
72-
final String role = ProjectDependencyAnalyzer.class.getName();
73-
final String roleHint = analyzer;
7473
try {
75-
return (ProjectDependencyAnalyzer) plexusContainer.lookup(role, roleHint);
76-
}
77-
catch (Exception exception) {
78-
throw new MojoExecutionException("Failed to instantiate ProjectDependencyAnalyser with role " + role
79-
+ " / role-hint " + roleHint, exception);
74+
return (ProjectDependencyAnalyzer) plexusContainer.lookup(ProjectDependencyAnalyzer.class, analyzer);
75+
} catch (ComponentLookupException cle) {
76+
throw new MojoExecutionException("Failed to instantiate ProjectDependencyAnalyser with role-hint " + analyzer, cle);
8077
}
8178
}
8279

83-
protected boolean analyze(final Set<Component> components, final Set<Dependency> dependencies) throws MojoExecutionException {
80+
protected String analyze(final Set<Component> components, final Set<Dependency> dependencies) throws MojoExecutionException {
8481
final Set<String> componentRefs = new LinkedHashSet<>();
8582
// Use default dependency analyzer
8683
dependencyAnalyzer = createProjectDependencyAnalyzer();
@@ -111,7 +108,7 @@ protected boolean analyze(final Set<Component> components, final Set<Dependency>
111108
if (schemaVersion().getVersion() >= 1.2) {
112109
dependencies.addAll(buildDependencyGraph(null));
113110
}
114-
return true;
111+
return "makeBom";
115112
}
116113

117114
/**

src/main/java/org/cyclonedx/maven/CycloneDxPackageMojo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected boolean shouldInclude(MavenProject mavenProject) {
5656
return Arrays.asList(new String[]{"war", "ear"}).contains(mavenProject.getPackaging());
5757
}
5858

59-
protected boolean analyze(Set<Component> components, Set<Dependency> dependencies) throws MojoExecutionException {
59+
protected String analyze(Set<Component> components, Set<Dependency> dependencies) throws MojoExecutionException {
6060
final Set<String> componentRefs = new LinkedHashSet<>();
6161
getLog().info(MESSAGE_RESOLVING_DEPS);
6262

@@ -77,6 +77,6 @@ protected boolean analyze(Set<Component> components, Set<Dependency> dependencie
7777
dependencies.addAll(buildDependencyGraph(mavenProject));
7878
}
7979
}
80-
return true;
80+
return "makePackageBom";
8181
}
8282
}

src/main/java/org/cyclonedx/maven/DefaultModelConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,11 @@ else if (licenseChoiceToResolve.getExpression() != null && CycloneDxSchema.Versi
315315
return false;
316316
}
317317

318-
public Metadata convert(final MavenProject project, String projectType, CycloneDxSchema.Version schemaVersion, boolean includeLicenseText) {
318+
public Metadata convert(final MavenProject project, String analysis, String projectType, CycloneDxSchema.Version schemaVersion, boolean includeLicenseText) {
319319
final Tool tool = new Tool();
320320
final Properties properties = readPluginProperties();
321321
tool.setVendor(properties.getProperty("vendor"));
322-
tool.setName(properties.getProperty("name"));
322+
tool.setName(properties.getProperty("name") + ' ' + analysis);
323323
tool.setVersion(properties.getProperty("version"));
324324
// Attempt to add hash values from the current mojo
325325
final Artifact self = new DefaultArtifact(properties.getProperty("groupId"), properties.getProperty("artifactId"),

src/main/java/org/cyclonedx/maven/ModelConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ public interface ModelConverter {
4848
* Converts a MavenProject into a Metadata object.
4949
*
5050
* @param project the MavenProject to convert
51+
* @param analysis type of analysis
5152
* @param projectType the target CycloneDX component type
5253
* @param schemaVersion the target CycloneDX schema version
5354
* @param includeLicenseText should license text be included in bom?
5455
* @return a CycloneDX Metadata object
5556
*/
56-
Metadata convert(MavenProject project, String projectType, CycloneDxSchema.Version schemaVersion, boolean includeLicenseText);
57+
Metadata convert(MavenProject project, String analysis, String projectType, CycloneDxSchema.Version schemaVersion, boolean includeLicenseText);
5758
}

0 commit comments

Comments
 (0)