'Maven archetype: Modify artifactId
While working on project, my requirement is to create a module.
The command will be like:
mvn archetype:generate \
-DarchetypeCatalog=local \
-DartifactId=test-module
And the target should have following file structure
test-module
|--pom.xml
`--src
`--main
|--install
| `--install.sh
`--scripts
`--test_module.sh
My whole goal is to create another variable derived from artifactId (say artifactIdWithUnderscore) replacing all hyphens - by underscope _. So that I can use the updated variable to create file(s).
Example:
+------------------+---------------------------------+
|INPUT - artifactId|OUTPUT - artifactIdWithUnderscore|
+------------------+---------------------------------+
| test-module | test_module |
| temp | temp |
| test-temp-module | test_temp_module |
+------------------+---------------------------------+
I tried to create a new variable as artifactIdWithUnderscore by adding following entries in archetype-metadata.xml
Option 1:
<requiredProperty key="artifactIdWithUnderscore" >
<defaultValue>${StringUtils.replace(${artifactId}, "-", "_")}</defaultValue>
</requiredProperty>
Output:
${StringUtils.replace(${artifactId}, "-", "_")}
Option 2:
<requiredProperty key="artifactIdWithUnderscore" >
<defaultValue>${artifactId.replaceAll("-", "_")}</defaultValue>
</requiredProperty>
Output:
maven_archetype_script
The above value of artifactId is coming from the POM of archetype project itself.
Option 3:
<requiredProperty key="artifactIdWithUnderscore" >
<defaultValue>${artifactId}.replaceAll("-", "_")</defaultValue>
</requiredProperty>
Output:
test-module.replaceAll("-", "_")
Please let me know how I can achieve this.
EDIT:
Option 4:
<requiredProperty key="artifactIdWithUnderscore" >
<defaultValue>${__artifactId__.replaceAll("-", "_")}</defaultValue>
</requiredProperty>
Output:
INFO: Null reference [template 'artifactIdWithUnderscore', line 1, column 1] : ${__artifactId__.replaceAll("-", "_")} cannot be resolved.
Define value for property 'artifactIdWithUnderscore': ${__artifactId__.replaceAll("-", "_")}: :
Solution 1:[1]
Option 2 work for me:
<requiredProperty key="artifactIdWithoutDash">
<defaultValue>${artifactId.replaceAll("-", ".")}</defaultValue>
</requiredProperty>
I can use __artifactIdWithoutDash__.sh has a file name to create the file (in my case it was: some.letters.__artifactIdWithoutDash__.cfg)
Solution 2:[2]
In my case (which is somehow similar) a slightly modified option 2 did the trick:
<requiredProperty key="package-with-slash">
<defaultValue>${package.replace(".", "/")}</defaultValue>
</requiredProperty>
Note, that the replacement is done by the apache velocity engine and the originally used replaceAll uses a regex expression.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | avdyk |
| Solution 2 | Krabat |
