首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >maven原型:从现有属性值生成requiredProperty的默认值

maven原型:从现有属性值生成requiredProperty的默认值
EN

Stack Overflow用户
提问于 2016-10-19 15:45:04
回答 1查看 3.5K关注 0票数 10

我希望在原型-metadata.xml中为requiredProperty设置一个默认值,以便它基于从命令行传递的另一个属性。说

代码语言:javascript
复制
<requiredProperty key="customProperty">
        <defaultValue>${artifactId.toUpperCase()}</defaultValue>
</requiredProperty>

但是,当我使用生成的原型生成一个新项目时,并不是项目的artifactId获得了更高的感知,而是原型的artifactId。而当我把它改为

代码语言:javascript
复制
<requiredProperty key="customProperty">
        <defaultValue>${artifactId}</defaultValue>
</requiredProperty>

我得到了项目的artifactId,正如人们所期望的那样。

是否有一种方法可以根据另一个属性的值为自定义属性分配默认值?

注意:这只发生在交互模式下.

如何复制:

代码语言:javascript
复制
mvn -B archetype:generate -DartifactId=archet -DgroupId=com.example -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-archetype

由于某种原因,生成了archetype.xml。据我理解,这是一种旧的格式。代之以原型-metadata.xml(为了示例而最小化):

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="basic">

    <requiredProperties>
        <requiredProperty key="customPropertyUppercased">
            <defaultValue>${artifactId.toUpperCase()}</defaultValue>
        </requiredProperty>
        <requiredProperty key="customProperty">
            <defaultValue>${artifactId}</defaultValue>
        </requiredProperty>

        <!--JUnit version to use in generated project-->
        <requiredProperty key="junit-version">
            <defaultValue>4.12</defaultValue>
        </requiredProperty>
    </requiredProperties>

    <fileSets>
        <fileSet filtered="true" packaged="true">
            <directory>src/main/java</directory>
        </fileSet>
        <fileSet filtered="true" packaged="true">
            <directory>src/test/java</directory>
        </fileSet>
    </fileSets>

</archetype-descriptor>

./src/main/resources/archetype-resources/src/main/java/App.java:的模板

代码语言:javascript
复制
package ${groupId}.${artifactId};

/**

${customPropertyUppercased}

${customProperty}

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

使用生成的原型生成一个新项目

代码语言:javascript
复制
mvn archetype:generate -DgroupId=com.example -DartifactId=stacktest -DarchetypeArtifactId=archet -DarchetypeGroupId=com.example -DarchetypeCatalog=local

将所有属性设置为默认生成stacktest/src/main/java/com/example/App.java:

代码语言:javascript
复制
package com.example.stacktest;

/**

ARCHET

stacktest

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

因此,customPropertyUppercased是基于原型的artifactId,而customProperty是基于项目的artifactId。

EN

回答 1

Stack Overflow用户

发布于 2018-01-18 19:40:49

这是原型插件中的一个bug,记录在这里:https://issues.apache.org/jira/browse/ARCHETYPE-490

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40136085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档