Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

java - Generating UUID through Maven

I need to set a property in maven pom.xml file which should be a UUID. Can anybody tell me

what is the best possible way to set a property to UUID?

I am using a profile which launch the gigaspaces and gigaspaces requires group name which I

want to be unique(uuid). So, in my profile I want to set a groupName property value which

should change for each build. I wrote a UUIDGenerator plugin myself as I didn't found any.

So, I am looking How can this be achieved? Is writing a plugin better option or there is an

easier option.

Thanks,

Shekhar

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...