2

I'm creating a directory (local Eclipse b3 Aggregator/P2 mirror) with Maven.

Two configuration files (A.target, B.target) are used two create this directory.

If these configuration files change a new mirror directory should be created.

I want to add the checksum of the configuration files to the name of the mirror directory (e.g. mirror_65eb5293d29682a3414e8889a84104ee).

I could use the 'maven-assembly-plugin' to create a jar containing the configuration files but how do I create the checksum of this jar?

The 'checksum-maven-plugin' only writes the checksum into a csv file which I would have to parse somehow.

Is there a way to create the checksum into a property so that I can use it to add it to the name of the directory?

UPDATE:

My solution for now is to use a shell script to create the checksum and write it to a properties file.

#!/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

Then I can use the properties-maven-plugin to read the checksum from this properties file.

<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>

1 Answers1

1

Use the following after adapting your configuration files' location(s):

<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>

Output:

[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] ------------------------------------------------------------------------
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107