I used following in my pom.xml
to create the fat jar:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
But this includes the application code as well. I want to make 2 jars, one with only application code and another single jar with only dependencies.
I also tried this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
but it just puts all the dependency jars in a single folder.
The cleanest way of doing this ls to have a multi module maven project where 3 projects do separate jobs, here is an example.
The parent holds it all together:
<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>com.greg</groupId>
<artifactId>two-fat-jars</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>two-fat-jars</name>
<url>http://maven.apache.org</url>
<modules>
<module>common</module>
<module>application-jar</module>
<module>dependencies-jar</module>
</modules>
</project>
The first project builds the code
<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>
<parent>
<groupId>com.greg</groupId>
<artifactId>two-fat-jars</artifactId>
<version>1.0</version>
</parent>
<artifactId>common</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>
The second project has a dependency to the common project and builds the application jar, excluding certain dependencies:
<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>
<parent>
<groupId>com.greg</groupId>
<artifactId>two-fat-jars</artifactId>
<version>1.0</version>
</parent>
<artifactId>application-jar</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.greg</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>junit:junit</exclude>
</excludes>
<includes>
<include>com.greg:common</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The third project builds the dependencies jar by excluding the application code dependencies:
<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>
<parent>
<groupId>com.greg</groupId>
<artifactId>two-fat-jars</artifactId>
<version>1.0</version>
</parent>
<artifactId>dependencies-jar</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.greg</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>com.greg:common</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This is the example code structure:
./pom.xml
./application-jar
./application-jar/pom.xml
./application-jar/dependency-reduced-pom.xml
./dependencies-jar
./dependencies-jar/pom.xml
./common
./common/src
./common/src/main
./common/src/main/java
./common/src/main/java/com
./common/src/main/java/com/greg
./common/src/main/java/com/greg/App.java
./common/src/main/resources
./common/src/main/resources/configs
./common/src/main/resources/configs/config1.xml
./common/src/main/resources/configs/config2.xml
./common/src/main/resources/test2.properties
./common/src/main/resources/test1.properties
./common/src/test
./common/src/test/java
./common/src/test/java/com
./common/src/test/java/com/greg
./common/src/test/java/com/greg/AppTest.java
./common/pom.xml
./common/configs
./common/configs/config1.xml
./common/configs/config2.xml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With