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

发布于 2024-12-09 14:42:28 字数 3336 浏览 0 评论 0原文

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

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

我们需要将虚拟类移动到src/test/java。这意味着它们不会与 foo.jar 一起部署到从 Web 项目构建的 war 中。我在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>

foo-web 上运行 mvn install 时,在目标中foo 我会得到两个 jar: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。我还尝试在依赖项中使用 而不是 ,但它仍然是相同的。我尝试在配置文件之外使用上述依赖项(与其他依赖项一起),但它的行为仍然相同。

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

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

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

谢谢!


更新:

我尝试了多种方法,但仍然不起作用。

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

<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/tests 分类器/test 范围关系。当我尝试指定除主 jar 之外正在构建的新 jar 时,我得到了与尝试包含 -tests jar 时相同的行为。我检查了本地 Maven 存储库,依赖项目中的所有 jar 都安装得很好,所以问题是主项目的依赖解析。

tl;dr

一切都归结为一个问题:是否可以在多个分类器中包含相同的依赖关系。从我到目前为止所看到的情况来看,答案是否定的 - 当使用不同的分类器多次指定相同的依赖项时,我总是会得到 com.example-foo jar。

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.

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

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

发布评论

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

评论(5

四叶草在未来唯美盛开 2024-12-16 14:42:28

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

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>
东京女 2024-12-16 14:42:28

我看到您使用 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>

I see you use test-jar as type and you used classifier instead of type but probably also with test-jar ... but did you try the following?

<dependency>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <classifier>tests</classifier>
    <scope>test</scope>
</dependency>
年少掌心 2024-12-16 14:42:28

我来晚了一点,但我希望这对某人有帮助:您可以包含同一依赖项的多个类型。假设您的项目依赖于 common-artifact-1.0.jar,并且还有一个测试 jar common-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>

I'm a bit late to the party, but I hope this helps someone: you can include multiple types of the same dependency. Assume your project depends on common-artifact-1.0.jar, and there's also a test jar common-artifact-1.0-tests.jar.

You can import both the jar and the tests jar by doing this:

<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>
所谓喜欢 2024-12-16 14:42:28

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

<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 不会被修改,并且在本地构建时会部署存根。

不过,我仍然不知道是否可以部署多个分类器。 :)

One working solution that I found was to use the build-helper plugin to get the test folder in the assembly I'm building for the local profile. This is in the dependent project:

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

And the profile, also in the dependent project:

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

This way, the main project's pom does not get modified and the stubs get deployed when building locally.

I still haven't found out if you can deploy multiple classifiers, though. :)

焚却相思 2024-12-16 14:42:28

如果您在运行时需要 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> 

If you need the jars at runtime you might also want to try the following trick. This just copies the dependencies directly to your target folder when compiling. I expect that they will be included when a package is made ... I haven't tested this myself

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