5

I want to build the project so that final jar include all the dependencies in a single jar file (if not possible includes classes from dependencies in to the jar file) i followed the thread Including dependencies in a jar with Maven but lt included the dependencies also which i even did not mention in my pom. Here is my POM which has just two dependencies.

I want when when final is built , it includes it specific dependencies mentioned in pom(either in classes or jar form)

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>com.myProject</groupId>
  <artifactId>utils</artifactId>
  <version>1</version>
</dependency>
Community
  • 1
  • 1
user3198603
  • 5,528
  • 13
  • 65
  • 125
  • Have a look a the maven shade plugin https://maven.apache.org/plugins/maven-shade-plugin/ – SubOptimal Jan 29 '15 at 14:29
  • the other dependencies that you see are probably the dependencies of your dependencies... don't you want to include them? – jamp Jan 29 '15 at 14:30
  • Yes i don't want to include them – user3198603 Jan 29 '15 at 14:31
  • @suboptimal can you give me some example as i did not get how shade can help me here – user3198603 Jan 29 '15 at 14:37
  • @user3198603 Maybe I misread your question in the first step. You want to get one jar file which contains only the dependencies you mention in your `pom.xml` independent from the scope? But excluding the dependencies of the mentioned dependencies might lead into an nonfunctional application. – SubOptimal Jan 29 '15 at 14:42
  • @subOptimal I have verified that just including the mentioned dependencies does not lead any nonfunctionality – user3198603 Jan 29 '15 at 14:46

2 Answers2

4

Here you find an example pom.xml how to solve it with the maven-shade-plugin

When you run mvn package it generates an Jar file target/app-with-dependencies.jar. This Jar file include the compiled classes of the project itself and only the dependencies org.slf4j:slf4j-log4j12 and com.myProject:utils. It does not include the dependencies on which org.slf4j:slf4j-log4j12 depend.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sub.optimal</groupId>
    <artifactId>shadedemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <maven.shade.version>2.3</maven.shade.version>
        <maven.antrun.version>1.7</maven.antrun.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>com.myProject</groupId>
            <artifactId>utils</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>${maven.shade.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-shade-plugin</artifactId>
                <version>${maven.shade.version}</version>
                <configuration>
                    <quiet>true</quiet>
                    <verbose>false</verbose>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>app-with-dependencies</finalName>
                            <artifactSet>
                                <includes>
                                    <!--
                                    here you define the dependencies which
                                    you want to be included
                                    -->
                                    <include>org.slf4j:slf4j-log4j12:*</include>
                                    <include>com.myProject:utils:*</include>
                                </includes>
                            </artifactSet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>${maven.antrun.version}</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <delete>
                                    <fileset dir="${project.build.directory}" includes="${project.name}-*.jar" />
                                </delete>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Thanks a lot. One more quick thing i want to exclude specific property file(MyProject.properties) from final jar. I tried puuting element MyProject.properties below includes in your example but still MyProject.properties is included in final jar. Any idea why? – user3198603 Jan 30 '15 at 09:15
  • I have got answer from http://stackoverflow.com/questions/17658831/how-can-i-exclude-dsa-and-sf-files-from-shaded-jar. Thanks again. – user3198603 Jan 30 '15 at 09:22
  • one question on your solution. It creates two jar files i.e a) shadedemo-1.0-SNAPSHOT and second is app-with-dependencies. I just need one i.e app-with-dependencies.. Is there a way i can get output as single jar file instead of two? – user3198603 Jan 30 '15 at 11:04
  • @user3198603 You can remove it at the end of the `package`phase. But frankly I would leave it there. It needs to be build anyway for the shade plugin. If you want to clean up the `target` directory execute `mvn clean`. – SubOptimal Jan 30 '15 at 11:23
  • Thanks a lot. I got bit late in accepting the answer :( – user3198603 Feb 07 '15 at 10:27
3

One way is to use maven assembly plugin, the pom.xml configuration is like:

<plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-assembly-plugin</artifactId>  
            <configuration>  
                <archive>  
                    <manifest>  
                        <mainClass>com.taobao.top.appstore.App</mainClass>  
                    </manifest>  
                </archive>  
                <descriptorRefs>  
                    <descriptorRef>jar-with-dependencies</descriptorRef>  
                </descriptorRefs>  
            </configuration>  
</plugin>  
StackBox
  • 326
  • 1
  • 6
  • This is the solution suggested by http://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven. But as i said it also includes all the dependencies which i even did not mention in pom – user3198603 Jan 29 '15 at 14:34
  • When you import a dependency,it may depends on other jars,Notice that Maven Dependency Mechanism is an import function for maven, :http://www.mkyong.com/maven/how-to-use-maven-dependency-to-download-library-automatically/ – StackBox Jan 29 '15 at 14:46
  • But because of some reason i want to include dependencies that are mentioned in my pom only. Is there a way to do that ? – user3198603 Jan 29 '15 at 17:21