我需要在maven pom.xml文件中设置一个属性,该属性应为UUID。有人能告诉我如何将属性设置为UUID的最佳方法吗?我正在使用一个启动gigaspaces的配置文件,而gigaspaces需要组名,我希望它是唯一的(uuid)。因此,在我的配置文件中,我想设置一个groupName属性值,每次构建都应更改。由于我没有找到任何插件,因此我自己编写了一个UUIDGenerator插件。所以,我正在寻找如何实现这一点?编写插件是否是更好的选择,还是有更简单的选择?谢谢,Shekhar
<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 -> println key + "=" + value}
</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
只需将插件绑定到 gigaspaces 之前的阶段即可。
第二次执行仅用于演示目的(显示属性):
$ 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] ------------------------------------------------------------------------ ...
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.2.1</version>
</dependency>
/**
* 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);
}
}
${uniqueGroupName}
并像这样配置插件。<build>
<plugins>
<plugin>
<groupId>the.plugin.groupid</groupId>
<artifactId>groupNameGenerator</artifactId>
<executions>
<execution>
<goals><goal>generate</goal></goals>
</execution>
</executions>
<plugin>
有一个https://github.com/stevespringett/maven-uuid-generator,它公开了一个UUID用于构建,如${project.build.uuid}
。您可以像这样使用它
<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>
mvn build "-Dcommand.line.prop=hello again"
,然后你可以像这样使用它:line.prop=${command.line.prop}
。 - amra