Search code examples
javamavenpom.xmlmaven-jar-plugin

Bind jar:test-jar to package phase


I have a pom snippet containing the following:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.jason.Main</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>resources/</Class-Path>
                    </manifestEntries>
                </archive>
                <executions>
                    <execution>
                        <id>package-test-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </configuration>
        </plugin>

When i run "mvn package", the plugin does not execute and my test jar does not get created. When i run "mvn jar:test-jar" my jar gets created. Does anyone know why that might be?

Thank you kindly, Jason


Solution

  • You misplaced the <executions> block: it should be outside of the <configuration> one. As you wrote it, it means that you pass a structure of parameters named "executions" to the configuration of the maven jar plugin, which is simply ignored.

    Here's the correct configuration:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    ... your configuration data ...
                </configuration>
                <executions>
                    <execution>
                        <id>package-test-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    Note that specifying the "package" phase for your execution isn't necessary, since the maven jar plugin is attached by default to this phase.