java 如何配置 maven shade 插件以在我的 jar 中包含测试代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5149130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:42:39  来源:igfitidea点击:

How can I configure the maven shade plugin to include test code in my jar?

javamavenhadoopmaven-shade-plugin

提问by Patrick Marchwiak

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?

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

回答by Steve K

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

使用 maven-shade-plugin 的 2.2 版,他们添加了一个“shadeTestJar”选项(参见 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).

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

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?

此外,这个问题重复了另一个问题,尽管接受的答案并不是真正令人满意:How to include test classes in Jar created by maven-shade-plugin?

回答by user23288

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 shadeTestJarfeature can work like it's supposed to.

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

回答by Dmitry Vasilyev

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>

回答by Yuriy Nemtsov

Try includeing your test packages like this:

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

回答by user23288

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

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