Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
Signed-off-by: Hervé Boutemy <[email protected]>
  • Loading branch information
hboutemy committed Nov 11, 2024
1 parent 4450f3b commit 6429094
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 112 deletions.
13 changes: 5 additions & 8 deletions src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
import java.util.UUID;

public abstract class BaseCycloneDxMojo extends AbstractMojo {
static final String CYCLONEDX_PLUGIN_KEY = "org.cyclonedx:cyclonedx-maven-plugin";
static final String PROJECT_TYPE = "projectType";

@Parameter(property = "project", readonly = true, required = true)
private MavenProject project;
Expand All @@ -72,7 +74,7 @@ public abstract class BaseCycloneDxMojo extends AbstractMojo {
*
* @since 2.0.0
*/
@Parameter(property = "projectType", defaultValue = "library", required = false)
@Parameter(property = PROJECT_TYPE, defaultValue = "library", required = false)
private String projectType;

/**
Expand Down Expand Up @@ -622,13 +624,8 @@ private static boolean isDeployable(final MavenProject project,
for (final PluginExecution execution : plugin.getExecutions()) {
if (execution.getGoals().contains("deploy")) {
final Xpp3Dom executionConf = (Xpp3Dom) execution.getConfiguration();
boolean skipValue = defaultSkipValue;
if (executionConf != null) {
Xpp3Dom target = executionConf.getChild(parameter);
if (target != null) {
skipValue = Boolean.parseBoolean(target.getValue());
}
}
final Xpp3Dom target = (executionConf == null) ? null : executionConf.getChild(parameter);
final boolean skipValue = (target == null) ? defaultSkipValue : Boolean.parseBoolean(target.getValue());
if (!skipValue) {
return true;
}
Expand Down
33 changes: 9 additions & 24 deletions src/main/java/org/cyclonedx/maven/DefaultModelConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,10 @@
import org.apache.maven.model.Plugin;
import org.codehaus.plexus.util.xml.Xpp3Dom;

import java.util.Optional;

@Singleton
@Named
public class DefaultModelConverter implements ModelConverter {
private final Logger logger = LoggerFactory.getLogger(DefaultModelConverter.class);
private static final String CYCLONEDX_GROUP_ID = "org.cyclonedx";
private static final String CYCLONEDX_ARTIFACT_ID = "cyclonedx-maven-plugin";
private static final String PROJECT_TYPE_NODE = "projectType";

@Inject
private MavenSession session;
Expand Down Expand Up @@ -167,8 +162,8 @@ public Component convertMavenDependency(Artifact artifact, Version schemaVersion
final Component component = new Component();
component.setGroup(artifact.getGroupId());
component.setName(artifact.getArtifactId());
component.setVersion(artifact.getBaseVersion());
component.setType(Component.Type.LIBRARY);
component.setVersion(artifact.getBaseVersion());
component.setType(Component.Type.LIBRARY);

try {
logger.debug(BaseCycloneDxMojo.MESSAGE_CALCULATING_HASHES);
Expand All @@ -187,7 +182,7 @@ public Component convertMavenDependency(Artifact artifact, Version schemaVersion
final MavenProject project = getEffectiveMavenProject(artifact);

if (project != null) {
String projectType = getProjectTypeFromPluginConfiguration(project);
String projectType = getPluginConfiguration(project, BaseCycloneDxMojo.PROJECT_TYPE);
if (projectType != null) {
component.setType(resolveProjectType(projectType));
}
Expand All @@ -204,20 +199,11 @@ public Component convertMavenDependency(Artifact artifact, Version schemaVersion

}

public String getProjectTypeFromPluginConfiguration(MavenProject project) {
return Optional.ofNullable(project.getBuild())
.map(build -> build.getPlugins())
.flatMap(plugins -> plugins.stream()
.filter(plugin -> CYCLONEDX_GROUP_ID.equals(plugin.getGroupId()) &&
CYCLONEDX_ARTIFACT_ID.equals(plugin.getArtifactId()))
.findFirst()
)
.map(Plugin::getConfiguration)
.filter(Xpp3Dom.class::isInstance)
.map(Xpp3Dom.class::cast)
.map(configuration -> configuration.getChild(PROJECT_TYPE_NODE))
.map(Xpp3Dom::getValue)
.orElse(null);
public String getPluginConfiguration(MavenProject project, String property) {
Plugin plugin = project.getPlugin(BaseCycloneDxMojo.CYCLONEDX_PLUGIN_KEY);
Xpp3Dom configuration = (plugin == null) ? null : (Xpp3Dom) plugin.getConfiguration();
Xpp3Dom value = (configuration == null) ? null : configuration.getChild(property);
return (value == null) ? null : value.getValue();
}

private static void setExternalReferences(Component component, ExternalReference[] externalReferences) {
Expand Down Expand Up @@ -357,8 +343,7 @@ private LicenseChoice resolveMavenLicenses(final List<org.apache.maven.model.Lic
return licenseChoice;
}

private boolean resolveLicenseInfo(final LicenseChoice licenseChoice, final LicenseChoice licenseChoiceToResolve, final Version schemaVersion)
{
private boolean resolveLicenseInfo(final LicenseChoice licenseChoice, final LicenseChoice licenseChoiceToResolve, final Version schemaVersion) {
if (licenseChoiceToResolve != null) {
if (licenseChoiceToResolve.getLicenses() != null && !licenseChoiceToResolve.getLicenses().isEmpty()) {
licenseChoice.addLicense(licenseChoiceToResolve.getLicenses().get(0));
Expand Down
63 changes: 18 additions & 45 deletions src/test/java/org/cyclonedx/maven/Issue521Test.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.cyclonedx.maven;



import org.junit.runner.RunWith;


Expand All @@ -25,45 +23,25 @@

@RunWith(MavenJUnitTestRunner.class)
@MavenVersions({"3.6.3"})

public class Issue521Test extends BaseMavenVerifier {

public Issue521Test(MavenRuntimeBuilder runtimeBuilder) throws Exception {
super(runtimeBuilder);

}


@Test
public void testFilePresence() throws Exception {
File projDir = cleanAndBuild("issue-521", null);
assertFileExists(projDir, "target/bom.json");
assertFileExists(projDir, "target/bom.xml");
}

@Test
public void testBomJsonContent() throws Exception {
File projDir = cleanAndBuild("issue-521", null);
verifyBomJsonContains(projDir, "target/bom.json", "issue-521-module1", "application");
}

@Test
public void testBomXmlContent() throws Exception {
File projDir = cleanAndBuild("issue-521", null);
checkBomXml(projDir);
}



@Test
public void testBomJsonContent() throws Exception {
File projDir = cleanAndBuild("issue-521", null);
checkBomXml(projDir, "target/bom.xml", "issue-521-app", "application");
checkBomJson(projDir, "target/bom.json", "issue-521-app", "application");
}

private static void assertFileExists(File basedir, String filePath) {
File file = new File(basedir, filePath);
assertTrue(file.exists(), String.format("File %s should exist", filePath));
private static void assertFileExists(File file) {
assertTrue(file.exists(), String.format("File %s should exist", file));
}
private void verifyBomJsonContains(File basedir, String filePath, String expectedName, String expectedType) throws IOException {

private void checkBomJson(File basedir, String filePath, String expectedName, String expectedType) throws IOException {
File bomJsonFile = new File(basedir, filePath);
assertTrue(bomJsonFile.exists(), String.format("File %s should exist", filePath));
assertFileExists(bomJsonFile);

// Leggi il contenuto del file bom.json e verifica la presenza dei campi desiderati
String bomContents = fileRead(bomJsonFile, true);
Expand All @@ -73,8 +51,11 @@ private void verifyBomJsonContains(File basedir, String filePath, String expecte
String.format("bom.json should contain a module with type '%s'", expectedType));
}

private void checkBomXml(final File projDir) throws Exception {
final Document bom = readXML(new File(projDir, "target/bom.xml"));
private void checkBomXml(File basedir, String filePath, String expectedName, String expectedType) throws Exception {
File bomXmlFile = new File(basedir, filePath);
assertFileExists(bomXmlFile);

final Document bom = readXML(bomXmlFile);

final NodeList componentsList = bom.getElementsByTagName("component");
assertTrue(componentsList.getLength() > 0, "Expected at least one component element");
Expand All @@ -84,21 +65,13 @@ private void checkBomXml(final File projDir) throws Exception {
Element component = (Element) componentsList.item(i);
String name = component.getElementsByTagName("name").item(0).getTextContent();
String type = component.getAttribute("type");
if ("issue-521-module1".equals(name) && "application".equals(type)) {

if (expectedName.equals(name) && expectedType.equals(type)) {
found = true;
break;
}
}

assertTrue(found, "Expected to find a component with name 'module1' and type 'application'");
}








}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@
<version>${revision}</version>
</parent>

<artifactId>issue-521-module1</artifactId>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1</version>
</dependency>
</dependencies>
<artifactId>issue-521-app</artifactId>

<build>
<plugins>
Expand All @@ -30,7 +22,6 @@
<configuration>
<projectType>application</projectType>
</configuration>

</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,5 @@
<version>${revision}</version>
</parent>

<artifactId>issue-521-module2</artifactId>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1</version>
</dependency>
</dependencies>

<artifactId>issue-521-lib</artifactId>
</project>
17 changes: 2 additions & 15 deletions src/test/resources/issue-521/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</licenses>

<modules>
<module>module1</module>
<module>module2</module>
<module>app</module>
<module>lib</module>
</modules>

<properties>
Expand All @@ -44,19 +44,6 @@
</goals>
</execution>
</executions>
<configuration>

<schemaVersion>1.4</schemaVersion>
<includeBomSerialNumber>true</includeBomSerialNumber>
<includeCompileScope>true</includeCompileScope>
<includeProvidedScope>false</includeProvidedScope>
<includeRuntimeScope>false</includeRuntimeScope>
<includeSystemScope>false</includeSystemScope>
<includeTestScope>false</includeTestScope>
<includeLicenseText>false</includeLicenseText>
<outputFormat>all</outputFormat>
</configuration>

</plugin>
</plugins>
</build>
Expand Down

0 comments on commit 6429094

Please sign in to comment.