Skip to content

Commit

Permalink
automated bom
Browse files Browse the repository at this point in the history
  • Loading branch information
therealryan committed Jun 7, 2024
1 parent 9e85d22 commit 3447820
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 51 deletions.
86 changes: 48 additions & 38 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,121 +12,131 @@

<dependencyManagement>
<dependencies>

<dependency>
<groupId>${project.groupId}</groupId>
<groupId>com.mastercard.test.flow</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<dependency>
<groupId>com.mastercard.test.flow</groupId>
<artifactId>validation-junit4</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.mastercard.test.flow</groupId>
<artifactId>validation-junit5</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
87 changes: 87 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,87 @@
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(), artifact.artifactId(), artifact.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(), dep.artifactId(), dep.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() )
.orElse( "${project.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

0 comments on commit 3447820

Please sign in to comment.