项目结构:
目的是为了方便 Main 方法读取到配置文件就这样了,如果有目录层级则在 Main 方法里面添加相应的即可
Maven-<build>节点下的<resources>节点
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
<!-- 把配置打到外部使用 -->
<targetPath>${project.build.directory}</targetPath>
</resource>
</resources>
意思就是把resource下面的东西打包到外面,不在jar包里面,如图:
最重要的一点:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!--MANIFEST.MF 中 Class-Path 加入前缀-->
<classpathPrefix>lib/</classpathPrefix>
<!--jar包不包含唯一版本标识-->
<!--<useUniqueVersions>false</useUniqueVersions>-->
<!--指定入口类-->
<mainClass>com.xxx.ApplicationMain</mainClass>
</manifest>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependency</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
最后可以愉快的执行 jar 了
我的完整 <build> 节点:
<build>
<finalName>xxxx-server</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
<!-- 把配置打到外部使用 -->
<targetPath>${project.build.directory}</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!--MANIFEST.MF 中 Class-Path 加入前缀-->
<classpathPrefix>lib/</classpathPrefix>
<!--jar包不包含唯一版本标识-->
<!--<useUniqueVersions>false</useUniqueVersions>-->
<!--指定入口类-->
<mainClass>com.xxxx.ApplicationMain</mainClass>
</manifest>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependency</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<encoding>UTF-8</encoding>
<delimiters>
<delimiter>${*}</delimiter>
<delimiter>@*@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>${compiler.version}</source>
<target>${compiler.version}</target>
<encoding>utf-8</encoding>
<skip>true</skip><!-- 跳过测试 -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<skipTests>true</skipTests><!-- 跳过测试 -->
</configuration>
</plugin>
</plugins>
</build>