Including dependencies in a jar with Maven

后端 未结 13 1984
盖世英雄少女心
盖世英雄少女心 2020-11-22 00:39

Is there a way to force maven(2.0.9) to include all the dependencies in a single jar file?

I have a project the builds into a single jar file. I want the classes fro

相关标签:
13条回答
  • 2020-11-22 01:00

    Putting Maven aside, you can put JAR libraries inside the Main Jar but you will need to use your own classloader.

    Check this project: One-JAR link text

    0 讨论(0)
  • 2020-11-22 01:04

    Thanks I have added below snippet in POM.xml file and Mp problem resolved and create fat jar file that include all dependent jars.

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
    
    0 讨论(0)
  • 2020-11-22 01:06

    You can use the newly created jar using a <classifier> tag.

    <dependencies>
        <dependency>
            <groupId>your.group.id</groupId>
            <artifactId>your.artifact.id</artifactId>
            <version>1.0</version>
            <type>jar</type>
            <classifier>jar-with-dependencies</classifier>
        </dependency>
    </dependencies>
    
    0 讨论(0)
  • 2020-11-22 01:06

    http://fiji.sc/Uber-JAR provides an excellent explanation of the alternatives:

    There are three common methods for constructing an uber-JAR:

    1. Unshaded. Unpack all JAR files, then repack them into a single JAR.
      • Pro: Works with Java's default class loader.
      • Con: Files present in multiple JAR files with the same path (e.g., META-INF/services/javax.script.ScriptEngineFactory) will overwrite one another, resulting in faulty behavior.
      • Tools: Maven Assembly Plugin, Classworlds Uberjar
    2. Shaded. Same as unshaded, but rename (i.e., "shade") all packages of all dependencies.
      • Pro: Works with Java's default class loader. Avoids some (not all) dependency version clashes.
      • Con: Files present in multiple JAR files with the same path (e.g., META-INF/services/javax.script.ScriptEngineFactory) will overwrite one another, resulting in faulty behavior.
      • Tools: Maven Shade Plugin
    3. JAR of JARs. The final JAR file contains the other JAR files embedded within.
      • Pro: Avoids dependency version clashes. All resource files are preserved.
      • Con: Needs to bundle a special "bootstrap" classloader to enable Java to load classes from the wrapped JAR files. Debugging class loader issues becomes more complex.
      • Tools: Eclipse JAR File Exporter, One-JAR.
    0 讨论(0)
  • 2020-11-22 01:12
            <!-- Method 1 -->
            <!-- Copy dependency libraries jar files to a separated LIB folder -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    <excludeTransitive>false</excludeTransitive> 
                    <stripVersion>false</stripVersion>
                </configuration>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Add LIB folder to classPath -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
    
    
            <!-- Method 2 -->
            <!-- Package all libraries classes into one runnable 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>            
    
    0 讨论(0)
  • 2020-11-22 01:16

    My definitive solution on Eclipse Luna and m2eclipse: Custom Classloader (download and add to your project, 5 classes only) :http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/plain/org.eclipse.jdt.ui/jar%20in%20jar%20loader/org/eclipse/jdt/internal/jarinjarloader/; this classloader is very best of one-jar classloader and very fast;

    <project.mainClass>org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader</project.mainClass> <project.realMainClass>my.Class</project.realMainClass>

    Edit in JIJConstants "Rsrc-Class-Path" to "Class-Path"
    mvn clean dependency:copy-dependencies package
    is created a jar with dependencies in lib folder with a thin classloader

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.java</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
                <targetPath>META-INF/</targetPath>
            </resource>
            <resource>
                <directory>${project.build.directory}/dependency/</directory>
                <includes>
                    <include>*.jar</include>
                </includes>
                <targetPath>lib/</targetPath>
            </resource>
        </resources>
    <pluginManagement>
            <plugins>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <mainClass>${project.mainClass}</mainClass>
                                <classpathPrefix>lib/</classpathPrefix>
                            </manifest>
    
                            <manifestEntries>
                                <Rsrc-Main-Class>${project.realMainClass}  </Rsrc-Main-Class>
                                <Class-Path>./</Class-Path>
                            </manifestEntries>
    
                        </archive>
                    </configuration>
                </plugin>
    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-dependencies</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    
    0 讨论(0)
提交回复
热议问题