如何配置 Maven Shade 插件以在我的 jar 中包含测试代码?

发布于 2024-10-19 13:00:33 字数 173 浏览 6 评论 0原文

我使用 Shade maven 插件来构建我的项目,以便其所有依赖项都包含在一个 jar 中(这使得在 Hadoop 上运行它变得更容易)。 Shade 似乎默认排除了我的测试代码,这是可以理解的。由于我想针对我的集群运行集成测试,我希望设置另一个配置文件来为此目的构建一个单独的 jar。有什么方法可以配置此插件以包含测试代码吗?

I use the shade maven plugin to build my project so that all of its dependencies are included in one jar (this makes it easier to run it on Hadoop). Shade seems to exclude my test code by default, which is understandable. Since I would like to run integration tests against my cluster, I am hoping to setup another profile to build a separate jar for this purpose. Is there any way to configure this plugin to also include test code?

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

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

发布评论

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

评论(5

我是有多爱你 2024-10-26 13:00:33

最后几个答案充其量只是针对损坏的功能的混乱解决方法。事实仍然是 maven-shade-plugin 中存在一个错误。与此同时,我调查并找出了该错误的根源,并创建了一个补丁。现在,我希望 Apache 的某个人尽快将其纳入其中,最终 shadeTestJar 功能可以像预期的那样工作。

These last couple of answers are messy workarounds for a broken feature at best. The fact of the matter remains that there is a bug in maven-shade-plugin. In the meantime I've investigated and root-caused the bug, and created a patch. Now I hope someone at Apache includes it soon and then finally the shadeTestJar feature can work like it's supposed to.

蓝梦月影 2024-10-26 13:00:33

在 maven-shade-plugin 2.2 版本中,他们添加了一个“shadeTestJar”选项(请参阅 MSHADE-158): http://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html#shadeTestJar

但是,我尝试使用这个,但无法无法让它发挥作用。这是我的插件配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadeTestJar>true</shadeTestJar>
            </configuration>
        </execution>
    </executions>
</plugin>

“...-tests.jar”文件没有任何条目,但主阴影 jar 看起来不错(尽管它不包含任何测试类)。

另外,这个问题重复了另一个问题,尽管接受的答案并不真正令人满意: 如何在 maven-shade-plugin 创建的 Jar 中包含测试类?

With version 2.2 of the maven-shade-plugin, they added a "shadeTestJar" option (see MSHADE-158): http://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html#shadeTestJar

However, I tried using this and couldn't get it to work. Here's my plugin config:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadeTestJar>true</shadeTestJar>
            </configuration>
        </execution>
    </executions>
</plugin>

The "...-tests.jar" file has no entries, but the main shaded jar looks fine (although it doesn't contain any test classes).

Also, this question duplicates this other question, although the accepted answer isn't real satisfying: How to include test classes in Jar created by maven-shade-plugin?

你对谁都笑 2024-10-26 13:00:33

我已经成功地通过添加以下内容使其工作:

<plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>build-helper-maven-plugin</artifactId>
     <version>1.9.1</version>
     <executions>

        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
               <goal>add-source</goal>
            </goals>
            <configuration>
               <sources>
                   <source>${project.basedir}/src/test/java/</source>
               </sources>
            </configuration>
        </execution>

      </executions>
</plugin>

I've managed to make it work by adding :

<plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>build-helper-maven-plugin</artifactId>
     <version>1.9.1</version>
     <executions>

        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
               <goal>add-source</goal>
            </goals>
            <configuration>
               <sources>
                   <source>${project.basedir}/src/test/java/</source>
               </sources>
            </configuration>
        </execution>

      </executions>
</plugin>
℉服软 2024-10-26 13:00:33

尝试像这样include您的测试包:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>1.2.2</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <artifactSet>
          <includes>
            <include>org.apache.maven:*</include>
          </includes>
        </artifactSet>
      </configuration>
    </execution>
  </executions>
</plugin>

Try includeing your test packages like this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>1.2.2</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <artifactSet>
          <includes>
            <include>org.apache.maven:*</include>
          </includes>
        </artifactSet>
      </configuration>
    </execution>
  </executions>
</plugin>
假扮的天使 2024-10-26 13:00:33

按照上面 ~steve-k 的解释使用 maven-shade-plugin 是正确的,不幸的是,由于 bug shadeTestJar 不起作用,并且生成的测试 JAR 是空的。

Using the maven-shade-plugin as explained by ~steve-k above is correct, unfortunately due to a bug shadeTestJar doesn't work and the resulting test JAR is empty.

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