我正在使用Maven创建一个目录(本地Eclipse b3聚合器/ P2镜像)。
使用两个配置文件(A.target,B.target)创建此目录。
如果这些配置文件发生更改,则应创建新的镜像目录。
我想将配置文件的校验和添加到镜像目录的名称中(例如mirror_65eb5293d29682a3414e8889a84104ee)。
我可以使用'maven-assembly-plugin'来创建一个包含配置文件的jar,但是如何创建这个jar的校验和呢?
'checksum-maven-plugin'只将校验和写入csv文件,我不得不以某种方式解析。
有没有办法在属性中创建校验和,以便我可以用它将它添加到目录名中?
更新:
我现在的解决方案是使用shell脚本创建校验和并将其写入属性文件。
#!/bin/bash
os=$(uname)
if [[ "$os" == "Linux" ]];
then
checksum=$(cat {A,B}.target | md5sum | awk '{print $1}')
elif [[ "$os" == "Darwin" ]]; then
checksum=$(cat {A,B}.target | md5 -q)
else
echo "unknown OS ${os}"
exit 1
fi
printf "checksum=%s" "${checksum}" > ./checksum.properties
然后我可以使用 properties-maven-plugin 从这个属性文件中读取校验和。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>./checksum.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
答案 0 :(得分:1)
在调整配置文件的位置后使用以下内容:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>set-property</id>
<phase>process-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scripts>
<script>
import java.nio.file.Path
import java.nio.file.FileSystems
import java.nio.file.Files
import java.io.BufferedInputStream
import java.security.MessageDigest
import javax.xml.bind.annotation.adapters.HexBinaryAdapter
Path pathA = FileSystems.getDefault().getPath('src/main/resources/A.target')
Path pathB = FileSystems.getDefault().getPath('src/main/resources/B.target')
byte[] configsBytes = new byte[Files.size(pathA) + Files.size(pathB)]
InputStream inA = new BufferedInputStream(Files.newInputStream(pathA))
inA.read(configsBytes)
inA.close()
InputStream inB = new BufferedInputStream(Files.newInputStream(pathB))
inB.read(configsBytes, (int)Files.size(pathA), (int)Files.size(pathB))
inB.close()
MessageDigest md5 = MessageDigest.getInstance('MD5')
// from http://stackoverflow.com/a/12514417/1744774
String digest = new HexBinaryAdapter().marshal(md5.digest(configsBytes))
log.info(String.format('Setting property configs.checksum to %s', digest))
project.properties.setProperty('configs.checksum', digest)
// InvocationTargetException: No such property: configs
//log.info(String.format('Property configs.checksum=%s', "${configs.checksum}"))
</script>
</scripts>
</configuration>
</execution>
<execution>
<id>use-property</id>
<phase>process-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scripts>
<script>
// ...
log.info(String.format('Property configs.checksum=%s', "${configs.checksum}"))
// ...
</script>
</scripts>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.3</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
输出:
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building main 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- gmavenplus-plugin:1.5:execute (set-property) @ main ---
[INFO] Using Groovy 2.4.3 to perform execute.
[INFO] Setting property configs.checksum to 567A4B1644EA9ACB6FC047C1B4C5624B
[INFO]
[INFO] --- gmavenplus-plugin:1.5:execute (use-property) @ main ---
[INFO] Using Groovy 2.4.3 to perform execute.
[INFO] Property configs.checksum=567A4B1644EA9ACB6FC047C1B4C5624B
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.682 s
[INFO] Finished at: 2015-06-06T06:34:30+01:00
[INFO] Final Memory: 14M/111M
[INFO] ------------------------------------------------------------------------