Maven项目导出可执行jar

 

配置文件中添加插件

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>

                            <!-- 主程序入口类 -->
                            <mainClass>com.java.demo.JsonDemo</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

 

 

示例如下:

1.   新建Maven项目 exe-jar

 

2.   pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.java</groupId>
    <artifactId>exe-jar</artifactId>
    <version>1.0.0</version>

    <dependencies>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.56</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>

        <plugins>

            <!-- 编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- 打包插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>

                            <!-- 主程序入口类 -->
                            <mainClass>com.java.demo.JsonDemo</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>

 

 

3.   JsonDemo.java

package com.java.demo;

import com.alibaba.fastjson.JSON;

public class JsonDemo {

    public static void main(String[] args) {
        System.out.println(JSON.toJSONString(args, true));
    }

}

 

4.   在项目上单击右键 》 Run As 》 Maven build... 》。 如下图:

 

 

在Goals 里输入 clean package , 单击【Run】。如下图

 

 控制台打出BUILD SUCCESS ,打包完成。

 

打开target 目录,测试运行

在命令行窗口输入

java -jar exe-jar.jar Hello Good Hi

回车执行,如下图

 

 

 

控制台打印结果:

["Hello","Good","Hi"]

 

 

 

打包成功。

 

 

 

 

.

posted @ 2019-04-12 13:35  诚信天下  阅读(4008)  评论(0编辑  收藏  举报