前言
- 本文针对 普通maven工程 的 idea打jar包做介绍,并非springboot工程。
操作步骤
- 创建普通测试的maven工程,并修改pom文件
代码如下(示例):
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>jartest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 测试类代码JarTest
代码如下:
public class JarTest {
public static final String propFileName = "test.properties";
//运行jar包,获取jar包同级目录下的配置文件,加载配置文件,解析成Properties
static {
//加载配置文件,解析成Properties
Properties properties = null;
try {
InputStream fis = JarTest.class.getClassLoader().getResourceAsStream(propFileName);
properties = new Properties();
properties.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
Set<Map.Entry<Object, Object>> entries = properties.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
}
public static void main(String[] args) {
}
}
加载jar包同级目录下的test.properties文件:
name=zhangsan
sex=male
age=18
-
测试:
jar包在target文件夹下:
在jar包目录下打开cmd,输入指令:
java -jar jartest-1.0-SNAPSHOT.jar
执行代码,测试结果为:
总结
-
此种方法在代码未改变,再次进行打包时,不会重新生成新的jar包。若要生成新的jar包,需将原来的jar包删除即可。
-
若出现针对java版本的问题,需对settings、project structrue 和pom下的java版本修改,保证和本地的java版本保持一致。