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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>assemble-executable-jar</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>myjar</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<!-- 打包用的plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<!-- 配置入口函数及配置文件 -->
<configuration>
<archive>
<!-- 配置入口静态函数 -->
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.test.Main</mainClass>
</manifest>
<!-- 指定配置文件位置
<manifestEntries>
<Class-Path>config/</Class-Path>
</manifestEntries>
-->
</archive>
<!-- 打包的包名描述,可省略 -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<!-- 执行打包的方法 -->
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 配置编译器版本 -->
<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>
</configuration>
</plugin>
</plugins>
<!-- 指定程序所需配置文件打包后的相对位置 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>dbconfig.properties</include>
</includes>
<filtering>false</filtering>
<targetPath>${project.build.directory}/config</targetPath>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.9.1</version>
</dependency>
<!-- more dependencies... -->
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
执行 jar 的 java 命令:
java -jar myjar.jar # 后面可输入参数,供程序使用