How to package a pom project as runnable jar

This question already has an answer here:

  • How can I create an executable JAR with dependencies using Maven? 33 answers

  • There are many options for you

  • Maven Assembly Plugin with jar-with-dependencies descriptor

  • Maven One Jar Plugin

  • Distribution Jar - Zip Archive with your jar, all dependencies in directory lib and configured manifest for easy execution. Sample configuration below.

  • Executable Jar with Maven Shade Plugin

  • Runnable Jar with dependencies mapped to local maven repository (possible, but not portable) Altering The Classpath: Using a Maven Repository-Style Classpath

  • 1. Jar Assembled With Dependencies

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>make-jar-with-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>eu.codearte.MainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    2. Maven One Jar

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>eu.codearte.MainClass</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.dstovall</groupId>
        <artifactId>onejar-maven-plugin</artifactId>
        <version>1.4.4</version>
        <executions>
            <execution>
                <goals>
                    <goal>one-jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    ...
    <pluginRepositories>
        <pluginRepository>
            <id>onejar-maven-plugin.googlecode.com</id>
            <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
        </pluginRepository>
    </pluginRepositories>
    

    3. Distribution Jar configuration

    Maven Dependency Plugin

    <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.8</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    <overWriteReleases>true</overWriteReleases>
                    <overWriteSnapshots>true</overWriteSnapshots>
                    <overWriteIfNewer>true</overWriteIfNewer>
                    <stripVersion>false</stripVersion>
                    <stripClassifier>false</stripClassifier>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    Maven Assembly Plugin

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/bundle.xml</descriptor>
                    </descriptors>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    Maven Jar Plugin

    <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
           <execution>
                <id>exe-jar</id>
                <goals>
                    <goal>jar</goal>
                </goals>
                <phase>package</phase>
                <configuration>
                    <classifier>exe</classifier>
                    <archive>
                        <manifest>
                            <mainClass>eu.codearte.MainClass</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                    </archive>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    Assembly Descriptor

    <?xml version='1.0' encoding='UTF-8'?>
    <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">
        <id>bundle</id>
        <formats>
            <format>zip</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <fileSets>
            <fileSet>
                <directory>target/lib</directory>
                <outputDirectory>lib</outputDirectory>
            </fileSet>
        </fileSets>
        <files>
            <file>
                <source>target/project-exe.jar</source>
                <outputDirectory>/</outputDirectory>
            </file>
        </files>
    </assembly>
    
    链接地址: http://www.djcxy.com/p/29626.html

    上一篇: 打包使用Maven构建的独立Java程序

    下一篇: 如何将pom项目打包为可运行jar