Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven build assembly with dependencies

Tags:

java

maven

I have standalone java application, which i wanted to package as: myapp.jar. And all dependent jars to be copied to 'alternate folder'. Ideally, i would like to have maven update META-INF file to add all classpath dependencies jars entries into it.

for example, if my project is referencing commons.jar, and when i use this plugin to build assembly, it copies all .class files and packages from commons.jar into myappjar-with-dependencies.jar.

The problem with maven assembly plugin unjars all dependencies into myappjar-with-dependencies.jar.

<plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.core.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> 
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
like image 205
gpa Avatar asked Nov 28 '12 22:11

gpa


2 Answers

You can try as

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.core.App</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <descriptor>src/assembly/bin.xml</descriptor>
                </descriptors>
            </configuration>
        </plugin>

src/assembly/bin.xml

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>false</unpack>
            <includes>
                <include>${artifact}</include>
            </includes>
        </dependencySet>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <unpack>false</unpack>
            <excludes>
                <exclude>${artifact}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

Run it as

mvn clean package assembly:single

Details: http://maven.apache.org/plugins/maven-assembly-plugin/index.html

like image 75
Evgeniy Dorofeev Avatar answered Sep 22 '22 13:09

Evgeniy Dorofeev


Please follow the below steps:

Step 1: You should have your regular Jar present in Maven Package inside folder - target. If not, Run as > Maven Install(at this time Goals in Maven Build Run configuration will be 'clean package')

Step 2: Add below script in pom.xml

    <build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>Full path with Main class(App) for example com.first.apicode.App</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
  </build>

Step 3: Change Goals in Maven Build Run configuration to 'clean package assembly:single' and Click Run

THAT'S ALL!

Now you will be able to see runnable JAR with all dependencies inside target folder like YourArtifactID-0.0.1-SNAPSHOT-jar-with-dependencies.jar

like image 26
Ash Avatar answered Sep 22 '22 13:09

Ash