Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building single fat jar from a multi module maven project - including test classes

I'm in a situation where I need to build a fat jar from a multi module maven project. The project is built based on Selenium/cucumber API's.

Structure of my project is as follows

   Parent -- pom
   |
   |__core_module src/main/java --> helper classes for selenium
   |
   |__acme_module src/main/test --> Test Classes for acme project

I have tried different ways to build a "acme_test.jar" which includes core_module+acme_module.But none of them helped me.

Much appreciate any clue to solve this .

Thanks

like image 315
kallada Avatar asked Feb 11 '17 20:02

kallada


People also ask

Does Maven include test classes in jar?

You can produce a jar which will include your test classes and resources. To reuse this artifact in an other project, you must declare this dependency with type test-jar : <project>

How do you make a fat jar?

Maven can build a Fat JAR from your Java project. A Fat JAR is a single JAR file that contains all the compiled Java classes from your project, and all compiled Java classes from all JAR files your project depends on (see Maven Dependencies).

How do I create a fat jar in eclipse?

In the "Package-Explorer" (not the "Resource-View") right click on the project "demolib". Select "+ Build Fat Jar". A Configuration Dialog appears. Just press "Finish".


2 Answers

As @MariuszS mentions, first restructure your project so that you separate any Unit tests or integration tests for the actual classes that test (=drive/verify navigation for) Acme.

   Parent -- pom
   |
   |__core_module src/main/java --> helper classes for selenium
   |
   |__acme_module src/main/java --> Classes specific for navigating acme 
   |
   |__acme_module src/test/java--> (Unit etc) Test Classes for acme project

Then, you need acme_module to contain core_module as dependency.

Finally, in acme_module, put this in your build plugins section:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <id>create-fat-jar</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                <!-- add Main-Class to manifest file -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>your.main.class</mainClass>
                    </transformer>
                </transformers>
                    <finalName>YourJarName</finalName>
            </configuration>
        </execution>
    </executions>
</plugin>

You can also explicitly include the core_module by adding an include filter in the plugin configuration section

like image 121
Yiannis Dermitzakis Avatar answered Oct 13 '22 19:10

Yiannis Dermitzakis


Configure acme_module as fat module - this module should produce executable fat jar with all dependencies.

Add core_module to acme_module as dependency.

Move your test classes from src/test/java to src/main/java, because this class should be executable. If you have tests (like junit), then leave this classes inside test directory, but executable part should stay in main.

like image 32
MariuszS Avatar answered Oct 13 '22 21:10

MariuszS