Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Include a SINGLE dependency into a jar using maven and fatjar plugin

Tags:

maven

uberjar

I feel a bit stupid about this question but i can't figure out how to add a SINGLE dependency (jdom.jar) into another jar.

Context: We developed a simple plug-in for our application, this plug-in have many dependency. We were using fatjar to include jdom.jar into it. I am trying to fix a bug in this plug-in, so i decided to "maven-ize" it at the same time. (We just switched to maven) This plug-in is loaded on the runtime so the only dependencies we want packaged with it is the jdom.jar.

Problem: I found that there is a maven fatjar plug-in! Unfortunately i could not find any documentation and this maven plug-in add EVERY dependency into the ouput jar. After many try i decided to give up on this fatjar plug-in and searched for another one. I found one-jar , shade but after a quick read on them they look like they add every dependency.

Question: what would be a simple way to add only jdom.jar into my plug-in jar like this:

-MyPlug-in.jar
|
|-src
 |-main
  |-java
   |-*.java
|-jdom.jar

Also I don't want to alter the manifest or the output jar filename

Thank a lots for your time.

like image 594
drgn Avatar asked Jul 11 '12 19:07

drgn


People also ask

How do I package a jar with dependencies?

2.3. There are three main parts to this configuration. First, <shadedArtifactAttached> marks all dependencies to be packaged into the jar. Second, we need to specify the transformer implementation; we used the standard one in our example. Finally, we need to specify the main class of our application.

How do I add a dependency in Maven?

Add a Java Maven Dependency to the Utility ProjectRight-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.

Which build plugin allows you to create a fat jar that contains all the dependencies in the final jar file?

Creating fat JARs using the Maven Assembly plugin The Maven Assembly plugin provides the ability to combine project output into a single distributable archive that contains dependencies, modules, site documentation, and other files.


2 Answers

There was no answer here regarding how to use maven to include one single jar-file with the maven-shader-plugin. It took me some time to figure out how to actually do that. Here is a snippet to include just the classes from the dependency com.googlecode.json-simple:json-simple.

<project>
    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <includes>
                                   <include>com.googlecode.json-simple:json-simple</include>
                                </includes>
                            </artifactSet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            ...
        </plugins>
    </build>
    ...
</project>
like image 160
Gustav Ruthgård Avatar answered Oct 09 '22 20:10

Gustav Ruthgård


Using maven Shade would work fine, one-jar would have done the job too. But we finally decided that packaging jdom in our extension would be a bad practice.

So instead we gonna do this:

|-Root application Folder
 |-Extension Folder
  |-MyExtension.jar
  |-libs Folder
   |-jdom.jar

The jar into the lib folder will be loaded dynamically and won't be loaded if the extension cannot find the appropriate libs into the libs folder.

For the people who look to solve my primary problem please check out @khmarbaise Answer.

like image 33
drgn Avatar answered Oct 09 '22 21:10

drgn