Skip to content

Commit

Permalink
add test for dependency property goal and property list goal
Browse files Browse the repository at this point in the history
add output file parameter to property list goal
make dependency properties goal also create properties for transitive dependencies
  • Loading branch information
jschwarz-eitco-de committed Jun 26, 2024
1 parent 743684a commit afd398c
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 15 deletions.
60 changes: 60 additions & 0 deletions src/it/dependency-properties/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.eitco.build.essentials.test</groupId>
<artifactId>cmn-build-essentials-test</artifactId>
<version>@project.version@</version>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.17.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>build-utilities-maven-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<outputFile>target/properties.txt</outputFile>
<addFiles>true</addFiles>
</configuration>
<executions>
<execution>
<goals>
<goal>dependencies-version-properties</goal>
<goal>list-properties</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
16 changes: 16 additions & 0 deletions src/it/dependency-properties/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.nio.charset.StandardCharsets
import java.nio.file.Files



File propertyInfo = new File(new File("$basedir"), 'target/properties.txt')

assert propertyInfo.isFile()

String fileContent = new String(Files.readAllBytes(propertyInfo.toPath()), StandardCharsets.UTF_8)

assert fileContent.contains('maven.dependency.org.springframework.boot:spring-boot-starter:jar.version = 3.2.5')
assert fileContent.contains('maven.dependency.com.fasterxml.jackson.core:jackson-annotations:jar.version = 2.17.1')
assert fileContent.contains('maven.dependency.com.google.guava:guava:jar.version = 25.1-jre')
assert fileContent.contains('maven.dependency.org.springframework:spring-core:jar.version = 6.1.6')

Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,33 @@
*/
package de.eitco.cicd.build.utilities;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;

import java.util.Set;
import java.io.File;
import java.util.List;

/**
* This goal generates properties, containing the version and file system path of every direct dependency the
* current project has. For every dependency, two properties are generated: 'maven.dependency.&lt;dependency-conflict-id&gt;.file'
* set to the absolute file system path of the dependency and 'maven.dependency.&lt;dependency-conflict-id&gt;.version'
* set to the version of the dependency.
* The dependency conflict id is constructed as follows: &lt;groupId&gt;:&lt;artifactId&gt;:&lt;type&gt;[&lt;classifier&gt;]
*
*/
@Mojo(name = "dependencies-version-properties", requiresDependencyResolution = ResolutionScope.TEST, defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true)
public class DependenciesVersionPropertiesMojo extends AbstractMojo {
Expand All @@ -37,23 +47,104 @@ public class DependenciesVersionPropertiesMojo extends AbstractMojo {
@Parameter(property = "dependencies-version-properties.skip", defaultValue = "false")
private boolean skip;

@Parameter(property = "dependencies-version-properties.add-files", defaultValue = "false")
private boolean addFiles;

@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;

@Parameter(defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true)
private List<ArtifactRepository> remoteRepositories;

@Component(hint = "default")
private DependencyGraphBuilder dependencyGraphBuilder;

@Component
private ArtifactResolver artifactResolver;

@Override
public void execute() {
public void execute() throws MojoExecutionException {

if (skip) {

getLog().info("skipping dependencies version properties ");
return;
}

Set<Artifact> artifacts = project.getDependencyArtifacts();
ProjectBuildingRequest buildingRequest =
new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());

buildingRequest.setProject(project);

try {

DependencyNode dependencyNode = dependencyGraphBuilder.buildDependencyGraph(buildingRequest, null);

dependencyNode.accept(new DependencyNodeVisitor() {
@Override
public boolean visit(DependencyNode node) {

getLog().debug("setting properties maven.dependency." + node.getArtifact().getDependencyConflictId() + ".[file/version]");

if (addFiles) {

File file = node.getArtifact().getFile();

if (file == null) {

ProjectBuildingRequest buildingRequest =
new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());

buildingRequest.setRemoteRepositories(remoteRepositories);

for (Artifact artifact : artifacts) {
ArtifactCoordinate coordinate = getArtifactCoordinate(node);

getLog().debug("setting properties maven.dependency." + artifact.getDependencyConflictId() + ".[file/version]");
try {

project.getProperties().setProperty("maven.dependency." + artifact.getDependencyConflictId() + ".file", artifact.getFile().getAbsolutePath());
project.getProperties().setProperty("maven.dependency." + artifact.getDependencyConflictId() + ".version", artifact.getVersion());
ArtifactResult artifactResult = artifactResolver.resolveArtifact(buildingRequest, coordinate);

file = artifactResult.getArtifact().getFile();

} catch (ArtifactResolverException e) {

getLog().warn("unable to resolve artifact " + coordinate, e);
}
}

if (file != null) {

project.getProperties().setProperty("maven.dependency." + node.getArtifact().getDependencyConflictId() + ".file", file.getAbsolutePath());
}
}
project.getProperties().setProperty("maven.dependency." + node.getArtifact().getDependencyConflictId() + ".version", node.getArtifact().getVersion());


return true;
}

@Override
public boolean endVisit(DependencyNode node) {
return true;
}
});


} catch (DependencyGraphBuilderException e) {

throw new MojoExecutionException(e);
}

}

private static ArtifactCoordinate getArtifactCoordinate(DependencyNode node) {

DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
coordinate.setGroupId(node.getArtifact().getGroupId());
coordinate.setArtifactId(node.getArtifact().getArtifactId());
coordinate.setVersion(node.getArtifact().getVersion());
coordinate.setClassifier(node.getArtifact().getClassifier());
coordinate.setExtension(node.getArtifact().getType());

return coordinate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
*/
package de.eitco.cicd.build.utilities;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Map;

/**
Expand All @@ -30,8 +36,14 @@ public class ListPropertiesMojo extends AbstractMojo {
@Parameter
private String prefix;

/**
* This parameter sets the file to write the properties to. If not set the properties will simply be logged.
*/
@Parameter
private File outputFile;

@Override
public void execute() {
public void execute() throws MojoExecutionException {


StringBuilder builder = new StringBuilder();
Expand All @@ -46,7 +58,24 @@ public void execute() {
builder.append(property.getKey()).append(" = ").append(property.getValue()).append("\n");
}

getLog().info("project properties : \n" + builder);
if (outputFile != null) {

try {

FileUtils.forceMkdir(outputFile.getParentFile());

Files.writeString(outputFile.toPath(), builder.toString());

} catch (IOException e) {

throw new MojoExecutionException(e);
}


} else {

getLog().info("project properties : \n" + builder);
}

}
}

0 comments on commit afd398c

Please sign in to comment.