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

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

问题描述

我在 com.example 组下有两个项目,foofoo-web.foo-web 依赖于 foo.

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

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

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

我们需要将虚拟类移动到 src/test/java.这意味着它们不会使用 foo.jar 部署到从 Web 项目构建的战争中.我在Maven 站点,但它们似乎对我不起作用.

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.

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>

当在 foo-web 上运行 mvn install 时,在 foo 的目标中我会得到两个 jars:foo-1.0.0-SNAPSHOT.jarfoo-1.0.0-SNAPSHOT-tests.jar.它们都安装在本地 Maven 存储库中.

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.

之前,foo-web 依赖是这样的:

Before, the foo-web dependency looked like this:

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

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

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>

这会导致使用不同的名称部署源 jar:com.example-foo.jar,并且不会部署测试 jar.我还尝试在依赖项中使用 而不是 ,但它仍然这样做.我尝试在配置文件之外使用上述依赖项(以及其他依赖项),但它的行为仍然相同.

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.

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

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.

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

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.

谢谢!

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

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

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

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>

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

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.

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

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.

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

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.

推荐答案

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

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>

在此之后 mvn install/release 还将部署一个工件 foo-1.0.0-SNAPSHOT-tests.jar

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

然后使用分类器配置对测试 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天全站免登陆