javamaven-2archetypes

How can I test a maven archetype that I've just created?


I've created a few archetypes for a project that work fine for now, but I'd like to be able to verify that the code generated from the archetypes continues to work in the future.

What I'd like is a phase of the archetype build that takes the archetype just created, runs mvn archetype:generate on it, and then runs mvn verify on the generated code to verify that the generated code is actually OK. If need be I'll write my own mojo to do this, but wanted to see if a solution already exists. I see the archetype:integration-test goal, but it doesn't seem to be doing what I want.


Solution

  • UPDATE 2013: This is now much easier than the other answers suggest.

    https://issues.apache.org/jira/browse/ARCHETYPE-334 was completed in Aug 2011

    To use, simply place the word install inside the goal.txt file mentioned above, and the tests from the project you are archetyping will be invoked as part of a normal build. (And/or verify in the case of OP.)

    However, if you new to making archetypes be aware that this popular mini-guide is out of date and, while it will work for making an archetype it will not work for having archetype integration tests run. You should instead be creating an archetype-metadata.xml file as described here. (This is much nicer to work with as well, as it uses file sets!)

    Also note these integration tests do not respond to -DskipTests but this can be remedied as follows:

    <build>
      <plugins>
    
        <plugin>
          <artifactId>maven-archetype-plugin</artifactId>
          <version>2.2</version>
          <configuration>
            <skip>${skipTests}</skip>
          </configuration>        
        </plugin>
    
      </plugins>
    </build>
    

    (Although this looks like it skips the entire plugin, it actually works, probably because it falls back to a legacy mode; whereas I could not find any successful way to skip just the integration-test goal execution using code above.)