Generating UUID through Maven

There is https://github.com/stevespringett/maven-uuid-generator which exposes a uuid for the build as ${project.build.uuid}. You can use it like

<plugins>
    <plugin>
        <groupId>us.springett</groupId>
        <artifactId>maven-uuid-generator</artifactId>
        <version>1.0.1</version>
        <executions>
            <execution>
                <phase>initialize</phase>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

Arian's solution (implementing a maven plugin) is IMO a clean way to implement your requirement (and +1 for his answer).

But if you don't plan to reuse your plugin somewhere else, a quick alternative would be to hack the pom using the GMavenPlus plugin. Here is an example showing how to do so using a Java class from a library to generate some uuid and set it as a property:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow</groupId>
  <artifactId>Q3984794</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.safehaus.jug</groupId>
        <artifactId>jug</artifactId>
        <version>2.0.0</version>
        <!-- the classifer is important!! -->
        <classifier>lgpl</classifier>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    ...
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmaven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>set-custom-property</id>
            <phase>initialize</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <classpath>
                <element>
                  <groupId>org.safehaus.jug</groupId>
                  <artifactId>jug</artifactId>
                  <classifier>lgpl</classifier>
                </element>
              </classpath>
              <source>
                import org.safehaus.uuid.UUIDGenerator
                def uuid = UUIDGenerator.getInstance().generateRandomBasedUUID()
                project.properties.setProperty('groupName', uuid.toString())
              </source>
            </configuration>
          </execution>
          <execution>
            <id>show-custom-property</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <source>
                def props = project.properties
                props.each {key, value -&gt; println key + "=" + value}
              </source>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Just bind the plugin to a phase prior to the gigaspaces stuff.

The second execution is just there for demonstration purpose (to show the properties):

$ mvn generate-resources 
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3984794 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- gmaven-plugin:1.3:execute (set-custom-property) @ Q3984794 ---
[INFO] 
[INFO] --- gmaven-plugin:1.3:execute (show-custom-property) @ Q3984794 ---
downloadSources=true
downloadJavadocs=true
project.reporting.outputEncoding=UTF-8
project.build.sourceEncoding=UTF-8
groupName=814ff1a5-a102-426e-875c-3c40bd85b737
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

First of all, if your set up requires something called "group name", you probably should provide a meaningful value. If it has to be unique, you can append some generated characters, like "MyApplication-10937410". Also, using a UUID seems to me like using a sledge-hammer to crack a nut. But this is independent of your actual problem, so here is the solution I propose:

If you have not already done so, create a maven plugin (there's an archetype for that). Add this dependency:

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-project</artifactId>
        <version>2.2.1</version>
    </dependency>

This is how your MOJO should look like:

/**
 * Goal which generates a group name.
 *
 * @goal generate
 * @phase initialize
 */
public class GroupNameGeneratorMojo extends AbstractMojo {

    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    @Override
    public void execute() throws MojoExecutionException {
        String groupName = ... ;
        project.getProperties().setProperty("uniqueGroupName", groupName);
    }

}

In your actual projects pom, use ${uniqueGroupName} whereever you need it and configure your plugin like this

<build>
    <plugins>
        <plugin>
            <groupId>the.plugin.groupid</groupId>
            <artifactId>groupNameGenerator</artifactId>
            <executions>
                <execution>
                    <goals><goal>generate</goal></goals>
                </execution>
            </executions>
        <plugin>

Tags:

Java

Maven