开发者

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

开发者 https://www.devze.com 2023-02-13 20:15 出处:网络
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

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?


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.


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?


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>


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号