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

5

18

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.

Iceskate answered 2/11, 2010 at 22:24 Comment(0)
K
17

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.)

Kinnie answered 20/9, 2013 at 11:49 Comment(2)
The only problem with this approach is you can seem to only do one goal rather than multiple (e.g. install site)Hummocky
This answer would be more useful if it included the goal.txt file example, since now what is "above" is below.Gaynell
C
3

beside the the approach of using the maven-invoker-plugin, we are using a different approach. With the help of the Maven Verifier you can test your maven plugins and archetypes easily. Just add the following dependency into your pom of your maven test project:

<dependency>                                
  <groupId>org.apache.maven.shared</groupId>
  <artifactId>maven-verifier</artifactId>   
  <version>1.2</version>                    
</dependency>                               

Now you are able to use

org.apache.maven.it.Verifier

into your normal JUnit Tests. With the verifier you can run maven goals and some assertions about the result. For a complete example just check out the integration test maven modules of our javascript-archetypes: https://github.com/akquinet/javascript-archetypes

Crick answered 21/1, 2011 at 14:1 Comment(0)
C
2

I was struggling a little with this myself, and figured that when using current v2.3 of the maven-archetype-plugin, in addition to a src/test/resources/projects/first/goal.txt, one also needs a src/test/resources/projects/first/archetype.properties containing something like this:

sourceEncoding=UTF-8
groupId=integrationtest.group
artifactId=integrationtest.artifactId
version=1.0.0-SNAPSHOT
package=org.eclipse.xtend.xtend-archetype.integrationtest
packageInPathFormat=org/eclipse/xtend/xtend-archetype/integrationtest

This pull request illustrates a complete working example.

Carswell answered 30/3, 2015 at 12:28 Comment(4)
I can't seem to get this to work. Do you need to do more than add the files? I was expecting the archetype to run the integration test during mvn clean compile but it seems to skip over them entirely.Gaynell
@Gaynell the complete working example linked above does work. If I were you I would just look around, e.g. on GitHub, for other examples, and I'm sure you'll figure it!Carswell
I did end up getting something working, however I did have to add the <phase> to the archetype integration-test goal which I'm not sure if that is a symptom of another problem with maven not automatically executing these tests. How do you typically invoke these tests? Maven seems to completely ignore my goals.txt file. I had migrated my archetype to use archetype-metadata.xml over archetype.xml and that does not seem to have made a difference.Gaynell
@Gaynell integration-test phase comes only after compile phase. If you want to run archetype integration tests as part of the build, you need to run either install or deploy. Reference: maven.apache.org/guides/introduction/…Intosh
I
1

I see the archetype:integration-test goal, but it doesn't seem to be doing what I want.

Unless I misunderstood what you want, the archetype:integration-test goal seems to be a very good solution:

Execute the archetype integration tests, consisting of a creation of a project from the current archetype with defined properties and optional comparison with reference copy. An IT consists of a directory in src/test/resources/projects containing:

  • goal.txt (content actually not used, but future version should interpret it as a goal to run against the generated project: see ARCHETYPE-334),
  • archetype.properties with properties for project generation,
  • optional reference/ directory containing a reference copy of the expected project created from the IT.

According to the above description, this goals allows precisely to run Integration Test(s) to check a project generated with the current archetype against an expected result and this looks like a clean, simple, self contained way to test an archetype.

Why is this approach not satisfying? What did I miss?

Inchoate answered 3/11, 2010 at 20:26 Comment(2)
The archetype:integration-test does not actually build or test the generated code, which is what I want. That's what I now realize feature 334 is about.Iceskate
@mes5k: Ahhhhh, I think I understand now what you want to do i.e. running tests from the generated project. That wasn't clear for me in the original question.Inchoate
E
0

I guess that would be a scenario for a continuous integration server like hudson.

You'd define a job that

  1. empties a directory (shell script)
  2. creates a new project based on the archetype (mvn archetype:generate)
  3. runs the project (mvn package)

While this could probably somehow be fit into one maven lifecycle, it would feel like an awful mess. Use CI instead.

Edeline answered 3/11, 2010 at 8:14 Comment(1)
This would work, but I think as a fallback solution. The problem is that I wouldn't find out my template code was broken until after it had been committed, something I'd rather avoid. Also, invoking maven within maven is exactly what the maven-release-plugin does, so this isn't something unusual.Iceskate

© 2022 - 2024 — McMap. All rights reserved.