【问题标题】:How do I include a dependency's test jar into a Maven project's deployment?如何将依赖项的测试 jar 包含到 Maven 项目的部署中?
【发布时间】:2011-12-06 19:14:54
【问题描述】:

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

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

我们需要将虚拟类移至src/test/java。这意味着他们不会使用foo.jar 部署到从 Web 项目构建的战争中。我在 maven 网站上找到了 these instructions,但它们似乎不适合我。

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 的目标中我会得到两个罐子: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>

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

我尝试了多种方式来做到这一点:

<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。我还尝试在依赖项中使用&lt;classifier&gt; 而不是&lt;type&gt;,但它仍然是一样的。我尝试在配置文件之外使用上述依赖项(与其他依赖项一起),但它的行为仍然相同。

如果我将 &lt;type&gt; 添加到主依赖项(不添加其他依赖项),我会部署测试 jar(与上面的名称相同),但自然不会部署源。

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

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

谢谢!


更新:

我尝试了其他几种方法,但仍然无法正常工作。

我在 foo 项目(依赖项,而不是主 web 项目)中的 maven-jar-plugin 中添加了另一个执行,我希望在其中强制 maven 将测试类编译到与主要类相同的 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>

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

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

tl;博士

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

【问题讨论】:

标签: java deployment maven dependencies


【解决方案1】:

我参加聚会有点晚了,但我希望这对某人有所帮助:您可以包含相同依赖项的多个 类型。假设你的项目依赖common-artifact-1.0.jar,还有一个测试jarcommon-artifact-1.0-tests.jar

您可以通过执行以下操作同时导入 jar 和测试 jar:

<dependencies>
    <dependency>
        <groupId>my.corp</groupId>
        <artifactId>common-artifact</artifactId>
        <version>1.0</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>my.copr</groupId>
        <artifactId>common-artifact</artifactId>
        <version>1.0</version>
        <type>test-jar</type>
        <scope>test</scope> <!-- the "compile" scope also works here -->
    </dependency>
</dependencies>

【讨论】:

    【解决方案2】:

    最好在你的第一个模块中配置 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>
    

    在此之后,mvn install/release 还将部署一个工件 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>
    

    【讨论】:

    • 我认为使用&lt;classifier&gt;tests&lt;/classifier&gt; 比使用&lt;type&gt;test-jar&lt;/type&gt; 更清晰。
    • 这里的文档maven.apache.org/pom.html 说“类型通常与使用的包装相对应,尽管情况并非总是如此。一些示例是 jar、ejb-client 和 test-jar。”他们可能希望保留分类器“以区分从相同 POM 构建但内容不同的工件”,例如 jdk 版本。但在实践中可能会奏效。
    【解决方案3】:

    如果您在运行时需要 jar,您可能还想尝试以下技巧。这只是在编译时将依赖项直接复制到目标文件夹。我希望在制作软件包时将它们包括在内......我自己没有测试过这个

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>             
          <id>copy-dependencies</id>
          <phase>compile</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <artifactSet>
              <includes>
            <include>com.example:foo</include>
              </includes>
            </artifactSet>
            <outputDirectory>target/dependencies</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin> 
    

    【讨论】:

      【解决方案4】:

      我发现一个可行的解决方案是使用 build-helper plugin 在我为本地配置文件构建的程序集中获取测试文件夹。这是在依赖项目中:

      <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <!-- used to package the dummy daos when building 
               with the local profile -->
          <artifactId>build-helper-maven-plugin</artifactId>
          <executions>
              <execution>
                  <phase>generate-sources</phase>
                  <goals>
                      <goal>add-source</goal>
                  </goals>
                  <configuration>
                      <sources>
                          <source>${extra.sources.dir}</source>
                      </sources>
                  </configuration>
              </execution>
          </executions>
      </plugin>
      

      还有配置文件,也在依赖项目中:

      <profile>
          <id>local</id>
          <properties>
              <env.resources.dir>src/test/resources</env.resources.dir>
              <extra.sources.dir>src/test/java</extra.sources.dir>
          </properties>
          <dependencies>
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.8.1</version>
                  <scope>compile</scope>
              </dependency>
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>org.springframework.test</artifactId>
                  <scope>compile</scope>
              </dependency>
          </dependencies>
      </profile>
      

      这样,主项目的 pom 不会被修改,并且在本地构建时会部署存根。

      不过,我还没有发现您是否可以部署多个分类器。 :)

      【讨论】:

      • 不幸的是,这对 IntelliJ 造成了严重破坏,它认为单个源根仅存在于单个模块中。
      【解决方案5】:

      我看到您使用test-jar 作为type 并且您使用classifier 而不是type,但可能还使用test-jar ...但是您尝试过以下操作吗?

      <dependency>
          <groupId>com.example</groupId>
          <artifactId>foo</artifactId>
          <version>1.0.0-SNAPSHOT</version>
          <classifier>tests</classifier>
          <scope>test</scope>
      </dependency>
      

      【讨论】:

      • 是的,我做到了 - &lt;classifier&gt;tests&lt;/classifier&gt;,但没有 &lt;scope&gt; 属性 - 我需要运行时的测试 jar。
      • @AlexCiminian:“我在运行时需要测试 jar。”这句话我肯定会三思而后行。如果您在运行时确实需要它,这可能表明您在测试中拥有高效的代码,而不是在正确的位置!? (如果我错过了一些非常特殊的情况,我不得不原谅自己没有阅读最初的两个半屏幕页面来描述你的整个问题。:))
      • @TomFink 我遇到了同样的问题,我的情况如下:我有一个项目(我们称之为 CorrectnessTests),它检查另一个项目(FooTests)是否正确实现了它的所有测试。 IE。我在 CorrectnessTests 中定义了 FooTests 中的每个测试都应该遵循某些规则,应该至少有 n 个特定类型的测试等。这完全需要在 CorrectnessTests 运行时需要一个 FooTests 测试罐的情况。
      • @ThePadawan 但是如果“CorrectnessTests”模块的功能是doinf测试,为什么不在那里将其作为测试(或更多)实现。然后你再次陷入测试-测试关系。 :) 但我理解这个问题。我们最近遇到了一种情况,其中充当消息驱动系统的“刺激器”的工具正在使用另一个模块的测试代码。所以这导致了同样的情况。我通过将一些实用程序代码移动到生产代码来解决它。因为否则创建的 jar-with-dependencies 将包含所有测试框架依赖项...... :(
      猜你喜欢
      • 2018-12-19
      • 1970-01-01
      • 2011-10-23
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 2017-02-01
      相关资源
      最近更新 更多