Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build and package application and dependencies in separate jars with Maven

I am looking for a pom.xml configuration that would package my application in one jar and all application dependencies in another jar. I looked at the maven-assembly-plugin and with the following configuration i was able to build an application jar and then an application jar with all dependencies, but i needs the dependencies jar to not contain my application:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
        <execution>
            <id>jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>attached</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Thanks

like image 860
VilleLipponen Avatar asked Oct 31 '14 01:10

VilleLipponen


1 Answers

I have seen this solved by building an application in modules. Here is a good blog on it: http://giallone.blogspot.com/2012/12/maven-install-missing-offline.html?_sm_au_=iVVnK0HqJZDtb1Hw

For your case, you could have a module called 'dependencies' that used the maven-dependency-plugin and maven-assembly-plugin to copy all dependencies and package them in a single jar. Then your application can reference that jar as it's dependency in a separate module. The top-level will build both.

top-level pom

.
.
.
    <packaging>pom</packaging>
.
.
    <modules>
        <module>dependencies</module>
        <module>yourApp</module>
    </modules>
.
.
.

dependencies pom

.
.
.
    <dependencies>
        <dependency>
            <groupId>some.group.id</groupId>
            <artifactId>yourFirstDependency</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>some.group.id2</groupId>
            <artifactId>yourSecondDependency</artifactId>
            <version>1.1.4</version>
        </dependency>
    <dependencies>

    <build>
        <defaultGoal>install</defaultGoal>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
.
.
.

main application pom

.
.
.
    <!-- Reference your dependency module here as the first in the list -->
    <dependencies>
        <dependency>
            <groupId>your.group.id</groupId>
            <artifactId>yourDependencyJarName</artifactId>
            <version>1.0</version>
        </dependency>
        .
        .
    </dependencies>
.
.
.
like image 121
Dzohs Avatar answered Sep 28 '22 08:09

Dzohs