diff --git a/src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java b/src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java index dc05ed86..fe6f21f2 100644 --- a/src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java +++ b/src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java @@ -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; @@ -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; /** @@ -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; } diff --git a/src/main/java/org/cyclonedx/maven/DefaultModelConverter.java b/src/main/java/org/cyclonedx/maven/DefaultModelConverter.java index d0b1da8d..ca659c4e 100644 --- a/src/main/java/org/cyclonedx/maven/DefaultModelConverter.java +++ b/src/main/java/org/cyclonedx/maven/DefaultModelConverter.java @@ -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; @@ -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); @@ -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)); } @@ -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) { @@ -357,8 +343,7 @@ private LicenseChoice resolveMavenLicenses(final List 0, "Expected at least one component element"); @@ -84,8 +65,8 @@ 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; } @@ -93,12 +74,4 @@ private void checkBomXml(final File projDir) throws Exception { assertTrue(found, "Expected to find a component with name 'module1' and type 'application'"); } - - - - - - - - } diff --git a/src/test/resources/issue-521/module1/pom.xml b/src/test/resources/issue-521/app/pom.xml similarity index 76% rename from src/test/resources/issue-521/module1/pom.xml rename to src/test/resources/issue-521/app/pom.xml index 603a0df5..381bfd6d 100644 --- a/src/test/resources/issue-521/module1/pom.xml +++ b/src/test/resources/issue-521/app/pom.xml @@ -11,15 +11,7 @@ ${revision} - issue-521-module1 - - - - junit - junit - 4.1 - - + issue-521-app @@ -30,7 +22,6 @@ application - diff --git a/src/test/resources/issue-521/module2/pom.xml b/src/test/resources/issue-521/lib/pom.xml similarity index 65% rename from src/test/resources/issue-521/module2/pom.xml rename to src/test/resources/issue-521/lib/pom.xml index ef438b5e..604d5e6b 100644 --- a/src/test/resources/issue-521/module2/pom.xml +++ b/src/test/resources/issue-521/lib/pom.xml @@ -11,14 +11,5 @@ ${revision} - issue-521-module2 - - - - junit - junit - 4.1 - - - + issue-521-lib diff --git a/src/test/resources/issue-521/pom.xml b/src/test/resources/issue-521/pom.xml index 44eb2977..0cfbe815 100644 --- a/src/test/resources/issue-521/pom.xml +++ b/src/test/resources/issue-521/pom.xml @@ -18,8 +18,8 @@ - module1 - module2 + app + lib @@ -44,19 +44,6 @@ - - - 1.4 - true - true - false - false - false - false - false - all - -