John Marshall
John Marshall

Reputation: 324

Create a checksum for all maven artifacts

I'm trying to create an SHA-1 checksum for ALL the artifacts currently in my maven build and write these checksums to a file. Currently I am looking into using the maven-install-plugin which has a built in checksum generator.

https://maven.apache.org/plugins/maven-install-plugin/examples/installing-checksums.html

The issue is that this solution appears to only be for one artifact and not all the artifacts in my build. I can't specify all the artifacts at build time either, I need a solution that includes all the artifacts.

Additional info: I'm using Jenkins as a build tool so if there is a Jenkins solution I'd be okay with that as well. I haven't found one myself though.

Upvotes: 4

Views: 7387

Answers (1)

A_Di-Matteo
A_Di-Matteo

Reputation: 27812

You could use the Checksum Maven Plugin and its artifacts goal.

I just tested the following sample POM:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>create-jar-something</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <classifier>something</classifier>
                    </configuration>
                </execution>
                <execution>
                    <id>create-jar-else</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <classifier>else</classifier>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>net.ju-n.maven.plugins</groupId>
            <artifactId>checksum-maven-plugin</artifactId>
            <version>1.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>artifacts</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Note: I am creating three artefacts from this project: the default one and two additional jars with classifiers.
Thanks to the simple and single execution of the artifacts goal, I got the following content as part of the build (mvn clean verify):

enter image description here

Via the additional individualFiles configuration entry, you can also decide whether to store the checksums in separated files (default) or appended in one single file, while via the individualFilesOutputDirectory configuration entry you can redirect their creation to another location, if required.

Hence, this approach could suit your need, since it will automatically detect any generated artifact and calculate the checksum for it.

Also note in case of multi-modules projects that as per documentation:

Is NOT inherited by default in multi-project builds.

In such a case you can place the snippet above in the pluginManagement section of your parent/aggregator pom and just re-declare the plugin (but not its execution nor its version) in the submodules where you want to activate the behavior.

Hence, in your parent/aggregator you could have something like:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>net.ju-n.maven.plugins</groupId>
            <artifactId>checksum-maven-plugin</artifactId>
            <version>1.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>artifacts</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

And in the modules you want to enable this behavior you would then only specify:

<build>
    <plugins>
        <plugin>
            <groupId>net.ju-n.maven.plugins</groupId>
            <artifactId>checksum-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Last but not least: if part of a CI build on Jenkins, you could also consider a dedicated profile for this behavior and activate it on the build but not as part of the default build.

Upvotes: 3

Related Questions