具有自定义属性的Maven原型生成 [英] Maven archetype generate with custom properties

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

问题描述

这里是新手问题,我有一个使用.properties文件从Java项目创建的Maven原型.我希望能够为原型中的变量生成动态值,但我不确定该怎么做.目前,我正在关注此maven指南,但我似乎无法复制他们所做的事情.特别是在指南的底部,在该处它们将String值替换为新值.

Newbie question here, I have a maven archetype that I'm creating from a java project using a .properties file. I want to be able to generate dynamic values for variables within the archetype and I'm not sure how to do that. Currently I'm following this maven guide and but I can't seem to reproduce what they've done. Specifically at the bottom of the guide where they replace the String value with a new value.

这是我正在使用的属性文件:

Here's the properties file I'm using:

archetype.groupId=com.mycompany
archetype.artifactId=sample-project
archetype.version=1.0

archetype.languages=java

myParam=something

我检查了archetype-metadata.xml文件,并且尝试加载的属性位于其中:

I checked my archetype-metadata.xml file and the properties I'm trying to load are in there:

<requiredProperty key="myParam">
  <defaultValue>new value here</defaultValue>
</requiredProperty>

但是,我的所有类均未加载新值.我缺少步骤来替换参数的默认值,并将其替换为prop文件中指定的新值吗?

However, none of my classes have the new value loaded. Is there a step that I'm missing that replaces the default value of the parameter and replaces it with the new value specified in the prop file?

提前谢谢!

推荐答案

您可能是正确的,在创建原型之后和创建项目之前,没有任何片段出现App.java或其他类的样子.

You are probably right, there is not any fragment how a App.java or others class look likes after creating archetype and before creating a project.

假设您不知道如何使用自定义属性,并且最终的App.java类需要看起来像这样(仅作为示例):

Assuming you dont know how to use your custom properties, and your final App.java class need to look like this (just an example):

package my.new.group;

/**
 * Hello world!
 *
 */
public class App
{
    String value = "something";
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

您的archetype.properties文件看起来像您粘贴的一样,您的原型App.java类(在安装原型之前!)需要看起来像这样:

And your archetype.properties file looks like this you pasted, your Archetype App.java class (before installing archetype!) need to look like this:

package ${archetype.groupId};

/**
 * Hello world!
 *
 */
public class ${archetype.artifactId}
{
    String value = "${myParam}";
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

如果您已准备好这样的文件,则可以轻松地mvn install此原型

if you got prepared file like this, you can easly mvn install this archetype

然后,如果您创建项目,则将要求您从属性文件中确认一个值.就这样.应该使用archetype.properties中的值

then if u create project you will be ask to confirm a values from properties file. And thats all. Value from archetype.properties should be applied

这篇关于具有自定义属性的Maven原型生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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