Search code examples
mavenmaven-archetype

Creating an Archetype From POM: Property is Missing


I created a Maven archetype and want to create an example project of it in my repository, which seems to be an unusual use-case.

Since I don't want to create the archetype manually, I added the following execution:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-archetype-plugin</artifactId>
    <configuration>
        <archetypeGroupId>my.company.archetypes</archetypeGroupId>
        <archetypeVersion>${project.version}</archetypeVersion>

        <groupId>org.acme</groupId>
        <version>0.1.2-SNAPSHOT</version>
        <interactiveMode>false</interactiveMode>
    </configuration>
    <executions>
        <execution>
            <id>archetype-one</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <archetypeArtifactId>archetype-one</archetypeArtifactId>
                <artifactId>one</artifactId>
                <package>org.acme.one</package>
            </configuration>
        </execution>
    </executions>
</plugin>

This leads to the following exception:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:generate (archetype-one) on project examples: Archetype my.company.archetypes:archetype-one:0.9.0-SNAPSHOT is not configured
[ERROR]     Property groupId is missing.
[ERROR]     Property artifactId is missing.
[ERROR]     Property package is missing.

Which is just not true, since I've defined all of these. At least the IDE proposes these tags on that position. Moving the configuration tags around doesn't help either.

So I checked the source code of generate, and lo and behold, the target GAVs aren't present.

How do I define them when generating an archetype directly from another pom.xml?


Solution

  • So I ended up using an entirely different Maven plug-in:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>archetype-one</id>
                <goals>
                    <goal>exec</goal>
                </goals>
                <phase>package</phase>
                <configuration>
                    <executable>mvn</executable>>
                    <arguments>
                        <argument>archetype:generate</argument>
    
                        <argument>-DarchetypeGroupId=my.company.archetypes</argument>
                        <argument>-DarchetypeVersion=${project.version}</argument>
                        <argument>-DgroupId=org.acme</argument>
                        <argument>-Dversion=0.1.2-SNAPSHOT</argument>
                        <argument>-DinteractiveMode=false</argument>
    
                        <argument>-DarchetypeArtifactId=archetype-one</argument>
                        <argument>-DartifactId=one</argument>
                        <argument>-Dpackage=org.acme.one</argument>
                    </arguments>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    And since I wanted to generate multiple archetypes, I put the first six arguments into a general <configuration> block, and appended the remaining three with <arguments combine.children="append">.