代码之家  ›  专栏  ›  技术社区  ›  Shekhar

通过maven生成uuid

  •  9
  • Shekhar  · 技术社区  · 16 年前

    我需要在maven pom.xml文件中设置一个属性,该属性应该是uuid。有人能告诉我吗

    将属性设置为UUID的最佳方法是什么?

    我正在使用启动gigaspaces的配置文件,gigaspaces需要组名

    希望是唯一的(UUID)。所以,在我的配置文件中,我想设置一个groupname属性值,

    应该为每个构建进行更改。我自己写了一个UuidGenerator插件,因为我没有找到任何插件。

    所以,我想知道这是怎么做到的?正在编写插件更好的选项,或者

    更容易的选择。

    谢谢,

    谢卡尔

    2 回复  |  直到 8 年前
        1
  •  7
  •   Cephalopod    16 年前

    首先,如果您的设置需要称为“组名”的东西,那么您可能应该提供一个有意义的值。如果必须是唯一的,可以附加一些生成的字符,如“myapplication-10937410”。而且,在我看来,使用UUID就像用大锤敲碎一个螺母。但这与你的实际问题无关,所以我建议的解决方案是:

    如果您还没有这样做,请创建一个maven插件(有一个原型)。添加此依赖项:

        <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);
        }
    
    }
    

    在你的实际项目中,使用pom ${uniqueGroupName} 在任何时候你需要它并像这样配置你的插件

    <build>
        <plugins>
            <plugin>
                <groupId>the.plugin.groupid</groupId>
                <artifactId>groupNameGenerator</artifactId>
                <executions>
                    <execution>
                        <goals><goal>generate</goal></goals>
                    </execution>
                </executions>
            <plugin>
    
        2
  •  9
  •   Gerold Broser    8 年前

    Arian solution (实现一个Maven插件)是IMO实现您的需求的一种干净方法(他的答案是+1)。

    但是如果你不打算在其他地方重用你的插件,一个快速的替代方法是使用 GMavenPlus plugin . 下面是一个示例,演示如何使用库中的Java类生成一些UUID并将其设置为属性:

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

    只需将插件绑定到gigaspace之前的某个阶段。

    第二次执行只是为了演示(显示属性):

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