【问题标题】:maven archetype: generate a default value for requiredProperty from a value of an existing propertymaven 原型:从现有属性的值生成 requiredProperty 的默认值
【发布时间】:2016-10-19 15:45:04
【问题描述】:

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

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

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

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

如预期的那样,我得到了项目的 artifactId。

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

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

如何重现:

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

由于某种原因,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>

./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!" );
    }
}

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

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

将所有属性设置为默认值会生成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。

【问题讨论】:

  • 你能发一个minimal reproducible example吗?确保您使用的是最新的 Maven Archetype 版本 (2.4)。
  • @Tunaki:完成,抱歉问题含糊不清。
  • 嗯,我无法重现。刚刚用你的设置做了一个小项目。修改后您是否更新了本地目录?确保您的根 POM(原型项目的 POM)是 according to this page,并且您在其上运行 mvn clean install
  • @Tunaki:我已将 pom.xml 更新为您提到的页面建议。我已经运行mvn clean install archetype:update-local-catalog。结果是一样的。
  • 嗯,我们正在进入“为我工作”场景:)。我看不出这会使用错误的工件 ID 的原因。在这一点上...也许您可以针对该问题创建一个 GitHub 存储库?

标签: maven maven-archetype


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    相关资源
    最近更新 更多