Search code examples
javamavenmaven-3

How to include only certain test packages in a maven test-jar & all non test code in a non test jar?


I have a maven module named synapse.

main
  java
    package1
      A.java

test
  java
    common
      C.java
    package1
      ATest.java

Can someone let me know if in maven I can create a non-executable jar containing all non-test code (main/) and another test-jar containing only the code under the package test/java/common?

I tried the below

<build>
  <plugins>
       <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <skip>true</skip>
        </configuration>
       </plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
         <version>3.3.0</version>
         <configuration>
          <includes>
            <include>common/**</include>
          </includes>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>test-jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
     </plugin>
  </plugins>
</build>

I am observing that the test-jar is getting created as expected containing only the code from test/java/common but the non-executable, non-test jar does not contain the code from main/java/package1.

  • Maven Version - 3.8.4
  • Java 17

Solution

  • Your configuration filter is being applied to the default jar as well as the test jar. Move your configuration into the scope of the execution:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>common/**</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>