maven 原型:从现有属性的值生成 requiredProperty 的默认值 [英] maven archetype: generate a default value for requiredProperty from a value of an existing property

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

问题描述

我想为 archetype-metadata.xml 中的 requiredProperty 设置默认值,使其基于从命令行传递的另一个属性.说

I want to set a default value for a requiredProperty in archetype-metadata.xml such that it is based on another property passed from command line. Say

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

但是,当我使用生成的原型生成新项目时,不是项目的 artifactId 被大写,而是原型的 artifactId.而当我将其更改为

However, when I use the resulting archetype to generate a new project, it is not the project's artifactId that gets uppercased, but rather the archetype's artifactId. Whereas when I change it to

<requiredProperty key="customProperty">
        <defaultValue>${artifactId}</defaultValue>
</requiredProperty>

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

I get the project's artifactId as one would expect.

有没有办法根据另一个属性的值为自定义属性分配默认值?

Is there a way to assign a default value for a custom property based on the value of another property?

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

mvn -B archetype:generate -DartifactId=archet -DgroupId=com.example -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-archetype

出于某种原因,archetype.xml 被生成.这是我的理解,它是一种旧格式.将其替换为 archetype-metadata.xml(为了示例而最小化):

For some reason, archetype.xml gets generated. It is my understanding, it is an old format. Replaced it with archetype-metadata.xml (minimized for the sake of an example):

<?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 中的模板:

package ${groupId}.${artifactId};

/**

${customPropertyUppercased}

${customProperty}

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

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

Generating a new project using the resulting archetype

mvn archetype:generate -DgroupId=com.example -DartifactId=stacktest -DarchetypeArtifactId=archet -DarchetypeGroupId=com.example -DarchetypeCatalog=local

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

with all properties set to default produces stacktest/src/main/java/com/example/App.java:

package com.example.stacktest;

/**

ARCHET

stacktest

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

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

So, the customPropertyUppercased is based on the archetype's artifactId, while customProperty is based on the project's artifactId.

推荐答案

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

This is a bug in the archetype plugin, and is documented here: https://issues.apache.org/jira/browse/ARCHETYPE-490

这篇关于maven 原型:从现有属性的值生成 requiredProperty 的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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