无依赖包pom.xml配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<useUniqueVersions>false</useUniqueVersions>
<mainClass>
com.github.lzh.FileTools #main函数所在的类路径
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
MANIFEST.MF文件讲解:
Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.2.2 # maven-jar-plugin版本
Build-Jdk-Spec: 17 #jdk的版本
Main-Class: com.github.lzh.FileTools #对应的是mainClass配置项
有依赖包pom.xml配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!--拷贝项目中依赖包到lib目录下-->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<!--间接依赖拷贝-->
<excludeTransitive>false</excludeTransitive>
<!--带上版本号-->
<stripVersion>false</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
加载配置文件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resource</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!--目标文件所在的路径-->
<outputDirectory>${project.build.directory}/conf</outputDirectory>
<resources>
<resource>
<!--源文件所在的路径-->
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>