Skip to content

Commit

Permalink
Merge pull request #8 from maveniverse/use-dep-control
Browse files Browse the repository at this point in the history
More control about deps getting into BOM
  • Loading branch information
cstamas authored Oct 21, 2024
2 parents fc2fcb5 + 95ce702 commit 617e7ff
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
Expand Down Expand Up @@ -79,7 +80,9 @@ public class BuildBomMojo extends AbstractMojo {
private String bomName;

/**
* BOM name
* Whether to add collected versions to BOM properties
*
* @see #usePropertiesForVersion
*/
@Parameter
private boolean addVersionProperties;
Expand Down Expand Up @@ -111,13 +114,51 @@ public class BuildBomMojo extends AbstractMojo {
private List<DependencyExclusion> dependencyExclusions;

/**
* Whether to use properties to specify dependency versions in BOM
* Whether to use properties to specify dependency versions in BOM. This will also add properties to BOM with
* dependency versions.
*
* @see #addVersionProperties
*/
@Parameter(property = "bom.usePropertiesForVersion")
boolean usePropertiesForVersion;

@Parameter(property = "bom.useDependencies")
boolean useDependencies;
/**
* Modes to control dependencies getting into BOM.
*
* @since 1.0.2
*/
public enum UseDependencies {
PROJECT_ONLY(true, false, false),
DIRECT_ONLY(false, true, false),
TRANSITIVE_ONLY(false, false, true),
PROJECT_AND_DIRECT(true, true, false),
PROJECT_AND_TRANSITIVE(true, false, true);

private final boolean project;
private final boolean directDependencies;
private final boolean transitiveDependencies;

UseDependencies(boolean project, boolean directDependencies, boolean transitiveDependencies) {
this.project = project;
this.directDependencies = directDependencies;
this.transitiveDependencies = transitiveDependencies;
}

public boolean isProject() {
return project;
}

public boolean isDirectDependencies() {
return directDependencies;
}

public boolean isTransitiveDependencies() {
return transitiveDependencies;
}
}

@Parameter(property = "bom.useDependencies", defaultValue = "PROJECT_ONLY")
UseDependencies useDependencies;

@Parameter(property = "bom.includePoms")
boolean includePoms;
Expand Down Expand Up @@ -208,17 +249,23 @@ private Model initializeModel() throws MojoExecutionException {
}

private void addDependencyManagement(Model pomModel) {
// Sort the artifacts for readability
List<Artifact> projectArtifacts = new ArrayList<>();
if (useDependencies) {
projectArtifacts.addAll(mavenProject.getArtifacts());
} else {
HashSet<Artifact> projectArtifactsSet = new HashSet<>();
if (useDependencies.isProject()) {
for (MavenProject prj : allProjects) {
if (includePoms || !"pom".equals(prj.getArtifact().getType())) {
projectArtifacts.add(prj.getArtifact());
projectArtifactsSet.add(prj.getArtifact());
}
}
}
if (useDependencies.isDirectDependencies()) {
projectArtifactsSet.addAll(mavenProject.getDependencyArtifacts());
}
if (useDependencies.isTransitiveDependencies()) {
projectArtifactsSet.addAll(mavenProject.getArtifacts());
}

// Sort the artifacts for readability
ArrayList<Artifact> projectArtifacts = new ArrayList<>(projectArtifactsSet);
Collections.sort(projectArtifacts);

Properties versionProperties = new Properties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private BuildBomMojo createBuildBomMojo() {
mojo.mavenProject.getBuild().setOutputDirectory("target");
mojo.outputFilename = "pom.xml";
mojo.allProjects = Collections.emptyList();
mojo.useDependencies = BuildBomMojo.UseDependencies.PROJECT_ONLY;
return mojo;
}

Expand Down
2 changes: 1 addition & 1 deletion it3/src/it/basic-bom-with-exclusions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<bomGroupId>org.test</bomGroupId>
<bomArtifactId>junit-bom</bomArtifactId>
<bomVersion>1.0</bomVersion>
<useDependencies>true</useDependencies>
<useDependencies>TRANSITIVE_ONLY</useDependencies>
<exclusions>
<exclusion>
<dependencyGroupId>org.apache.maven</dependencyGroupId>
Expand Down
2 changes: 1 addition & 1 deletion it3/src/it/basic-bom-with-properties/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<bomGroupId>org.test</bomGroupId>
<bomArtifactId>junit-bom</bomArtifactId>
<bomVersion>1.0</bomVersion>
<useDependencies>true</useDependencies>
<useDependencies>TRANSITIVE_ONLY</useDependencies>
<addVersionProperties>true</addVersionProperties>
</configuration>
</execution>
Expand Down
2 changes: 1 addition & 1 deletion it3/src/it/basic-bom-with-type/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<bomGroupId>org.test</bomGroupId>
<bomArtifactId>junit-bom</bomArtifactId>
<bomVersion>1.0</bomVersion>
<useDependencies>true</useDependencies>
<useDependencies>TRANSITIVE_ONLY</useDependencies>
</configuration>
</execution>
</executions>
Expand Down
2 changes: 1 addition & 1 deletion it3/src/it/bom-with-excluded-dependency/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<bomGroupId>org.test</bomGroupId>
<bomArtifactId>junit-bom</bomArtifactId>
<bomVersion>1.0</bomVersion>
<useDependencies>true</useDependencies>
<useDependencies>TRANSITIVE_ONLY</useDependencies>
<dependencyExclusions>
<dependencyExclusion>
<groupId>org.apache.maven</groupId>
Expand Down
2 changes: 1 addition & 1 deletion it3/src/it/bom-with-version-properties/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<bomArtifactId>junit-bom</bomArtifactId>
<bomVersion>1.0</bomVersion>
<usePropertiesForVersion>true</usePropertiesForVersion>
<useDependencies>true</useDependencies>
<useDependencies>TRANSITIVE_ONLY</useDependencies>
</configuration>
</execution>
</executions>
Expand Down

0 comments on commit 617e7ff

Please sign in to comment.