如何将pom项目打包为可运行jar

这个问题在这里已经有了答案:

  • 我怎样才能创建一个可执行的JAR与依赖使用Maven? 33个答案

  • 你有很多选择

  • Maven Assembly插件,带有jar-with-dependencies描述符

  • Maven一个Jar插件

  • 分发Jar - Zip存档与您的jar,目录库和配置清单中的所有依赖关系,以便于执行。 下面的示例配置。

  • Maven Shade插件的可执行jar

  • 具有依赖关系的可运行Jar映射到本地Maven存储库(可能,但不可移植)更改类路径:使用Maven存储库样式类路径

  • 1.罐子与依赖组装

    <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.分配罐配置

    Maven依赖插件

    <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>
        <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>
        <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>
    

    装配描述符

    <?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/29625.html

    上一篇: How to package a pom project as runnable jar

    下一篇: How to include maven dependencies in a jar file?