Maven archetype not using properties to create module names

tonio picture tonio · Oct 11, 2012 · Viewed 8.4k times · Source

I created an archetype where you can set the moduleName (or expect to) using a required property moduleName, here is the archetype metadata xml (reduced, which I also tried with similar results)

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="modules-archetype">

  <requiredProperties>
    <requiredProperty key="moduleName">
    </requiredProperty>
  </requiredProperties>

  <modules>
    <module id="modules-${moduleName}-api" 
            dir="modules-__moduleName__-api" 
            name="modules-${moduleName}-api">
      <fileSets>
        <fileSet encoding="UTF-8">
          <directory>src/main/java</directory>
        </fileSet>
      </fileSets>
    </module>
  </modules>

</archetype-descriptor>

After installing and generating, the moduleName value is not used in the directory name or the artifactid, the resuting values are

For the directory:        project/module-__moduleName__-api
For the pom/artifactId:   module-${moduleName}-api

The value is replaced correctly on some other files of the project, so no spelling problems I guess.

I've seen a lot of similar things, but all of them using rootArtifactId, and in fact if I use rootArtifactId (as the starting part of the name) it works as expected.

Not able to find a similar problem around, any idea why is it not working, or how to make it work?

Answer

tonio picture tonio · Oct 18, 2012

I found a reasonable work-around.

Seems the only place where you can only use the rootArtifactId is the <module> element in the archetype descriptor, but in every other place you can use the moduleName property without any trouble.

What I did:

Use ${moduleName} in

  • artifactId in module's pom
  • some default dependencies
  • The module definition in the main pom (<module>module-${moduleName}</module>)
  • etc.

Use __moduleName__ in

  • package name in src/main/java & resources

In the artifact descriptor

  • Use a fix module name something like module-sample1 & module-sample2 (and of course name the directories inside the artifact resources the same way).

Things to fix after the project is created

  1. The modules' directory name
  2. Take out the module elements in the main pom that contains module-sample[12]

All of this in a ant script embedded in a postcreate-pom.xml to be run after project creation.

And it worked smoothly.

Hope its useful for someone.

thanks to everybody that just take the time to read my question, tonio.