Abhijit Deb
Abhijit Deb

Reputation: 39

Creating one jar with all the dependent jars in it

I am trying to create one jar file all the dependent jar files in it. Maven creates a separate lib folder and copies all the jars into it then the executable jar works as the jar and the lib are at the same location but I want the jars to be part of the main jar and not outside in a lib folder. How do I do this?

Upvotes: 0

Views: 1103

Answers (2)

Jakub Kotowski
Jakub Kotowski

Reputation: 7571

You should use the Maven Assembly plugin or the Maven Shade plugin.

The fact is that the JAR file format doesn't have native support for including dependencies inside itself (there's a 20-year old RFE logged for it). So you can achieve what you're asking for but it will always be some sort of workaround. For example you have to be careful about possible file name conflicts - that is something that the Shade plugin is trying to solve, the Assembly plugin is easier to use, IMHO, but will keep overwriting files.

Have a look at this example how to use the assembly plugin and how to deal with DLLs: Package Dll in Jar using Maven- single goal

Upvotes: 1

Abhijit Deb
Abhijit Deb

Reputation: 39

<build>
    <finalName>MIM</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.tsc.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>


    </plugins>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.apache.maven.plugins
                                    </groupId>
                                    <artifactId>
                                        maven-dependency-plugin
                                    </artifactId>
                                    <versionRange>
                                        [2.1,)
                                    </versionRange>
                                    <goals>
                                        <goal>prepare-package</goal>
                                        <goal>
                                            copy-dependencies
                                        </goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Upvotes: 0

Related Questions