Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running tests after packaging

One of my projects needs a pretty complex setup for the resulting JAR file, so I'd like to run a test after the package phase to make sure the JAR contains what it should.

How do I do that with Maven 2?

like image 500
Aaron Digulla Avatar asked Feb 25 '23 08:02

Aaron Digulla


2 Answers

You can use the surefire-plugin for this. what you need to do is associate a phase with an execution (see below). You will need to change the phase to be whatever you want it to be in your case one after the package phase.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skip>true</skip>
            </configuration>
            <executions>
                <execution>
                    <id>unittests</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <skip>false</skip>
                        <includes>
                            <include>**/**/**/*Test.java</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
like image 85
Paul Whelan Avatar answered Mar 04 '23 06:03

Paul Whelan


Convert your project into a multi-module build. In the first module, build your original project. In the second module, add a dependency to the first.

This will add the first JAR to the classpath.


Update by OP: This works but I had to add this to my POM:

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${version.maven-surefire-plugin}</version>
            <configuration>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
        </plugin>

The important part is <useSystemClassLoader>false</useSystemClassLoader>. Without this, my classpath only contained a couple of VM JARs plus the surefire bootstrap JAR (which contains the test classpath in the MANIFEST.MF). I have no idea why this test classpath isn't visible from the classes loaded from it.

like image 26
martin Avatar answered Mar 04 '23 06:03

martin