Maven Reassembling Matching Test and Source Jars while maintaining DRY principal

30 views Asked by At

In a large multi module maven project we collect test jars from 30 modules to create a customer-test jar. We exclude the tests, keeping only the utility classes supporting tests.

I want to make the equivalent source jar but don't want to violate DRY (don't-repeat-yourself) by cutting and pasting my dependency unpack execution.

Baring refactoring the test utility classes into their own module (which probably stands as the best option for packaging but perhaps a less good option for on going development) what approach do you think makes sense?

My guess is that I should move away from maven-dependency-plugin + antrun-plugin to use an assembly where I pass in the classifier and include ending (class vs java). I wanted to check to see if someone had a better solution. If not, I'll post my solution upon completion.

Again, I realize that refactoring the test support routines into their own module is probably the superior option and I may follow that route.

Unpack Execution

          <execution>
            <id>dist profile: unpack classes for core-app-tests</id>
            <phase>prepare-package</phase>
            <goals><goal>unpack</goal></goals>
            <configuration>
              <overWrite>true</overWrite>
              <outputDirectory>${coreAppTestClassDir}</outputDirectory>
              <artifactItems>
                <artifactItem>
                  <groupId>...platform</groupId>
                  <artifactId>app</artifactId>
                  <classifier>tests</classifier>
                </artifactItem>
                ...

Vetting/Re-assmble via antrun

          <execution>
            <id>dist profile: Finalizing ${baseName} kit files</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <taskdef resource="ise/antelope/tasks/antlib.xml"
                         classpathref="maven.plugin.classpath"/>
                <taskdef resource="net/sf/antcontrib/antlib.xml"/>
                <delete>
                  <fileset dir="${coreAppTestClassDir}">
                    <include name="**/*Test.class"/>
                    <include name="**/*Test$*.class"/>
                    <include name="**/*java"/>
                    <include name="org/**"/>
                  </fileset>
                </delete>
                <jar destfile="${project.build.directory}/kit/sdk/lib-test/core-app-tests.jar" includes="**/*.class" excludes="org/**,META-INF/**" manifest="${project.build.directory}/classes/META-INF/MANIFEST.MF" >
                  <fileset dir="${coreAppTestClassDir}"/>
                </jar>
                ...
1

There are 1 answers

0
Peter Kahn On

If jar creation happens in its own module (which is should given the one-module-one-thing favored approach) this works:

  • Declare required test dependencies in test scope
  • Define exclusions as properties
  • Use maven-dependency-plugin unpack-dependencies goal to unpack specific classifiers and apply exclusions

The result is a classes and sources directory which can be collected via assembly plugin.

<dependencies>    
  <dependency>
    <groupId>...</groupId>
    <artifactId>amodule</artifactId>
    <classifier>tests</classifier>
    <scope>test</scope>
  </dependency>
  ...
</depedencies>


<properties>
  <coreTestSourcesDir>${kitTop}/sources</coreTestSourcesDir>              
  <classSourceExclusions>**/*Test.*,**/*Test$*.*,org/**</classSourceExclusions>
  ...
</properties>
...
<build>
  <plugins>    
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
         <id>Unpack Source</id>
         <phase>generate-resources</phase>
         <goals>
           <goal>unpack-dependencies</goal>
         </goals>
         <configuration>
            <excludeTransitive>true</excludeTransitive>
            <classifier>test-sources</classifier>
            <outputDirectory>${coreTestSourcesDir}</outputDirectory>
            <excludes>${classSourceExclusions}</excludes>
         </configuration>
       </execution>
        <execution>
         <id>Unpack Classes</id>
         <phase>generate-resources</phase>
         <goals>
           <goal>unpack-dependencies</goal>
         </goals>
         <configuration>
            <excludeTransitive>true</excludeTransitive>
            <classifier>tests</classifier>
            <outputDirectory>${project.build.directory}/classes</outputDirectory>
            <excludes>**/*.java,${classSourceExclusions}</excludes>
         </configuration>
       </execution>
      </executions>
    </plugin>