Maven原型:修改artifactId

Amb*_*ish 7 maven maven-archetype

在进行项目时,我的要求是创建一个模块。

该命令将类似于:

mvn archetype:generate \
  -DarchetypeCatalog=local \
  -DartifactId=test-module
Run Code Online (Sandbox Code Playgroud)

并且目标应该具有以下文件结构

test-module
|--pom.xml
`--src
   `--main
      |--install
      |  `--install.sh
      `--scripts
         `--test_module.sh
Run Code Online (Sandbox Code Playgroud)

我的整个目标是创建另一个从artifactId(例如artifactIdWithUnderscore)派生的变量,-用 underscope替换所有连字符_。这样我就可以使用更新后的变量来创建文件。

例子:

+------------------+---------------------------------+
|INPUT - artifactId|OUTPUT - artifactIdWithUnderscore|
+------------------+---------------------------------+
|    test-module   |          test_module            |
|       temp       |             temp                |
| test-temp-module |       test_temp_module          |
+------------------+---------------------------------+
Run Code Online (Sandbox Code Playgroud)

我尝试通过在archetype-metadata.xml中添加以下条目来创建一个新变量: artifactIdWithUnderscore

选项1:

<requiredProperty key="artifactIdWithUnderscore" >
  <defaultValue>${StringUtils.replace(${artifactId}, "-", "_")}</defaultValue>
</requiredProperty>
Run Code Online (Sandbox Code Playgroud)

输出:

${StringUtils.replace(${artifactId}, "-", "_")}
Run Code Online (Sandbox Code Playgroud)

选项2:

<requiredProperty key="artifactIdWithUnderscore" >
  <defaultValue>${artifactId.replaceAll("-", "_")}</defaultValue>
</requiredProperty>
Run Code Online (Sandbox Code Playgroud)

输出:

maven_archetype_script
Run Code Online (Sandbox Code Playgroud)

上述artifactId的值来自archetype项目本身的POM。

选项 3:

<requiredProperty key="artifactIdWithUnderscore" >
  <defaultValue>${artifactId}.replaceAll("-", "_")</defaultValue>
</requiredProperty>
Run Code Online (Sandbox Code Playgroud)

输出:

test-module.replaceAll("-", "_")
Run Code Online (Sandbox Code Playgroud)

请让我知道如何实现这一目标。

编辑

选项 4:

<requiredProperty key="artifactIdWithUnderscore" >
    <defaultValue>${__artifactId__.replaceAll("-", "_")}</defaultValue>
</requiredProperty>
Run Code Online (Sandbox Code Playgroud)

输出:

INFO: Null reference [template 'artifactIdWithUnderscore', line 1, column 1] : ${__artifactId__.replaceAll("-", "_")} cannot be resolved. 
Define value for property 'artifactIdWithUnderscore': ${__artifactId__.replaceAll("-", "_")}: :
Run Code Online (Sandbox Code Playgroud)

avd*_*dyk 1

选项 2 对我有用:

<requiredProperty key="artifactIdWithoutDash">
  <defaultValue>${artifactId.replaceAll("-", ".")}</defaultValue>
</requiredProperty>
Run Code Online (Sandbox Code Playgroud)

我可以使用 __artifactIdWithoutDash__.sh 有一个文件名来创建文件(在我的例子中是:some.letters.__artifactIdWithoutDash__.cfg)