Skip to content

Commit

Permalink
Merge pull request #340 from CycloneDX/properties
Browse files Browse the repository at this point in the history
use metadata properties instead of tool name
  • Loading branch information
hboutemy authored Apr 16, 2023
2 parents 8d4be59 + 30d6af9 commit 6574473
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
10 changes: 7 additions & 3 deletions src/it/makeAggregateBom/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ void assertBomFiles(String path, boolean aggregate) {
assert bomFileXml.exists()
assert bomFileJson.exists()

String analysis = (aggregate ? "makeAggregateBom" : "makeBom") + ' compile+provided+runtime+system'
assert bomFileXml.text.contains('<name>CycloneDX Maven plugin ' + analysis + '</name>')
assert bomFileJson.text.contains('"name" : "CycloneDX Maven plugin ' + analysis + '"')
String analysis = aggregate ? "makeAggregateBom" : "makeBom"
assert bomFileXml.text.contains('<property name="maven.goal">' + analysis + '</property>')
assert bomFileXml.text.contains('<property name="maven.scopes">compile,provided,runtime,system</property>')
assert bomFileJson.text.contains('"name" : "maven.goal",')
assert bomFileJson.text.contains('"value" : "' + analysis + '"')
assert bomFileJson.text.contains('"name" : "maven.scopes",')
assert bomFileJson.text.contains('"value" : "compile,provided,runtime,system"')
}

assertBomFiles("target/bom", true) // aggregate
Expand Down
29 changes: 21 additions & 8 deletions src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.cyclonedx.model.Component;
import org.cyclonedx.model.Dependency;
import org.cyclonedx.model.Metadata;
import org.cyclonedx.model.Property;
import org.cyclonedx.parsers.JsonParser;
import org.cyclonedx.parsers.Parser;
import org.cyclonedx.parsers.XmlParser;
Expand Down Expand Up @@ -256,14 +257,19 @@ public void execute() throws MojoExecutionException {

String analysis = extractComponentsAndDependencies(topLevelComponents, componentMap, dependencyMap);
if (analysis != null) {
List<String> scopes = new ArrayList<>();
if (includeCompileScope) scopes.add("compile");
if (includeProvidedScope) scopes.add("provided");
if (includeRuntimeScope) scopes.add("runtime");
if (includeSystemScope) scopes.add("system");
if (includeTestScope) scopes.add("test");

final Metadata metadata = modelConverter.convert(project, analysis + " " + String.join("+", scopes), projectType, schemaVersion(), includeLicenseText);
final Metadata metadata = modelConverter.convert(project, projectType, schemaVersion(), includeLicenseText);

if (schemaVersion().getVersion() >= 1.3) {
metadata.addProperty(newProperty("maven.goal", analysis));

List<String> scopes = new ArrayList<>();
if (includeCompileScope) scopes.add("compile");
if (includeProvidedScope) scopes.add("provided");
if (includeRuntimeScope) scopes.add("runtime");
if (includeSystemScope) scopes.add("system");
if (includeTestScope) scopes.add("test");
metadata.addProperty(newProperty("maven.scopes", String.join(",", scopes)));
}

final Component rootComponent = metadata.getComponent();
componentMap.remove(rootComponent.getPurl());
Expand All @@ -274,6 +280,13 @@ public void execute() throws MojoExecutionException {
}
}

private Property newProperty(String name, String value) {
Property property = new Property();
property.setName(name);
property.setValue(value);
return property;
}

private void generateBom(String analysis, Metadata metadata, List<Component> components, List<Dependency> dependencies) throws MojoExecutionException {
try {
getLog().info(String.format(MESSAGE_CREATING_BOM, schemaVersion, components.size()));
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/cyclonedx/maven/DefaultModelConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,12 @@ else if (licenseChoiceToResolve.getExpression() != null && CycloneDxSchema.Versi
return false;
}

public Metadata convert(final MavenProject project, String analysis, String projectType, CycloneDxSchema.Version schemaVersion, boolean includeLicenseText) {
@Override
public Metadata convert(final MavenProject project, String projectType, CycloneDxSchema.Version schemaVersion, boolean includeLicenseText) {
final Tool tool = new Tool();
final Properties properties = readPluginProperties();
tool.setVendor(properties.getProperty("vendor"));
tool.setName(properties.getProperty("name") + ' ' + analysis);
tool.setName(properties.getProperty("name"));
tool.setVersion(properties.getProperty("version"));
// Attempt to add hash values from the current mojo
final Artifact self = new DefaultArtifact(properties.getProperty("groupId"), properties.getProperty("artifactId"),
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/cyclonedx/maven/ModelConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ public interface ModelConverter {
* Converts a MavenProject into a Metadata object.
*
* @param project the MavenProject to convert
* @param analysis type of analysis
* @param projectType the target CycloneDX component type
* @param schemaVersion the target CycloneDX schema version
* @param includeLicenseText should license text be included in bom?
* @return a CycloneDX Metadata object
*/
Metadata convert(MavenProject project, String analysis, String projectType, CycloneDxSchema.Version schemaVersion, boolean includeLicenseText);
Metadata convert(MavenProject project, String projectType, CycloneDxSchema.Version schemaVersion, boolean includeLicenseText);
}
1 change: 0 additions & 1 deletion src/test/java/org/cyclonedx/maven/CyclicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down

0 comments on commit 6574473

Please sign in to comment.