通过Maven生成UUID [英] Generating UUID through Maven

查看:630
本文介绍了通过Maven生成UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在maven pom.xml文件中设置一个属性,该文件应该是UUID。任何人都可以告诉我

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

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

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

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

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

想要是唯一的(uuid)。因此,在我的个人资料中,我想设置一个groupName属性值,

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

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

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

更容易的选项。

谢谢,

Shekhar

推荐答案

首先,如果你的设置需要一个叫做组名的东西,你可能应该提供一个有意义的价值。如果它必须是唯一的,您可以附加一些生成的字符,例如MyApplication-10937410。另外,在我看来,使用UUID就像使用大锤来破解坚果。但这与你的实际问题无关,所以这是我建议的解决方案:

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:

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

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>

这就是你的MOJO应该是这样的:

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

}

在您的实际项目中使用pom,使用 $ {uniqueGroupName} 无论您需要它还是像这样配置您的插件

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>

这篇关于通过Maven生成UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆