Skip to content

Commit 6429094

Browse files
committed
simplify code
Signed-off-by: Hervé Boutemy <[email protected]>
1 parent 4450f3b commit 6429094

File tree

6 files changed

+36
-112
lines changed

6 files changed

+36
-112
lines changed

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import java.util.UUID;
6262

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

6567
@Parameter(property = "project", readonly = true, required = true)
6668
private MavenProject project;
@@ -72,7 +74,7 @@ public abstract class BaseCycloneDxMojo extends AbstractMojo {
7274
*
7375
* @since 2.0.0
7476
*/
75-
@Parameter(property = "projectType", defaultValue = "library", required = false)
77+
@Parameter(property = PROJECT_TYPE, defaultValue = "library", required = false)
7678
private String projectType;
7779

7880
/**
@@ -622,13 +624,8 @@ private static boolean isDeployable(final MavenProject project,
622624
for (final PluginExecution execution : plugin.getExecutions()) {
623625
if (execution.getGoals().contains("deploy")) {
624626
final Xpp3Dom executionConf = (Xpp3Dom) execution.getConfiguration();
625-
boolean skipValue = defaultSkipValue;
626-
if (executionConf != null) {
627-
Xpp3Dom target = executionConf.getChild(parameter);
628-
if (target != null) {
629-
skipValue = Boolean.parseBoolean(target.getValue());
630-
}
631-
}
627+
final Xpp3Dom target = (executionConf == null) ? null : executionConf.getChild(parameter);
628+
final boolean skipValue = (target == null) ? defaultSkipValue : Boolean.parseBoolean(target.getValue());
632629
if (!skipValue) {
633630
return true;
634631
}

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

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,10 @@
6363
import org.apache.maven.model.Plugin;
6464
import org.codehaus.plexus.util.xml.Xpp3Dom;
6565

66-
import java.util.Optional;
67-
6866
@Singleton
6967
@Named
7068
public class DefaultModelConverter implements ModelConverter {
7169
private final Logger logger = LoggerFactory.getLogger(DefaultModelConverter.class);
72-
private static final String CYCLONEDX_GROUP_ID = "org.cyclonedx";
73-
private static final String CYCLONEDX_ARTIFACT_ID = "cyclonedx-maven-plugin";
74-
private static final String PROJECT_TYPE_NODE = "projectType";
7570

7671
@Inject
7772
private MavenSession session;
@@ -167,8 +162,8 @@ public Component convertMavenDependency(Artifact artifact, Version schemaVersion
167162
final Component component = new Component();
168163
component.setGroup(artifact.getGroupId());
169164
component.setName(artifact.getArtifactId());
170-
component.setVersion(artifact.getBaseVersion());
171-
component.setType(Component.Type.LIBRARY);
165+
component.setVersion(artifact.getBaseVersion());
166+
component.setType(Component.Type.LIBRARY);
172167

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

189184
if (project != null) {
190-
String projectType = getProjectTypeFromPluginConfiguration(project);
185+
String projectType = getPluginConfiguration(project, BaseCycloneDxMojo.PROJECT_TYPE);
191186
if (projectType != null) {
192187
component.setType(resolveProjectType(projectType));
193188
}
@@ -204,20 +199,11 @@ public Component convertMavenDependency(Artifact artifact, Version schemaVersion
204199

205200
}
206201

207-
public String getProjectTypeFromPluginConfiguration(MavenProject project) {
208-
return Optional.ofNullable(project.getBuild())
209-
.map(build -> build.getPlugins())
210-
.flatMap(plugins -> plugins.stream()
211-
.filter(plugin -> CYCLONEDX_GROUP_ID.equals(plugin.getGroupId()) &&
212-
CYCLONEDX_ARTIFACT_ID.equals(plugin.getArtifactId()))
213-
.findFirst()
214-
)
215-
.map(Plugin::getConfiguration)
216-
.filter(Xpp3Dom.class::isInstance)
217-
.map(Xpp3Dom.class::cast)
218-
.map(configuration -> configuration.getChild(PROJECT_TYPE_NODE))
219-
.map(Xpp3Dom::getValue)
220-
.orElse(null);
202+
public String getPluginConfiguration(MavenProject project, String property) {
203+
Plugin plugin = project.getPlugin(BaseCycloneDxMojo.CYCLONEDX_PLUGIN_KEY);
204+
Xpp3Dom configuration = (plugin == null) ? null : (Xpp3Dom) plugin.getConfiguration();
205+
Xpp3Dom value = (configuration == null) ? null : configuration.getChild(property);
206+
return (value == null) ? null : value.getValue();
221207
}
222208

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

360-
private boolean resolveLicenseInfo(final LicenseChoice licenseChoice, final LicenseChoice licenseChoiceToResolve, final Version schemaVersion)
361-
{
346+
private boolean resolveLicenseInfo(final LicenseChoice licenseChoice, final LicenseChoice licenseChoiceToResolve, final Version schemaVersion) {
362347
if (licenseChoiceToResolve != null) {
363348
if (licenseChoiceToResolve.getLicenses() != null && !licenseChoiceToResolve.getLicenses().isEmpty()) {
364349
licenseChoice.addLicense(licenseChoiceToResolve.getLicenses().get(0));
Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.cyclonedx.maven;
22

3-
4-
53
import org.junit.runner.RunWith;
64

75

@@ -25,45 +23,25 @@
2523

2624
@RunWith(MavenJUnitTestRunner.class)
2725
@MavenVersions({"3.6.3"})
28-
2926
public class Issue521Test extends BaseMavenVerifier {
30-
3127
public Issue521Test(MavenRuntimeBuilder runtimeBuilder) throws Exception {
3228
super(runtimeBuilder);
33-
3429
}
35-
36-
37-
@Test
38-
public void testFilePresence() throws Exception {
39-
File projDir = cleanAndBuild("issue-521", null);
40-
assertFileExists(projDir, "target/bom.json");
41-
assertFileExists(projDir, "target/bom.xml");
42-
}
43-
44-
@Test
45-
public void testBomJsonContent() throws Exception {
46-
File projDir = cleanAndBuild("issue-521", null);
47-
verifyBomJsonContains(projDir, "target/bom.json", "issue-521-module1", "application");
48-
}
49-
50-
@Test
51-
public void testBomXmlContent() throws Exception {
52-
File projDir = cleanAndBuild("issue-521", null);
53-
checkBomXml(projDir);
54-
}
5530

56-
57-
31+
@Test
32+
public void testBomJsonContent() throws Exception {
33+
File projDir = cleanAndBuild("issue-521", null);
34+
checkBomXml(projDir, "target/bom.xml", "issue-521-app", "application");
35+
checkBomJson(projDir, "target/bom.json", "issue-521-app", "application");
36+
}
5837

59-
private static void assertFileExists(File basedir, String filePath) {
60-
File file = new File(basedir, filePath);
61-
assertTrue(file.exists(), String.format("File %s should exist", filePath));
38+
private static void assertFileExists(File file) {
39+
assertTrue(file.exists(), String.format("File %s should exist", file));
6240
}
63-
64-
private void verifyBomJsonContains(File basedir, String filePath, String expectedName, String expectedType) throws IOException {
41+
42+
private void checkBomJson(File basedir, String filePath, String expectedName, String expectedType) throws IOException {
6543
File bomJsonFile = new File(basedir, filePath);
66-
assertTrue(bomJsonFile.exists(), String.format("File %s should exist", filePath));
44+
assertFileExists(bomJsonFile);
6745

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

76-
private void checkBomXml(final File projDir) throws Exception {
77-
final Document bom = readXML(new File(projDir, "target/bom.xml"));
54+
private void checkBomXml(File basedir, String filePath, String expectedName, String expectedType) throws Exception {
55+
File bomXmlFile = new File(basedir, filePath);
56+
assertFileExists(bomXmlFile);
57+
58+
final Document bom = readXML(bomXmlFile);
7859

7960
final NodeList componentsList = bom.getElementsByTagName("component");
8061
assertTrue(componentsList.getLength() > 0, "Expected at least one component element");
@@ -84,21 +65,13 @@ private void checkBomXml(final File projDir) throws Exception {
8465
Element component = (Element) componentsList.item(i);
8566
String name = component.getElementsByTagName("name").item(0).getTextContent();
8667
String type = component.getAttribute("type");
87-
88-
if ("issue-521-module1".equals(name) && "application".equals(type)) {
68+
69+
if (expectedName.equals(name) && expectedType.equals(type)) {
8970
found = true;
9071
break;
9172
}
9273
}
9374

9475
assertTrue(found, "Expected to find a component with name 'module1' and type 'application'");
9576
}
96-
97-
98-
99-
100-
101-
102-
103-
10477
}

src/test/resources/issue-521/module1/pom.xml renamed to src/test/resources/issue-521/app/pom.xml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@
1111
<version>${revision}</version>
1212
</parent>
1313

14-
<artifactId>issue-521-module1</artifactId>
15-
16-
<dependencies>
17-
<dependency>
18-
<groupId>junit</groupId>
19-
<artifactId>junit</artifactId>
20-
<version>4.1</version>
21-
</dependency>
22-
</dependencies>
14+
<artifactId>issue-521-app</artifactId>
2315

2416
<build>
2517
<plugins>
@@ -30,7 +22,6 @@
3022
<configuration>
3123
<projectType>application</projectType>
3224
</configuration>
33-
3425
</plugin>
3526
</plugins>
3627
</build>

src/test/resources/issue-521/module2/pom.xml renamed to src/test/resources/issue-521/lib/pom.xml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,5 @@
1111
<version>${revision}</version>
1212
</parent>
1313

14-
<artifactId>issue-521-module2</artifactId>
15-
16-
<dependencies>
17-
<dependency>
18-
<groupId>junit</groupId>
19-
<artifactId>junit</artifactId>
20-
<version>4.1</version>
21-
</dependency>
22-
</dependencies>
23-
14+
<artifactId>issue-521-lib</artifactId>
2415
</project>

src/test/resources/issue-521/pom.xml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
</licenses>
1919

2020
<modules>
21-
<module>module1</module>
22-
<module>module2</module>
21+
<module>app</module>
22+
<module>lib</module>
2323
</modules>
2424

2525
<properties>
@@ -44,19 +44,6 @@
4444
</goals>
4545
</execution>
4646
</executions>
47-
<configuration>
48-
49-
<schemaVersion>1.4</schemaVersion>
50-
<includeBomSerialNumber>true</includeBomSerialNumber>
51-
<includeCompileScope>true</includeCompileScope>
52-
<includeProvidedScope>false</includeProvidedScope>
53-
<includeRuntimeScope>false</includeRuntimeScope>
54-
<includeSystemScope>false</includeSystemScope>
55-
<includeTestScope>false</includeTestScope>
56-
<includeLicenseText>false</includeLicenseText>
57-
<outputFormat>all</outputFormat>
58-
</configuration>
59-
6047
</plugin>
6148
</plugins>
6249
</build>

0 commit comments

Comments
 (0)