如何将依赖项的测试jar包含到Maven项目的部署中?

Ale*_*ian 35 java deployment dependencies maven

我有两个项目,foofoo-web在该com.example组下.foo-web取决于foo.

为了能够在不依赖外部服务的情况下开发应用程序的UI部分,实现了虚拟DAO foo(它们返回静态数据,因此我们不必连接到数据库等).

我们被要求将虚拟类移动到src/test/java.这意味着它们不会部署foo.jar到从Web项目构建的战争中.我在maven网站上找到了这些说明,但它们似乎对我不起作用.

foopom.xml,我有:

        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <executions>
            <execution>
              <id>test-jar</id>
              <phase>test-compile</phase>
              <goals>
                <goal>test-jar</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

在跑步mvn install的时候foo-web,在foo我的目标中我会得到两个罐子:foo-1.0.0-SNAPSHOT.jarfoo-1.0.0-SNAPSHOT-tests.jar.它们都在本地maven存储库中安装得很好.

以前,foo-web依赖关系看起来像这样:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这将触发foo-1.0.0-SNAPSHOT.jar战争中的部署.现在,我想部署-testsjar,最好只用于"本地"配置文件.

我尝试过各种方法来做到这一点:

<profile>
    <id>local</id>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>foo</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>test-jar</type>
        </dependency>
    </dependencies>
</profile>
Run Code Online (Sandbox Code Playgroud)

这会导致使用不同的名称部署源jar:com.example-foo.jar并且不部署测试jar.我也尝试使用<classifier>而不是<type>依赖,但它仍然做同样的事情.我尝试在配置文件之外使用上面的依赖项(和其他配置文件一样),但它仍然表现相同.

如果我添加<type>到主依赖项(不添加其他依赖项),我将部署测试jar(与上面的名称相同),但源代码自然不会被部署.

与文档中所写内容的唯一区别在于未为测试依赖项指定范围.它只适用于test范围吗?我可以以某种方式以不同方式部署测试类.

我知道这个问题有点令人费解,如果有什么我可以澄清,请告诉我.

谢谢!


更新:

我尝试了几种方法,但它仍然无法正常工作.

我在foo项目中的maven-jar-plugin中添加了另一个执行(依赖,而不是主要的web项目),我希望强制maven在与主要jar相同的jar中编译测试类,并通过不同的分类器.我无法让它工作:

<execution>
  <id>local-build</id>
  <phase>package</phase>
  <goals>
    <goal>jar</goal>
  </goals>
  <configuration>
    <classifier>batman</classifier>
    <directory>${basedir}/src/test/java</directory> <!-- tried several variations here -->
    <includes>
        <include>**</include>
    </includes>
  </configuration>
</execution>
Run Code Online (Sandbox Code Playgroud)

jar是用batman分类器生成的,但我找不到任何方法让它在jar目标中包含测试类.

这样做,我意识到这不依赖于test-jar类型/ tests分类器/ test范围关系.当我试图指定我正在构建的新jar时,除了主要的jar之外,我得到了与尝试包含-testsjar 时相同的行为.我检查了本地maven存储库,并且依赖项目中的所有jar都安装得很好,所以问题是主项目的依赖项解析.

TL;博士

如果你可以在多个分类器中包含相同的依赖关系,那么一切都归结为问题.从我看到的直到现在,答案是否定的 - com.example-foo当使用不同的分类器多次指定相同的依赖时,我总是得到jar.

rai*_*tin 48

最好在第一个模块中配置maven pom文件

<project>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
Run Code Online (Sandbox Code Playgroud)

在此之后,mvn安装/发布也将部署工件 foo-1.0.0-SNAPSHOT-tests.jar

然后使用分类器配置测试jar的依赖关系(如其他响应中建议的那样)

<dependency>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <type>test-jar</type>
    <!-- uncomment if needed in test scope only
         <scope>test</scope>
    -->
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • 这里的文档https://maven.apache.org/pom.html说"这种类型通常与使用的包装相对应,但情况并非总是如此.例如jar,ejb-client和test-jar." 他们可能希望保留分类器"以区分由相同POM构建但在内容上不同的工件",例如jdk版本.但在实践中可能会奏效. (4认同)
  • 我认为使用`<classifier> tests </ classifier>`比`<type> test-jar </ type>`更清楚. (2认同)