Search code examples
mavenmaven-jar-plugin

How do I avoid an empty test JAR in Maven?


I have Maven configured to create test JARs for all builds (snapshot and release) like so:

        <plugin>
            <!-- generate test-jar artifacts for creating test-test dependencies 
                across modules -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

That works but there is an odd behavior which I'd like to fix: Maven now tries to create a test JAR for modules with packaging pom (i.e. the parent POM). It's just a small nuisance but is there an easy way to fix this?

It doesn't create a main JARs for these modules. Maybe it's a bug in the test-jar goal?


Solution

  • skipIfEmpty does the trick, kudos to wemu:

            <plugin>
                <!-- generate test-jar artifacts for creating test-test dependencies 
                    across modules -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
    
                        <configuration>
                            <skipIfEmpty>true</skipIfEmpty>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    Documentation: https://maven.apache.org/plugins/maven-jar-plugin/test-jar-mojo.html#skipIfEmpty