tonio
tonio

Reputation: 493

Maven archetype not using properties to create module names

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?

Upvotes: 8

Views: 9867

Answers (4)

sgpalit
sgpalit

Reputation: 2686

archetype-metadata.xml and the pom.xml of the sub-module:

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

  <modules>
    <module name="${rootArtifactId}-ria" id="${rootArtifactId}-ria" dir="__rootArtifactId__-ria">
      <fileSets>
        <fileSet filtered="true" packaged="true">
          <directory>src/main/java</directory>
          <includes>
            <include>**/*.java</include>
          </includes>
        </fileSet>
        <fileSet filtered="true" packaged="true">
            <includes>
                <include>pom.xml</include>
            </includes>
        </fileSet>
      </fileSets>
    </module>
  </modules>


</archetype-descriptor>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>${groupId}</groupId>
    <artifactId>${rootArtifactId}</artifactId>
    <version>${version}</version>
  </parent>

  <artifactId>${artifactId}</artifactId>
  <packaging>war</packaging>

</project>

Update for directory structure: archetype

Upvotes: 4

BPS
BPS

Reputation: 1678

After generating the archetype from a project, edit the file target\generated-sources\archetype\src\main\resources\META-INF\maven\archetype-metadata.xml.

Add

    <fileSet filtered="true" packaged="true" encoding="UTF-8">
      <directory></directory>
      <includes>
        <include>pom.xml</include>
      </includes>
    </fileSet>

This will make it filter your pom file! Then run mvn install again.

Upvotes: 1

tonio
tonio

Reputation: 493

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.

Upvotes: 4

tonio
tonio

Reputation: 493

Looking inside maven-archetype sources (version 2.2), seems the only possible value to use is rootArtifactId extracted from DefaultFilesetArchetypeGenerator

   while (subprojects.hasNext() ) {

        ModuleDescriptor project = subprojects.next();

        File moduleOutputDirectoryFile = new File( outputDirectoryFile
             , StringUtils.replace( project.getDir()
                   , "__rootArtifactId__"
                   , rootArtifactId 
                   ));
        ...........

Upvotes: 2

Related Questions