Maven archetype: change artifact

While working on a project, I have to create a module.

The command will look like this:

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

      

And the target should have the following file structure

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

      

My goal is to create another variable derived from artifactId (say artifactIdWithUnderscore) replacing all hyphens -

with underscope _

. So that I can use the updated variable to create the 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 the following entries to 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 artifactId value comes from the POM project of the archetype 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("-", "_")}: :

      

+3


source to share


1 answer


Option 2 works for me:

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

      



I can use __artifactIdWithoutDash__.sh has the filename to create the file (in my case it was: some.letters .__ artifactIdWithoutDash __. Cfg)

0


source







All Articles