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

4

11

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.

Catching answered 11/7, 2012 at 19:39 Comment(0)
M
25

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>
Marleen answered 5/12, 2015 at 17:52 Comment(1)
Exactly what I needed and this stil works perfectly even with version 2.4.3. I was having difficulty with the various <configuration/> variations, but finally the <artifactSet/> instead of <filters/> worked out.Obtest
P
2

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.

Podvin answered 12/7, 2012 at 7:9 Comment(1)
Thank for this answer. I knew i was on the right direction but we finally decided to proceed in an other way. See my answer.Catching
C
1

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.

Catching answered 12/7, 2012 at 13:50 Comment(0)
F
1

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>
Funds answered 4/1 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.