Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven jar with dependencies?

Tags:

java

maven

build

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>
like image 234
user3198603 Avatar asked Jan 29 '15 14:01

user3198603


2 Answers

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>
like image 165
SubOptimal Avatar answered Oct 06 '22 01:10

SubOptimal


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>  
like image 41
StackBox Avatar answered Oct 06 '22 01:10

StackBox