Skip to content

Commit

Permalink
Merge pull request #6 from maveniverse/parent-attach
Browse files Browse the repository at this point in the history
Add support for parent and attaching the BOM (fixes #2 and #3)
  • Loading branch information
gnodet authored Oct 8, 2024
2 parents 2a5cdc7 + ef1da6b commit 223558f
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Exclusion;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.Parent;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -26,7 +26,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.util.StringUtils;

/**
Expand All @@ -39,6 +39,13 @@
public class BuildBomMojo extends AbstractMojo {

private static final String VERSION_PROPERTY_PREFIX = "version.";

/**
* BOM parent GAV
*/
@Parameter
private String bomParentGav;

/**
* BOM groupId
*/
Expand All @@ -57,10 +64,16 @@ public class BuildBomMojo extends AbstractMojo {
@Parameter(required = true, property = "bom.version", defaultValue = "${project.version}")
private String bomVersion;

/**
* BOM classifier
*/
@Parameter(required = true, property = "bom.classifier", defaultValue = "bom")
private String bomClassifier;

/**
* BOM name
*/
@Parameter(defaultValue = "", property = "bom.name")
@Parameter(property = "bom.name")
private String bomName;

/**
Expand All @@ -72,7 +85,7 @@ public class BuildBomMojo extends AbstractMojo {
/**
* BOM description
*/
@Parameter(defaultValue = "")
@Parameter
private String bomDescription;

/**
Expand All @@ -83,7 +96,7 @@ public class BuildBomMojo extends AbstractMojo {

/**
* Whether the BOM should include the dependency exclusions that
* are present in the source POM. By default the exclusions
* are present in the source POM. By default, the exclusions
* will not be copied to the new BOM.
*/
@Parameter
Expand All @@ -96,9 +109,9 @@ public class BuildBomMojo extends AbstractMojo {
private List<DependencyExclusion> dependencyExclusions;

/**
* Whether use properties to specify dependency versions in BOM
* Whether to use properties to specify dependency versions in BOM
*/
@Parameter
@Parameter(property = "bom.usePropertiesForVersion")
boolean usePropertiesForVersion;

@Parameter(property = "bom.useDependencies")
Expand All @@ -107,26 +120,26 @@ public class BuildBomMojo extends AbstractMojo {
@Parameter(property = "bom.includePoms")
boolean includePoms;

@Parameter(property = "bom.useProjectParentAsParent")
boolean useProjectParentAsParent;

@Parameter(property = "bom.attach")
boolean attach;

/**
* The current project
*/
@Component
@Parameter(defaultValue = "${project}")
MavenProject mavenProject;

@Parameter(defaultValue = "${session.allProjects}")
List<MavenProject> allProjects;

/**
*
* All projects from reactor
*/
@Component
private ModelBuilder modelBuilder;
@Parameter(defaultValue = "${session.allProjects}")
List<MavenProject> allProjects;

/**
*
*/
@Component
private ProjectBuilder projectBuilder;
MavenProjectHelper mavenProjectHelper;

private final PomDependencyVersionsTransformer versionsTransformer;
private final ModelWriter modelWriter;
Expand All @@ -135,7 +148,7 @@ public BuildBomMojo() {
this(new ModelWriter(), new PomDependencyVersionsTransformer());
}

public BuildBomMojo(ModelWriter modelWriter, PomDependencyVersionsTransformer versionsTransformer) {
BuildBomMojo(ModelWriter modelWriter, PomDependencyVersionsTransformer versionsTransformer) {
this.versionsTransformer = versionsTransformer;
this.modelWriter = modelWriter;
}
Expand All @@ -148,13 +161,33 @@ public void execute() throws MojoExecutionException {
model = versionsTransformer.transformPomModel(model);
getLog().debug("Dependencies versions converted to properties");
}
modelWriter.writeModel(model, new File(mavenProject.getBuild().getDirectory(), outputFilename));
File outputFile = new File(mavenProject.getBuild().getDirectory(), outputFilename);
modelWriter.writeModel(model, outputFile);
if (attach) {
mavenProjectHelper.attachArtifact(mavenProject, bomClassifier, outputFile);
}
}

private Model initializeModel() {
private Model initializeModel() throws MojoExecutionException {
Model pomModel = new Model();
pomModel.setModelVersion("4.0.0");

if (bomParentGav != null) {
String[] gav = bomParentGav.split(":");
if (gav.length != 3) {
throw new MojoExecutionException(
"BOM parent should be specified as [groupId]:[artifactId]:[version] but is '" + bomParentGav
+ "'");
}
Parent parent = new Parent();
parent.setGroupId(gav[0]);
parent.setArtifactId(gav[1]);
parent.setVersion(gav[2]);
pomModel.setParent(parent);
} else if (useProjectParentAsParent && mavenProject.getModel().getParent() != null) {
pomModel.setParent(mavenProject.getModel().getParent());
}

pomModel.setGroupId(bomGroupId);
pomModel.setArtifactId(bomArtifactId);
pomModel.setVersion(bomVersion);
Expand Down
26 changes: 26 additions & 0 deletions it3/src/it/bom-reactor/child1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>test</groupId>
<artifactId>basic-bom</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>child1</artifactId>

</project>
26 changes: 26 additions & 0 deletions it3/src/it/bom-reactor/child2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>test</groupId>
<artifactId>basic-bom</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>child2</artifactId>

</project>
31 changes: 31 additions & 0 deletions it3/src/it/bom-reactor/expected/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>33</version>
</parent>
<groupId>test</groupId>
<artifactId>basic-bom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>child1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>test</groupId>
<artifactId>child2</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
62 changes: 62 additions & 0 deletions it3/src/it/bom-reactor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>33</version>
</parent>

<groupId>test</groupId>
<artifactId>basic-bom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Test to create basic bom</name>

<modules>
<module>child1</module>
<module>child2</module>
</modules>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>eu.maveniverse.maven.plugins</groupId>
<artifactId>bom-builder3</artifactId>
<version>@pom.version@</version>
<executions>
<execution>
<id>build-bom</id>
<goals>
<goal>build-bom</goal>
</goals>
<configuration>
<attach>true</attach>
<useProjectParentAsParent>true</useProjectParentAsParent>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
12 changes: 12 additions & 0 deletions it3/src/it/bom-reactor/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def name = "bom-reactor"
File file = new File(basedir, "target/bom-pom.xml")
File expectedFile = new File(basedir, "expected/pom.xml")

String fileContents = file.getText('UTF-8')
String expectedFileContents = expectedFile.getText('UTF-8')

def isDifferent = !fileContents.equals(expectedFileContents)
if (isDifferent) {
System.err.println("Generated " + file.absolutePath + " differs from expected " + expectedFile.absolutePath)
return false
}

0 comments on commit 223558f

Please sign in to comment.