drgn
drgn

Reputation: 1140

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

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.

Upvotes: 12

Views: 10184

Answers (4)

AnonymousDev
AnonymousDev

Reputation: 1326

I had an issue where the Maven Shade Plugin was including all dependencies in the Uber Jar (from both parent pom.xml and child module pom.xml files), causing various conflicts. I only wanted a single dependency to be included (GSON) for the child module, which I managed to do as follows:

# Child pom.xml
...
<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>${gson.version}</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.5.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <artifactSet>
                            <includes>
                                <include>com.google.code.gson:gson</include>
                            </includes>
                        </artifactSet>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Upvotes: 1

Gustav Ruthg&#229;rd
Gustav Ruthg&#229;rd

Reputation: 386

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>

Upvotes: 26

drgn
drgn

Reputation: 1140

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.

Upvotes: 1

khmarbaise
khmarbaise

Reputation: 97437

For this kind of purpose i would suggest to use the maven-shade-plugin which will create a ueber-jar which can be controlled in many ways. With the shade plugin you can exclude things you don't like. But this might be caused by not using a separate maven module where you can control the dependencies.

Upvotes: 2

Related Questions