Building a fat jar using maven

后端 未结 6 1956
梦毁少年i
梦毁少年i 2020-11-21 23:51

I have a code base which I want to distribute as jar. It also have dependency on external jars, which I want to bundle in the final jar.

I heard that this can be do

相关标签:
6条回答
  • 2020-11-22 00:25

    actually, adding the

    <archive>
       <manifest>
        <addClasspath>true</addClasspath>
        <packageName>com.some.pkg</packageName>                     
        <mainClass>com.MainClass</mainClass>
      </manifest>
    </archive>
    

    declaration to maven-jar-plugin does not add the main class entry to the manifest file for me. I had to add it to the maven-assembly-plugin in order to get that in the manifest

    0 讨论(0)
  • 2020-11-22 00:28

    Maybe you want maven-shade-plugin, bundle dependencies, minimize unused code and hide external dependencies to avoid conflicts.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <minimizeJar>true</minimizeJar>
                            <createDependencyReducedPom>true</createDependencyReducedPom>
                            <dependencyReducedPomLocation>
                                ${java.io.tmpdir}/dependency-reduced-pom.xml
                            </dependencyReducedPomLocation>
                            <relocations>
                                <relocation>
                                    <pattern>com.acme.coyote</pattern>
                                    <shadedPattern>hidden.coyote</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    References:

    • http://maven.apache.org/plugins/maven-shade-plugin/plugin-info.html
    • http://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html
    0 讨论(0)
  • 2020-11-22 00:37

    An alternative is to use the maven shade plugin to build an uber-jar.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version> Your Version Here </version>
        <configuration>
                <!-- put your configurations here -->
        </configuration>
        <executions>
                <execution>
                        <phase>package</phase>
                        <goals>
                                <goal>shade</goal>
                        </goals>
                </execution>
        </executions>
    </plugin>
    
    0 讨论(0)
  • 2020-11-22 00:42

    You can use the maven-shade-plugin.

    After configuring the shade plugin in your build the command mvn package will create one single jar with all dependencies merged into it.

    0 讨论(0)
  • 2020-11-22 00:49

    Note: If you are a spring-boot application, read the end of answer

    Add following plugin to your pom.xml The latest version can be found at

    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>CHOOSE LATEST VERSION HERE</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>assemble-all</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    ...
    

    After configuring this plug-in, running mvn package will produce two jars: one containing just the project classes, and a second fat jar with all dependencies with the suffix "-jar-with-dependencies".

    if you want correct classpath setup at runtime then also add following plugin

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>fully.qualified.MainClass</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    

    For spring boot application use just following plugin (choose appropriate version of it)

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <fork>true</fork>
            <mainClass>${start-class}</mainClass>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
    0 讨论(0)
  • 2020-11-22 00:50

    You can use the onejar-maven-plugin for packaging. Basically, it assembles your project and its dependencies in as one jar, including not just your project jar file, but also all external dependencies as a "jar of jars", e.g.

    <build>
        <plugins>
            <plugin>
                <groupId>com.jolira</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                    <version>1.4.4</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>one-jar</goal>
                            </goals>
                        </execution>
                    </executions>
            </plugin>
        </plugins>
    </build>
    

    Note 1: Configuration options is available at the project home page.

    Note 2: For one reason or the other, the onejar-maven-plugin project is not published at Maven Central. However jolira.com tracks the original project and publishes it to with the groupId com.jolira.

    0 讨论(0)
提交回复
热议问题