Use Maven to compile multiple JARs during build

46 views Asked by At

I have some (trivially small) classes I'd like to compile to JARs during a Maven build, so that these JARs can be used when testing the application (the application loads JAR files, they are purely for test purposes).

I've got something working but I'm struggling to adjust the name of the final JAR files which makes me think I'm going down the wrong route.

Take the following pom.xml:

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.me</groupId>
    <artifactId>my-artifact</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <compileSourceRoots>${project.basedir}/ClassDir</compileSourceRoots>
                            <outputDirectory>${project.build.directory}/CompiledClassDir</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                        <configuration>
                            <testClassesDirectory>${project.build.directory}/CompiledClassDir</testClassesDirectory>
                            <outputDirectory>${project.build.directory}/CompiledJARDir</outputDirectory>
                            <!-- <finalName>MyJar</finalName> -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

During the test-compile phase, this will compile everything in ClassDir, and then package that into a JAR file in target/CompiledJARDir.

By default, this is producing an artifact with name my-artifact-0.0.1-SNAPSHOT-tests.jar - which is pretty cumbersome as whenever the version of the artifact changes, all the test references that load that JAR need to change.

Using the (undocumented) finalName parameter, I'm able to control it a little (e.g. to produce MyJar-tests.jar, but a warning is produced as finalName isn't supposed to be dynamic. I think that this property is actually affecting the final name of the project as a whole, rather than being specific to this execution.

Is there another way to adjust the name of the maven-jar-plugin? Or another way to compile a JAR?

I think the most Maven-centric approach to my problem is probably have submodules for each of the JARs I want to compile, but I'm unsure how to bind those submodules to the test-compile phase so that they are fully-packaged before any tests in my-artifact run.

0

There are 0 answers