多模块项目中的 Maven 测试依赖关系

发布于 2024-08-11 04:46:10 字数 931 浏览 2 评论 0原文

我使用 Maven 构建一个多模块项目。我的模块 2 依赖于编译范围内的模块 1 src 以及测试范围内的模块 1 测试。

模块 2 -

   <dependency>
       <groupId>blah</groupId>
       <artifactId>MODULE1</artifactId>
       <version>blah</version>
       <classifier>tests</classifier>
       <scope>test</scope>
   </dependency>

这很好用。假设我的模块 3 依赖于 Module1 src 并在编译时进行测试。

模块 3 -

   <dependency>
       <groupId>blah</groupId>
       <artifactId>MODULE1</artifactId>
       <version>blah</version>
       <classifier>tests</classifier>
       <scope>compile</scope>
   </dependency>

当我运行 mvn clean install 时,我的构建运行到模块 3,在模块 3 处失败,因为它无法解析模块 1 测试依赖项。然后我单独在模块 3 上执行 mvn install,返回并在我的父 pom 上运行 mvn install 以使其构建。我该如何解决这个问题?

I use maven to build a multi module project. My module 2 depends on Module 1 src at compile scope and module 1 tests in test scope.

Module 2 -

   <dependency>
       <groupId>blah</groupId>
       <artifactId>MODULE1</artifactId>
       <version>blah</version>
       <classifier>tests</classifier>
       <scope>test</scope>
   </dependency>

This works fine. Say my module 3 depends on Module1 src and tests at compile time.

Module 3 -

   <dependency>
       <groupId>blah</groupId>
       <artifactId>MODULE1</artifactId>
       <version>blah</version>
       <classifier>tests</classifier>
       <scope>compile</scope>
   </dependency>

When I run mvn clean install, my build runs till module 3, fails at module 3 as it couldn't resolve the module 1 test dependency. Then I do a mvn install on module 3 alone, go back and run mvn install on my parent pom to make it build. How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2024-08-18 04:46:15

我对您想要执行的操作有疑问,但我假设您想在另一个项目(module1)中重用您为项目(module1)创建的测试。正如 使用附加测试指南:

请注意,本指南的先前版本建议使用 tests 而不是 test-jar。虽然这目前适用于某些情况,但如果调用安装之前的生命周期阶段,则在测试 JAR 模块和任何使用者的反应器构建期间,它无法正常工作。在这种情况下,Maven 不会从反应器构建的输出中解析测试 JAR,而是从本地/远程存储库中解析。显然,存储库中的 JAR 可能已过时或完全丢失,从而导致构建失败(参见 MNG- 2045)。

因此,首先,要将已编译的测试打包在 JAR 中并部署它们以供一般重用,请按如下方式配置 maven-jar-plugin

<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>
     </plugin>
    </plugins>
  </build>
</project>

然后,照常安装/部署测试 JAR 工件(使用 <代码>mvn安装或mvn部署)。

最后,要使用测试 JAR,您应该指定具有指定类型的 test-jar 的依赖项:

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

I have a doubt about what you are trying to do but but I'll assume you want to reuse the tests that you have created for a project (module1) in another. As explained in the note at the bottom of the Guide to using attached tests:

Note that previous editions of this guide suggested to use <classifier>tests</classifier> instead of <type>test-jar</type>. While this currently works for some cases, it does not properly work during a reactor build of the test JAR module and any consumer if a lifecycle phase prior to install is invoked. In such a scenario, Maven will not resolve the test JAR from the output of the reactor build but from the local/remote repository. Apparently, the JAR from the repositories could be outdated or completely missing, causing a build failure (cf. MNG-2045).

So, first, to package up compiled tests in a JAR and deploy them for general reuse, configure the maven-jar-plugin as follows:

<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>
     </plugin>
    </plugins>
  </build>
</project>

Then, install/deploy the test JAR artifact as usual (using mvn install or mvn deploy).

Finally, to use the test JAR, you should specify a dependency with a specified type of test-jar:

<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>
み零 2024-08-18 04:46:15

关于我对帕斯卡问题的评论,我认为我已经找到了一个合理的答案:

<plugins>
    <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.2</version>
        <executions>
            <execution>
            <goals>
                <goal>test-jar</goal>
            </goals>
            <phase>test-compile</phase>
        </execution>
        </executions>
        <configuration>
            <outputDirectory>${basedir}\target</outputDirectory>
        </configuration>
    </plugin>
</plugins>

正如您在这里看到的,这里的主要区别是 标签。

我将创建测试 jar,它将在测试的编译阶段可用,而不仅仅是在打包阶段之后。

对我有用。

Regarding to my comment to Pascals question i think i have found a stuitable answer :

<plugins>
    <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.2</version>
        <executions>
            <execution>
            <goals>
                <goal>test-jar</goal>
            </goals>
            <phase>test-compile</phase>
        </execution>
        </executions>
        <configuration>
            <outputDirectory>${basedir}\target</outputDirectory>
        </configuration>
    </plugin>
</plugins>

The main difference here as you see here is the <phase> tag.

I will create the test-jar and it will be available in the compile phase of the tests and not only after the package phase.

Works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文