3

我正在尝试为当前在我的 Maven 构建中的所有工件创建一个 SHA-1 校验和,并将这些校验和写入文件。目前我正在研究使用具有内置校验和生成器的 maven-install-plugin。

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

问题是这个解决方案似乎只适用于一个工件,而不是我构建中的所有工件。我也无法在构建时指定所有工件,我需要一个包含所有工件的解决方案。

附加信息:我使用 Jenkins 作为构建工具,所以如果有 Jenkins 解决方案,我也可以接受。不过我自己还没有找到。

4

1 回答 1

3

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.

于 2016-01-21T22:47:45.057 回答