From 1f54dddbf811d7d613d37a3fa9424b4770883fdc Mon Sep 17 00:00:00 2001 From: Daniel Kellenberger Date: Thu, 21 Feb 2019 08:24:13 +0100 Subject: [PATCH 1/3] Update to Java 8 --- pom.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pom.xml b/pom.xml index debcf3e..674e93c 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,9 @@ UTF-8 3.0.4 3.2 + + 1.8 + 1.8 From b77c2f14f4f1a6702eab850ae6ccf8dae6f724a1 Mon Sep 17 00:00:00 2001 From: Daniel Kellenberger Date: Thu, 21 Feb 2019 17:37:22 +0100 Subject: [PATCH 2/3] Fix JUnit Execution with Java 8. --- pom.xml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pom.xml b/pom.xml index 674e93c..755a88c 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,7 @@ UTF-8 3.0.4 3.2 + 2.22.0 1.8 1.8 @@ -89,6 +90,20 @@ + + + org.apache.maven.plugins + maven-surefire-plugin + ${version.surefire.plugin} + + 3 + true + + -Xmx1024m -Djdk.net.URLClassPath.disableClassPathURLCheck=true + + + + From cae5bb48c31193916be4e3cf843ea0df98dcd82b Mon Sep 17 00:00:00 2001 From: Daniel Kellenberger Date: Thu, 21 Feb 2019 20:33:49 +0100 Subject: [PATCH 3/3] Support collecting dependencies from dependency management and dependencies only (without resolved dependencies) --- README.md | 34 ++-- .../plugins/bombuilder/BuildBomMojo.java | 154 ++++++++++++------ .../plugins/bombuilder/BuildBomMojoTest.java | 95 ++++++++--- 3 files changed, 199 insertions(+), 84 deletions(-) diff --git a/README.md b/README.md index 8e4ee97..74343b1 100644 --- a/README.md +++ b/README.md @@ -38,23 +38,31 @@ The plugin is configured in the "plugins" section of the pom. Config Parameters ----------------- -bomGroupId - The groupId to set in the generated BOM -bomArtifactId - The artifactId to set in the generated BOM -bomVersion - The version to set in the generated BOM -bomName - The name to set in the generated BOM -bomDescription - The description to set in the generated BOM -exclusions - A list of exclusions to set in the genertated BOM -dependencyExclusions - A list of dependencies which should not be included in the genertated BOM + +| Config | Description | +|:------------------------------------|:--------------------------------------------------------------------------| +| bomGroupId | The groupId to set in the generated BOM | +| bomArtifactId | The artifactId to set in the generated BOM | +| bomVersion | The version to set in the generated BOM | +| bomName | The name to set in the generated BOM | +| bomDescription | The description to set in the generated BOM | +| exclusions | A list of exclusions to set in the genertated BOM | +| dependencyExclusions | A list of dependencies which should not be included in the genertated BOM | +| useArtifacts | Use all dependencies and transitive dependencies (default: true) | +| useDependencies | Use only defined dependencies (default: false) | +| useDependencyManagementDependencies | Use dependency management dependencies (default: false) | Each exclusion should contain four parameters: - - dependencyGroupId - - dependencyArtifactId - - exclusionGroupId - - exclusionArtifactId + +- dependencyGroupId +- dependencyArtifactId +- exclusionGroupId +- exclusionArtifactId Each dependency exclusion should contain two parameters: - - groupId - - artifactId + +- groupId +- artifactId Exclusion Config Example ------------------- diff --git a/src/main/java/org/jboss/maven/plugins/bombuilder/BuildBomMojo.java b/src/main/java/org/jboss/maven/plugins/bombuilder/BuildBomMojo.java index c097e79..851271c 100644 --- a/src/main/java/org/jboss/maven/plugins/bombuilder/BuildBomMojo.java +++ b/src/main/java/org/jboss/maven/plugins/bombuilder/BuildBomMojo.java @@ -3,11 +3,13 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedList; import java.util.List; import java.util.Properties; +import java.util.stream.Collectors; import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Dependency; import org.apache.maven.model.DependencyManagement; @@ -66,7 +68,7 @@ public class BuildBomMojo * BOM name */ @Parameter - private boolean addVersionProperties; + boolean addVersionProperties; /** * BOM description @@ -101,6 +103,24 @@ public class BuildBomMojo @Parameter boolean usePropertiesForVersion; + /** + * Whether to use dependency management dependencies. + */ + @Parameter(defaultValue = "false") + boolean useDependencyManagementDependencies; + + /** + * Wheter to use dependencies. + */ + @Parameter(defaultValue = "false") + boolean useDependencies; + + /** + * Wheter to use all resolved dependencies (defaults to true). + */ + @Parameter(defaultValue = "true") + boolean useAllResolvedDependencies = true; + /** * The current project */ @@ -163,67 +183,93 @@ private Model initializeModel() return pomModel; } - private void addDependencyManagement( Model pomModel ) - { - // Sort the artifacts for readability - List projectArtifacts = new ArrayList( mavenProject.getArtifacts() ); - Collections.sort( projectArtifacts ); + private void addDependencyManagement( Model pomModel ) { + List dependencies = new LinkedList<>(); - Properties versionProperties = new Properties(); - DependencyManagement depMgmt = new DependencyManagement(); - for ( Artifact artifact : projectArtifacts ) - { - if (isExcludedDependency(artifact)) { - continue; - } + if (useAllResolvedDependencies) { + dependencies.addAll(getArtifactsAsDependencies()); + } + if (useDependencies) { + dependencies.addAll(getDependencies()); + } + if (useDependencyManagementDependencies) { + dependencies.addAll(getDependencyManagementDependencies()); + } - String versionPropertyName = VERSION_PROPERTY_PREFIX + artifact.getGroupId(); - if (versionProperties.getProperty(versionPropertyName) != null - && !versionProperties.getProperty(versionPropertyName).equals(artifact.getVersion())) { - versionPropertyName = VERSION_PROPERTY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId(); - } - versionProperties.setProperty(versionPropertyName, artifact.getVersion()); + final DependencyManagement depMgmt = new DependencyManagement(); + dependencies.stream() + .sorted(Comparator.comparing(Dependency::getGroupId).thenComparing(Dependency::getArtifactId)) + .forEach(depMgmt::addDependency); - Dependency dep = new Dependency(); - dep.setGroupId( artifact.getGroupId() ); - dep.setArtifactId( artifact.getArtifactId() ); - dep.setVersion( artifact.getVersion() ); - if ( !StringUtils.isEmpty( artifact.getClassifier() )) - { - dep.setClassifier( artifact.getClassifier() ); - } - if ( !StringUtils.isEmpty( artifact.getType() )) - { - dep.setType( artifact.getType() ); - } - if (exclusions != null) { - applyExclusions(artifact, dep); - } - depMgmt.addDependency( dep ); - } - pomModel.setDependencyManagement( depMgmt ); if (addVersionProperties) { + Properties versionProperties = generateVersionProperties(dependencies); pomModel.getProperties().putAll(versionProperties); } - getLog().debug( "Added " + projectArtifacts.size() + " dependencies." ); + + pomModel.setDependencyManagement(depMgmt); + getLog().debug( "Added " + dependencies.size() + " dependencies." ); } - boolean isExcludedDependency(Artifact artifact) { + private List getArtifactsAsDependencies() { + return mavenProject.getArtifacts().stream() + .map(this::map) + .filter(this::isDependencyNotExcluded) + .collect(Collectors.toList()); + } + + private List getDependencies() { + return mavenProject.getDependencies().stream() + .map(Dependency::clone) + .filter(this::isDependencyNotExcluded) + .collect(Collectors.toList()); + } + + private List getDependencyManagementDependencies() { + final DependencyManagement dependencyManagement = mavenProject.getDependencyManagement(); + if (dependencyManagement == null) { + return Collections.emptyList(); + } + return dependencyManagement.getDependencies().stream() + .map(Dependency::clone) + .filter(this::isDependencyNotExcluded) + .collect(Collectors.toList()); + } + + private Dependency map(Artifact artifact) { + Dependency dep = new Dependency(); + dep.setGroupId( artifact.getGroupId() ); + dep.setArtifactId( artifact.getArtifactId() ); + dep.setVersion( artifact.getVersion() ); + if ( !StringUtils.isEmpty( artifact.getClassifier() )) + { + dep.setClassifier( artifact.getClassifier() ); + } + if ( !StringUtils.isEmpty( artifact.getType() )) + { + dep.setType( artifact.getType() ); + } + if (exclusions != null) { + applyExclusions(artifact, dep); + } + return dep; + } + + boolean isDependencyNotExcluded(Dependency dependency) { if (dependencyExclusions == null || dependencyExclusions.size() == 0) { - return false; + return true; } for (DependencyExclusion exclusion : dependencyExclusions) { - if (matchesExcludedDependency(artifact, exclusion)) { - getLog().debug( "Artifact " + artifact.getGroupId() + ":" + artifact.getArtifactId() + " matches excluded dependency " + exclusion.getGroupId() + ":" + exclusion.getArtifactId() ); - return true; + if (matchesExcludedDependency(dependency, exclusion)) { + getLog().debug( "Dependency " + dependency.getGroupId() + ":" + dependency.getArtifactId() + " matches excluded dependency " + exclusion.getGroupId() + ":" + exclusion.getArtifactId() ); + return false; } } - return false; + return true; } - boolean matchesExcludedDependency(Artifact artifact, DependencyExclusion exclusion) { - String groupId = defaultAndTrim(artifact.getGroupId()); - String artifactId = defaultAndTrim(artifact.getArtifactId()); + boolean matchesExcludedDependency(Dependency dependency, DependencyExclusion exclusion) { + String groupId = defaultAndTrim(dependency.getGroupId()); + String artifactId = defaultAndTrim(dependency.getArtifactId()); String exclusionGroupId = defaultAndTrim(exclusion.getGroupId()); String exclusionArtifactId = defaultAndTrim(exclusion.getArtifactId()); boolean groupIdMatched = ("*".equals (exclusionGroupId) || groupId.equals(exclusionGroupId)); @@ -247,6 +293,18 @@ private void applyExclusions(Artifact artifact, Dependency dep) { } } + private Properties generateVersionProperties(List dependencies) { + Properties versionProperties = new Properties(); + dependencies.forEach(dependency -> { + String versionPropertyName = VERSION_PROPERTY_PREFIX + dependency.getGroupId(); + if (versionProperties.getProperty(versionPropertyName) != null + && !versionProperties.getProperty(versionPropertyName).equals(dependency.getVersion())) { + versionPropertyName = VERSION_PROPERTY_PREFIX + dependency.getGroupId() + "." + dependency.getArtifactId(); + } + versionProperties.setProperty(versionPropertyName, dependency.getVersion()); + }); + return versionProperties; + } static class ModelWriter { diff --git a/src/test/java/org/jboss/maven/plugins/bombuilder/BuildBomMojoTest.java b/src/test/java/org/jboss/maven/plugins/bombuilder/BuildBomMojoTest.java index 219d34f..5423766 100644 --- a/src/test/java/org/jboss/maven/plugins/bombuilder/BuildBomMojoTest.java +++ b/src/test/java/org/jboss/maven/plugins/bombuilder/BuildBomMojoTest.java @@ -1,21 +1,26 @@ package org.jboss.maven.plugins.bombuilder; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.DefaultArtifact; -import org.apache.maven.artifact.handler.ArtifactHandler; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import java.io.File; +import java.util.Collections; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.DependencyManagement; import org.apache.maven.model.Model; import org.apache.maven.project.MavenProject; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.MockitoAnnotations.initMocks; -import static org.mockito.internal.verification.VerificationModeFactory.times; - +@RunWith(MockitoJUnitRunner.class) public class BuildBomMojoTest { @@ -27,8 +32,10 @@ public class BuildBomMojoTest { @Before public void before() { - initMocks(this); - mojo = createBuildBomMojo(); + mojo = new BuildBomMojo(modelWriter, versionTransformer); + mojo.mavenProject = new MavenProject(); + mojo.mavenProject.getBuild().setOutputDirectory("target"); + mojo.outputFilename = "pom.xml"; } @Test @@ -44,20 +51,55 @@ public void testDependencyVersionIsStoredInProperties() throws Exception { mojo.execute(); - verify(versionTransformer, times(1)).transformPomModel(any(Model.class)); + verify(versionTransformer).transformPomModel(any(Model.class)); } - private BuildBomMojo createBuildBomMojo() { - BuildBomMojo mojo = new BuildBomMojo(modelWriter, versionTransformer); - mojo.mavenProject = new MavenProject(); - mojo.mavenProject.getBuild().setOutputDirectory("target"); - mojo.outputFilename = "pom.xml"; - return mojo; + @Test + public void testDependencyManagementRetrieved() throws Exception { + // given + mojo.useDependencyManagementDependencies = true; + mojo.useAllResolvedDependencies = false; + + mojo.mavenProject.setDependencies(Collections.singletonList(createDependency("groupId", "shouldNotBeUsed"))); + mojo.mavenProject.getModel().setDependencyManagement(new DependencyManagement()); + mojo.mavenProject.getDependencyManagement().addDependency(createDependency("groupId", "artifactId1")); + mojo.mavenProject.getDependencyManagement().addDependency(createDependency("groupId", "artifactId2")); + + // when + mojo.execute(); + + // then + final ArgumentCaptor modelCaptor = ArgumentCaptor.forClass(Model.class); + verify(modelWriter).writeModel(modelCaptor.capture(), any(File.class)); + final Model model = modelCaptor.getValue(); + assertThat(model.getDependencyManagement().getDependencies().size(), equalTo(2)); + assertThat(model.getDependencyManagement().getDependencies().get(0).getArtifactId(), equalTo("artifactId1")); + assertThat(model.getDependencyManagement().getDependencies().get(1).getArtifactId(), equalTo("artifactId2")); } + @Test + public void testDependencyRetrieved() throws Exception { + // given + mojo.useDependencies = true; + mojo.useAllResolvedDependencies = false; + + mojo.mavenProject.setDependencies(Collections.singletonList(createDependency("groupId", "artifactId"))); + mojo.mavenProject.getModel().setDependencyManagement(new DependencyManagement()); + mojo.mavenProject.getDependencyManagement().addDependency(createDependency("groupId", "shouldNotBeUsed")); + + // when + mojo.execute(); + + // then + final ArgumentCaptor modelCaptor = ArgumentCaptor.forClass(Model.class); + verify(modelWriter).writeModel(modelCaptor.capture(), any(File.class)); + final Model model = modelCaptor.getValue(); + assertThat(model.getDependencyManagement().getDependencies().size(), equalTo(1)); + assertThat(model.getDependencyManagement().getDependencies().get(0).getArtifactId(), equalTo("artifactId")); + } @Test - public void testMatchesExcludedDependency() throws Exception { + public void testMatchesExcludedDependency() { assertArtifactMatchesExcludedDependency(true, "groupId", "artifactId", "groupId", "artifactId"); assertArtifactMatchesExcludedDependency(true, "groupId", "artifactId", "*", "artifactId"); assertArtifactMatchesExcludedDependency(true, "groupId", "artifactId", "groupId", "*"); @@ -71,7 +113,7 @@ public void testMatchesExcludedDependency() throws Exception { } private void assertArtifactMatchesExcludedDependency(boolean expected, String artifactGroupId, String artifactArtifactId, String dependencyGroupId, String dependencyArtifactId) { - Artifact artifact = createArtifact(artifactGroupId, artifactArtifactId); + Dependency artifact = createDependency(artifactGroupId, artifactArtifactId); DependencyExclusion exclusion = createDependencyExclusion(dependencyGroupId, dependencyArtifactId); BuildBomMojo mojo = new BuildBomMojo(); assertEquals(expected, mojo.matchesExcludedDependency(artifact, exclusion)); @@ -81,7 +123,14 @@ private DependencyExclusion createDependencyExclusion(String groupId, String art return new DependencyExclusion(groupId, artifactId); } - private Artifact createArtifact(String groupId, String artifactId) { - return new DefaultArtifact(groupId, artifactId, "version", "scope", "type", "classifier", (ArtifactHandler)null); + private Dependency createDependency(String groupId, String artifactId) { + final Dependency dependency = new Dependency(); + dependency.setGroupId(groupId); + dependency.setArtifactId(artifactId); + dependency.setVersion("version"); + dependency.setScope("scope"); + dependency.setType("type"); + dependency.setClassifier("classifier"); + return dependency; } } \ No newline at end of file