Share test resources between maven projects

前端 未结 4 1677
太阳男子
太阳男子 2020-11-29 04:07

There is a clear solution for sharing the common test code between maven projects using test-jar goal of maven-jar-plugin plugin (see here).

<
相关标签:
4条回答
  • 2020-11-29 04:22

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

    Assuming you need something a little more flexible, you can use the jar plugin to package your test resources and run that goal with the main package goal with something 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 be added to the project-name-version-test-resources.jar when the jar goal is run.

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

    0 讨论(0)
  • 2020-11-29 04:25

    Just use jar:test-jar and declare the resulting JAR as a dependency (refer to this guide for more details). And while I don't understand the problem of having resources and classes in this jar, you 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 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>
    
    0 讨论(0)
  • 2020-11-29 04:28

    Using maven-dependency-plugin we can put the resource needed in the right directory, only modifying the 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 to get test resource
    outputDirectory is used to put the resource usable in tests

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

    0 讨论(0)
  • 2020-11-29 04:42

    Accepted answer helped me, but it's not quite accurate in case you need regular jar of same project as well. It will delete *.class files from both jars.
    Settings below 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>
    
    0 讨论(0)
提交回复
热议问题