Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support collecting dependencies from dependency management #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------
Expand Down
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mavenVersion>3.0.4</mavenVersion>
<mavenPluginPluginVersion>3.2</mavenPluginPluginVersion>
<version.surefire.plugin>2.22.0</version.surefire.plugin>

<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencies>
Expand Down Expand Up @@ -86,6 +90,20 @@
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.surefire.plugin}</version>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<!-- Workaround for Java8 Surefire Issue: The forked VM terminated without properly saying goodbye. VM crash -->
<argLine>-Xmx1024m -Djdk.net.URLClassPath.disableClassPathURLCheck=true</argLine>
</configuration>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
Expand Down
154 changes: 106 additions & 48 deletions src/main/java/org/jboss/maven/plugins/bombuilder/BuildBomMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +68,7 @@ public class BuildBomMojo
* BOM name
*/
@Parameter
private boolean addVersionProperties;
boolean addVersionProperties;

/**
* BOM description
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -163,67 +183,93 @@ private Model initializeModel()
return pomModel;
}

private void addDependencyManagement( Model pomModel )
{
// Sort the artifacts for readability
List<Artifact> projectArtifacts = new ArrayList<Artifact>( mavenProject.getArtifacts() );
Collections.sort( projectArtifacts );
private void addDependencyManagement( Model pomModel ) {
List<Dependency> 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<Dependency> getArtifactsAsDependencies() {
return mavenProject.getArtifacts().stream()
.map(this::map)
.filter(this::isDependencyNotExcluded)
.collect(Collectors.toList());
}

private List<Dependency> getDependencies() {
return mavenProject.getDependencies().stream()
.map(Dependency::clone)
.filter(this::isDependencyNotExcluded)
.collect(Collectors.toList());
}

private List<Dependency> 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));
Expand All @@ -247,6 +293,18 @@ private void applyExclusions(Artifact artifact, Dependency dep) {
}
}

private Properties generateVersionProperties(List<Dependency> 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 {

Expand Down
Loading