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

bad*_*hop 8 maven maven-archetype

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

<requiredProperty key="customProperty">
        <defaultValue>${artifactId.toUpperCase()}</defaultValue>
</requiredProperty>
Run Code Online (Sandbox Code Playgroud)

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

<requiredProperty key="customProperty">
        <defaultValue>${artifactId}</defaultValue>
</requiredProperty>
Run Code Online (Sandbox Code Playgroud)

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

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

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

如何重现:

mvn -B archetype:generate -DartifactId=archet -DgroupId=com.example -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-archetype
Run Code Online (Sandbox Code Playgroud)

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

<?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>
Run Code Online (Sandbox Code Playgroud)

一个模板在 ./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!" );
    }
}
Run Code Online (Sandbox Code Playgroud)

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

mvn archetype:generate -DgroupId=com.example -DartifactId=stacktest -DarchetypeArtifactId=archet -DarchetypeGroupId=com.example -DarchetypeCatalog=local
Run Code Online (Sandbox Code Playgroud)

将所有属性设置为默认生成 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!" );
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Chr*_*der 4

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