[ACCEPTED]-Share test resources between maven projects-maven-2

Accepted answer
Score: 43

Just use jar:test-jar and declare the resulting JAR 5 as a dependency (refer to this guide for more details). And 4 while I don't understand the problem of 3 having resources and classes in this jar, you 2 can always exclude all .class files:

<project>
  <build>
    <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-jar-plugin</artifactId>
       <version>2.2</version>
       <executions>
         <execution>
           <goals>
             <goal>test-jar</goal>
           </goals>
         </execution>
       </executions>
       <configuration> 
         <excludes>
           <exclude>**/*.class</exclude>
         </excludes>
       </configuration> 
     </plugin>
    </plugins>
  </build>
</project>

And to use 1 it:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>com.myco.app</groupId>
      <artifactId>foo</artifactId>
      <version>1.0-SNAPSHOT</version>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>
  </dependencies>
  ...
</project>
Score: 6

Accepted answer helped me, but it's not 4 quite accurate in case you need regular 3 jar of same project as well. It will delete 2 *.class files from both jars.
Settings below 1 translates to something like:

  • create me 2 jars, 1 regular, 1 test;
  • remove *.class files, but only from test jar

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>test-jar</goal>
                </goals>
                <configuration>
                    <excludes>
                        <exclude>**/*.class</exclude>
                    </excludes>
                </configuration>
            </execution>
        </executions>
    </plugin>
Score: 2

Using maven-dependency-plugin we can put the resource needed in 4 the right directory, only modifying the 3 pom on dependent project is needed:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-dependency-plugin</artifactId>
   <executions>
      <execution>
         <phase>generate-test-resources</phase>
         <goals>
            <goal>unpack</goal>
         </goals>
         <configuration>
            <artifactItems>
               <artifactItem>
                  <groupId>dependeeGroupId</groupId>
                  <artifactId>dependeeArtifactId</artifactId>
                  <version>dependeeVersion</version>
                  <type>test-jar</type>
                  <outputDirectory>${project.build.directory}/test-classes</outputDirectory>
                  <includes>resourceNeeded.txt</includes>
                  <overWrite>true</overWrite>
               </artifactItem>
            </artifactItems>
         </configuration>
      </execution>
   </executions>
</plugin>

type is used 2 to get test resource
outputDirectory is used to put the 1 resource usable in tests

Documentation here: https://maven.apache.org/plugins/maven-dependency-plugin/unpack-mojo.html

Score: 0

There is already a goal to build a test jar from 9 maven.

Assuming you need something a little 8 more flexible, you can use the jar plugin 7 to package your test resources and run that 6 goal with the main package goal with something 5 like this.

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <classifier>test-resources</classifier>
              <includes>
                <include>**/*.whatever-you-want</include>
              </includes>
            </configuration>
          </execution>
        </executions>
      </plugin>

Whatever you want bundled would 4 be added to the project-name-version-test-resources.jar 3 when the jar goal is run.

You could then 2 include it in a project via the same dependency 1 you use above.

More Related questions