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

Automated bom #836

Merged
merged 2 commits into from
Jun 7, 2024
Merged
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
48 changes: 29 additions & 19 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

<dependencyManagement>
<dependencies>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>api</artifactId>
Expand All @@ -21,112 +20,123 @@

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>builder</artifactId>
<artifactId>assert-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>message-core</artifactId>
<artifactId>assert-filter</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>message-text</artifactId>
<artifactId>assert-junit4</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>message-json</artifactId>
<artifactId>assert-junit5</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>message-http</artifactId>
<artifactId>builder</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>message-sql</artifactId>
<artifactId>coppice</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>message-web</artifactId>
<artifactId>duct</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>message-xml</artifactId>
<artifactId>message-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>model</artifactId>
<artifactId>message-http</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>validation-core</artifactId>
<artifactId>message-json</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>validation-junit4</artifactId>
<artifactId>message-sql</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>validation-junit5</artifactId>
<artifactId>message-text</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>coppice</artifactId>
<artifactId>message-web</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>report-core</artifactId>
<artifactId>message-xml</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>assert-filter</artifactId>
<artifactId>model</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>assert-core</artifactId>
<artifactId>report-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>assert-junit4</artifactId>
<artifactId>report-ng</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>assert-junit5</artifactId>
<artifactId>validation-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>validation-junit4</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>validation-junit5</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
95 changes: 95 additions & 0 deletions doc/src/test/java/com/mastercard/test/flow/doc/BomTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.mastercard.test.flow.doc;

import static java.util.stream.Collectors.joining;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.mastercard.test.flow.doc.PomData.DepData;

/**
* Ensures that our Bill of Materials pom contains all of the artifacts in this
* project
*/
class BomTest {

@Test
void check() {
PomData root = new PomData( null, Paths.get( "..", "pom.xml" ) );

PomData bom = root.modules()
.filter( m -> "bom".equals( m.artifactId() ) )
.findAny()
.orElseThrow( () -> new IllegalStateException( "Failed to find bom" ) );

List<PomData> artifacts = collectArtifacts( root, new ArrayList<>() );

// these are not releasable artifacts, so no point in managing them
artifacts.removeIf( pom -> pom.groupId().endsWith( "example" )
|| "aggregator".equals( pom.artifactId() )
|| "doc".equals( pom.artifactId() ) );
// no self-reference please
artifacts.remove( bom );

String expected = artifacts.stream()
.sorted( Comparator.comparing( PomData::artifactId ) )
.map( artifact -> String.format( ""
+ "\t\t\t<dependency>\n"
+ "\t\t\t\t<groupId>%s</groupId>\n"
+ "\t\t\t\t<artifactId>%s</artifactId>\n"
+ "\t\t\t\t<version>%s</version>\n"
+ "\t\t\t</dependency>",
artifact.groupId()
.replace( bom.groupId(), "${project.groupId}" ),
artifact.artifactId(),
artifact.version()
.replace( bom.version(), "${project.version}" ) ) )
.collect( joining(
"\n\n",
""
+ "\t<dependencyManagement>\n"
+ "\t\t<dependencies>\n",
""
+ "\n"
+ "\t\t</dependencies>\n"
+ "\t</dependencyManagement>" ) );

String actual = bom.dependencyManagement()
.sorted( Comparator.comparing( DepData::artifactId ) )
.map( dep -> String.format( ""
+ "\t\t\t<dependency>\n"
+ "\t\t\t\t<groupId>%s</groupId>\n"
+ "\t\t\t\t<artifactId>%s</artifactId>\n"
+ "\t\t\t\t<version>%s</version>\n"
+ "\t\t\t</dependency>",
dep.groupId()
.replace( bom.groupId(), "${project.groupId}" ),
dep.artifactId(),
dep.version()
.replace( bom.version(), "${project.version}" ) ) )
.collect( joining(
"\n\n",
""
+ "\t<dependencyManagement>\n"
+ "\t\t<dependencies>\n",
""
+ "\n"
+ "\t\t</dependencies>\n"
+ "\t</dependencyManagement>" ) );

assertEquals( expected, actual );
}

private static List<PomData> collectArtifacts( PomData pom, List<PomData> modules ) {
if( "jar".equals( pom.packaging() ) ) {
modules.add( pom );
}
pom.modules().forEach( child -> collectArtifacts( child, modules ) );
return modules;
}
}
38 changes: 37 additions & 1 deletion doc/src/test/java/com/mastercard/test/flow/doc/PomData.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ class PomData {
private final Path dirPath;
private final String groupId;
private final String artifactId;
private final String version;
private final String packaging;
private final String name;
private final String description;
private final List<PomData> modules;
private final List<DepData> dependencies;
private final List<DepData> dependencyManagement;

/**
* @param parent The parent pom, or <code>null</code>
Expand All @@ -54,6 +56,9 @@ class PomData {
groupId = Optional.ofNullable( xpath.evaluate( "/project/groupId", doc ) )
.filter( s -> !s.isEmpty() )
.orElseGet( () -> parent.groupId() );
version = Optional.of( xpath.evaluate( "/project/version", doc ) )
.filter( s -> !s.isEmpty() )
.orElseGet( () -> parent.version() );
packaging = xpath.evaluate( "/project/packaging", doc );
name = xpath.evaluate( "/project/name", doc );
description = xpath.evaluate( "/project/description", doc );
Expand All @@ -75,6 +80,14 @@ class PomData {
for( int i = 0; i < dnl.getLength(); i++ ) {
dependencies.add( new DepData( xpath, dnl.item( i ), this ) );
}

dependencyManagement = new ArrayList<>();
NodeList dpnl = (NodeList) xpath.evaluate(
"/project/dependencyManagement/dependencies/dependency", doc,
XPathConstants.NODESET );
for( int i = 0; i < dpnl.getLength(); i++ ) {
dependencyManagement.add( new DepData( xpath, dpnl.item( i ), this ) );
}
}
catch( ParserConfigurationException
| SAXException
Expand Down Expand Up @@ -112,10 +125,17 @@ public String groupId() {
return groupId;
}

/**
* @return The project's version string
*/
public String version() {
return version;
}

/**
* @return The project's packaging value
*/
public String getPackaging() {
public String packaging() {
return packaging;
}

Expand All @@ -140,6 +160,13 @@ public Stream<DepData> dependencies() {
return dependencies.stream();
}

/**
* @return managed dependencies
*/
public Stream<DepData> dependencyManagement() {
return dependencyManagement.stream();
}

/**
* @return Project name
*/
Expand Down Expand Up @@ -175,6 +202,7 @@ public String toString() {
public static class DepData {
private final String groupId;
private final String artifactId;
private final String version;
private final String scope;
private final boolean optional;

Expand All @@ -193,6 +221,7 @@ public static class DepData {
groupId = gd;
}
artifactId = xpath.evaluate( "artifactId", n );
version = xpath.evaluate( "version", n );
scope = Optional.ofNullable( xpath.evaluate( "scope", n ) )
.filter( s -> !s.isEmpty() )
.orElse( "compile" );
Expand All @@ -215,6 +244,13 @@ public String artifactId() {
return artifactId;
}

/**
* @return The dependency version string
*/
public String version() {
return version;
}

/**
* @return commbined group and artifact IDs
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static String parentLink( PomData pom ) {
.collect( toSet() );

private static String javadocBadge( PomData pom ) {
if( "jar".equals( pom.getPackaging() ) && !NO_JAVADOC.contains( pom.artifactId() ) ) {
if( "jar".equals( pom.packaging() ) && !NO_JAVADOC.contains( pom.artifactId() ) ) {
return String.format( ""
+ "["
+ "![javadoc](https://javadoc.io/badge2/%s/%s/javadoc.svg)"
Expand Down
1 change: 1 addition & 0 deletions message/message-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
Expand Down
12 changes: 1 addition & 11 deletions message/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,10 @@
<module>message-xml</module>
</modules>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>

</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>

<plugin>
<!-- provides and enforces formatting -->
<groupId>net.revelc.code.formatter</groupId>
Expand Down