【问题标题】:Maven archetype: Modify artifactIdMaven 原型:修改 artifactId
【发布时间】:2015-06-02 10:49:36
【问题描述】:

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

命令如下:

mvn archetype:generate \
  -DarchetypeCatalog=local \
  -DartifactId=test-module

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

test-module
|--pom.xml
`--src
   `--main
      |--install
      |  `--install.sh
      `--scripts
         `--test_module.sh

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

例子:

+------------------+---------------------------------+
|INPUT - artifactId|OUTPUT - artifactIdWithUnderscore|
+------------------+---------------------------------+
|    test-module   |          test_module            |
|       temp       |             temp                |
| test-temp-module |       test_temp_module          |
+------------------+---------------------------------+

我尝试通过在 archetype-metadata.xml

中添加以下条目来创建一个新变量作为 artifactIdWithUnderscore

选项 1:

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

输出:

${StringUtils.replace(${artifactId}, "-", "_")}

选项 2:

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

输出:

maven_archetype_script

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

选项 3:

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

输出:

test-module.replaceAll("-", "_")

请告诉我如何实现这一目标。

编辑

选项 4:

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

输出:

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

【问题讨论】:

  • 你能给我们看看你的archetype.properties文件吗?
  • @MattEckert,我没有archetype.properties 文件。使用archetype-metadata.xml

标签: maven maven-archetype


【解决方案1】:

选项 2 对我有用:

<requiredProperty key="artifactIdWithoutDash">
  <defaultValue>${artifactId.replaceAll("-", ".")}</defaultValue>
</requiredProperty>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-03
    • 2020-03-11
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2016-11-10
    • 2020-04-24
    • 2019-02-03
    相关资源
    最近更新 更多