如何将依赖项的测试jar包含到Maven项目的部署中? [英] How do I include a dependency's test jar into a Maven project's deployment?

查看:162
本文介绍了如何将依赖项的测试jar包含到Maven项目的部署中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个项目, foo foo-web com.example 组。 foo-web 取决于 foo



能够开发应用程序的UI部分而不依赖于外部服务,在 foo 中实现了虚拟DAO(它们返回静态数据,所以我们不必连接到数据库等) )。



我们需要将虚拟类移动到 src / test / java 。这意味着它们不会从网络项目构建的战争中部署到 foo.jar 。我发现 这些说明 在maven网站,但它们似乎不适合我。



foo pom.xml 我有:

 < plugin> 
< artifactId> maven-jar-plugin< / artifactId>
<执行>
< execution>
< id> test-jar< / id>
< phase> test-compile< / phase>
< goals>
< goal> test-jar< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>

在$ $ c上运行 mvn install $ c> foo-web ,目标是 foo 我会得到两个jar: foo-1.0.0 -SNAPSHOT.jar foo-1.0.0-SNAPSHOT-tests.jar 。它们都可以在本地的maven存储库中安装。



之前, foo-web 依赖关系如下所示:

 <依赖关系> 
< groupId> com.example< / groupId>
< artifactId> foo< / artifactId>
< version> 1.0.0-SNAPSHOT< / version>
< / dependency>

这将触发 foo-1.0.0-SNAPSHOT的部署。 jar 在战争中。现在,我还要部署 -tests jar,最好只用于本地配置文件。



我尝试以各种方式做到这一点:

 < profile> 
< id> local< / id>
<依赖关系>
<依赖关系>
< groupId> com.example< / groupId>
< artifactId> foo< / artifactId>
< version> 1.0.0-SNAPSHOT< / version>
< type> test-jar< / type>
< / dependency>
< / dependencies>
< / profile>

这导致使用不同的名称部署源jar: com。 example-foo.jar 并且不部署测试jar。我还尝试在依赖关系中使用< classifier> 而不是< type> ,但它仍然是一样的。我试图在配置文件之外(与其他人一起使用)上面的依赖关系,但它仍然是相同的。



如果我添加键入> 到主依赖关系(不添加其他依赖关系)我得到部署测试jar(与上述相同的名称),但源自然没有被部署。



与文档中写的内容的唯一区别是没有为测试依赖关系指定范围。它只适用于测试范围吗?我可以以某种方式部署测试课程。



我知道这个问题有点复杂,请让我知道,如果有什么我可以澄清。



谢谢!






更新:





我将另外一个执行文件添加到了$ $ c $中的maven-jar-plugin中c> foo 项目(依赖关系,而不是主要的Web项目),我希望强制maven在同一个jar中编译测试类,并以不同的分类器。我无法让它工作:

 < execution> 
< id> local-build< / id>
< phase> package< / phase>
< goals>
< goal> jar< / goal>
< / goals>
< configuration>
< classifier>蝙蝠侠< / classifier>
< directory> $ {basedir} / src / test / java< / directory> <! - 尝试了几种变体 - >
< includes>
< include> **< / include>
< / includes>
< / configuration>
< / execution>

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



做这个,我意识到这不取决于 test-jar type / tests classifier / test 范围关系。当我试图指定我正在构建的新的jar,除了主要的,我有同样的行为,当试图包括 -tests jar。我检查了本地的maven仓库,从依赖项目的所有罐子都安装得很好,所以问题是主要项目的依赖性解决。



tl; dr



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

解决方案

更好地在您的第一个模块中配置maven pom文件

 code><项目> 
< 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>
<执行>
< execution>
< goals>
< goal> test-jar< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>

此后,mvn install / release也将部署一个工件 foo-1.0 .0-SNAPSHOT-tests.jar



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

 <依赖关系> 
< groupId> com.example< / groupId>
< artifactId> foo< / artifactId>
< version> 1.0.0-SNAPSHOT< / version>
< type> test-jar< / type>
<! - 如果测试范围内需要,则取消注释
< scope> test< / scope>
- >
< / dependency>


I have two projects, foo and foo-web under the com.example group. foo-web depends on foo.

To be able to develop the UI part of the application without depending on external services, dummy DAOs were implemented in foo (they return static data so we don't have to connect to databases etc).

We were required to move the dummy classes to src/test/java. This means that they don't get deployed with foo.jar to the war built from the web project. I found these instructions on the maven site, but they don't seem to work for me.

In foo's pom.xml I have:

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

When running mvn install on foo-web, in the target of foo I'd get two jars: foo-1.0.0-SNAPSHOT.jar and foo-1.0.0-SNAPSHOT-tests.jar. They both get installed fine in the local maven repository.

Before, the foo-web dependency looked like this:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

And that would trigger the deployment of foo-1.0.0-SNAPSHOT.jar in the war. Now, I want to also deploy the -tests jar, preferably only for a "local" profile.

I tried in various ways to do this:

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

This causes the source jar to be deployed with a different name: com.example-foo.jar and does not deploy the test jar. I also tried using <classifier> instead of <type> in the dependency, but it still does the same. I tried using the above dependency outside of the profile (alongside the others), but it still behaves the same.

If I add the <type> to the main dependency (without adding the other dependency) I get the test jar deployed (with the same name as above), but the source, naturally, does not get deployed.

The only difference from what's written in the documentation is the fact that the scope is not specified for the test dependency. Does it only work for the test scope? Can I somehow deploy the test classes differently.

I know the question's a bit convoluted, please let me know if there's something I can clarify.

Thanks!


Update:

I tried it in several more ways but it still won't work.

I added another execution to the maven-jar-plugin in the foo project (the dependency, not the main web project) in which I hoped to force maven to compile the test classes in the same jar as the main ones and reference the big bundle by a different classifier. I couldn't get it to work:

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

The jar was generated with the batman classifier, but I couldn't find any way to get it to include test classes in the jar goal.

Doing this, I realized that this does not depend on the test-jar type/tests classifier/test scope relationship. When I tried to specify the new jar I'm building besides the main one, I got the same behavior as when trying to include the -tests jar. I checked the local maven repository and both all jars from the dependent project are getting installed fine, so the problem is the main project's dependency resolution.

tl;dr

Everything boils down to the question if you can include the same dependency with multiple classifiers. From what I saw until now, the answer is no - I always get the com.example-foo jar when specifying the same dependency multiple times with different classifiers.

解决方案

Better to configure maven pom file in your first module

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

After this a mvn install/release will also deploy an artifact foo-1.0.0-SNAPSHOT-tests.jar

Then configure the dependency on the test jar with classifier (like suggested in other responses)

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

这篇关于如何将依赖项的测试jar包含到Maven项目的部署中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆