在Maven项目之间共享测试资源

有一个明确的解决方案,可以使用maven-antrun-plugin插件的test-jar目标在maven项目之间共享通用测试代码(请参见此处)。

我需要对测试资源做类似的事情,特别是,我希望在测试过程中项目A的测试资源在项目B的类路径中可用。

对于项目A,需要声明:

<!-- Package and attach test resources to the list of artifacts: -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <jar destfile="${project.build.directory}/test-resources.jar">
                        <fileset dir="${project.basedir}/test-resources" />
                    </jar>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>attach-artifact</goal>
            </goals>
            <configuration>
                <artifacts>
                    <artifact>
                        <file>${project.build.directory}/test-resources.jar</file>
                        <type>jar</type>
                        <classifier>test-resources</classifier>
                    </artifact>
                </artifacts>
            </configuration>
        </execution>
    </executions>
</plugin>

在项目B中,它将是正常依赖项:

<dependency>
    <groupId>myproject.groupId</groupId>
    <artifactId>myartifact</artifactId>
    <version>1.0-SNAPSHOT</version>
    <classifier>test-resources</classifier>
    <scope>test</scope>
</dependency>

问题:是否在所有情况下都有效? 是否可以在没有maven-antrun-plugin的情况下打包资源(使用更多的“轻量级”插件)?

dma_k asked 2020-08-12T10:34:38Z
4个解决方案
37 votes

只需使用.class并将声明的JAR声明为依赖项(有关更多详细信息,请参阅本指南)。 而且,尽管我不了解在此jar中具有资源和类的问题,但是您始终可以排除所有.class文件:

<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>

并使用它:

<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>
Pascal Thivent answered 2020-08-12T10:34:49Z
1 votes

接受的答案对我有所帮助,但是如果您也需要同一个项目的常规jar,它就不太准确。 它将从两个jar中删除* .class文件。
以下设置转换为:

  • 给我制造2个罐子,1个常规,1个测试;
  • 删除* .class文件,但仅从测试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>
miracle_the_V answered 2020-08-12T10:35:22Z
0 votes

已经有一个目标可以从Maven构建测试罐。

假设您需要一些更灵活的工具,则可以使用jar插件来打包测试资源,并使用类似以下内容的主包目标来运行该目标。

      <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>

运行jar目标时,您想要捆绑的所有内容都会添加到project-name-version-test-resources.jar中。

然后,您可以通过上面使用的相同依赖项将其包含在项目中。

sal answered 2020-08-12T10:35:56Z
0 votes

使用type,我们可以将所需的资源放在正确的目录中,只需要修改依赖项目上的pom:

<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用于获取测试资源
outputDirectory用于放置可在测试中使用的资源

此处的文档:[https://maven.apache.org/plugins/maven-dependency-plugin/unpack-mojo.html]

Alexandre Lagane answered 2020-08-12T10:36:29Z
translate from https://stackoverflow.com:/questions/2247199/share-test-resources-between-maven-projects