Skip to content

Commit

Permalink
add goal to create and attach bom checksums
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwarz-eitco-de committed May 24, 2024
1 parent f2e30c8 commit c2c4985
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>de.eitco.cicd</groupId>
<artifactId>bom-maven-plugin</artifactId>
<version>5.0.1-SNAPSHOT</version>
<version>5.1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<developers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,21 @@ public abstract class AbstractBillOfMaterialsMojo extends AbstractMojo {

protected Artifact makeBomArtifact() {

Artifact result = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, "pom", null);
Artifact result = makeArtifact("pom");

result.setFile(targetFile);

return result;
}

protected Artifact makeArtifact(String extension) {

return artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, extension, null);
}

protected Artifact makeSignatureArtifact() {

Artifact result = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, "pom" + signatureExtension, null);
Artifact result = makeArtifact("pom" + signatureExtension);

result.setFile(makeSignatureFile());

Expand Down
50 changes: 50 additions & 0 deletions src/main/java/de/eitco/cicd/bom/CreateBomChecksumsMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.eitco.cicd.bom;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.install.DualDigester;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

@Mojo(name = "checksums", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
public class CreateBomChecksumsMojo extends AbstractBillOfMaterialsMojo {

private final DualDigester digester = new DualDigester();

@Override
public void execute() throws MojoExecutionException {

File bomFile = makeBomArtifact().getFile();

digester.calculate(bomFile);

attachChecksum(bomFile, ".md5", digester.getMd5());
attachChecksum(bomFile, ".sha1", digester.getSha1());
}

private void attachChecksum(File bomFile, String extension, String checksum) throws MojoExecutionException {

try {

File checksumFile = new File(bomFile.getAbsolutePath() + extension);

Files.writeString(checksumFile.toPath(), checksum);

Artifact artifact = makeArtifact("pom" + extension);

artifact.setFile(checksumFile);
project.addAttachedArtifact(artifact);

} catch (IOException e) {

throw new MojoExecutionException(e);
}

}


}

0 comments on commit c2c4985

Please sign in to comment.