首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在maven项目之间共享测试资源

在maven项目之间共享测试资源
EN

Stack Overflow用户
提问于 2010-02-12 03:39:31
回答 4查看 19.7K关注 0票数 44

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

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

对于项目A,需要声明:

代码语言:javascript
复制
<!-- 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中,它将是正常的依赖关系:

代码语言:javascript
复制
<dependency>
    <groupId>myproject.groupId</groupId>
    <artifactId>myartifact</artifactId>
    <version>1.0-SNAPSHOT</version>
    <classifier>test-resources</classifier>
    <scope>test</scope>
</dependency>

问:它应该在所有情况下都有效吗?有没有可能在没有maven-antrun-plugin的情况下打包资源(使用更轻量级的插件)?

EN

Stack Overflow用户

发布于 2019-08-30 15:48:20

使用maven-dependency-plugin,我们可以将需要的资源放在正确的目录下,只需要修改依赖项目上的pom:

代码语言:javascript
复制
<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

票数 2
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2247199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档