我的SpringBoot项目有一个外部jar,该jar需要获取一些configFile,配置文件位于src/main/resources/config
中,在mvn package
之后,config
位于BOOT-INF\classes\config
中,但是外部jar得到的配置路径是:/BOOT-INF/lib/config-xxx-1.0.8.jar!/config
,jar如何获得正确的路径?当我进行构思调试时,它可以获得正确的路径。
jar的代码:
public static String getConfigPathUtil() {
String configPath = ConfigPathUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();
try {
configPath = URLDecoder.decode(configPath, "utf-8");
} catch (UnsupportedEncodingException var2) {
throw new RuntimeException("Transcoding mistakes when the configuration file path" + var2.getMessage());
}
File f = new File(configPath);
configPath = f.getAbsolutePath();
if (configPath.endsWith(".jar")) {
configPath = configPath.substring(0, configPath.lastIndexOf(LIB_PATH));
}
if (configPath.endsWith(BIN_PATH_ENDSWITH)) {
configPath = configPath.substring(0, configPath.lastIndexOf(BIN_PATH_ENDSWITH));
}
configPath = configPath.replace(BIN_PATH, File.separator);
configPath = configPath + CONFIG_PATH;
return configPath;
}
maven pom:
<dependency>
<groupId>xxx</groupId>
<artifactId>config-xxx</artifactId>
<version>1.0.8</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/config-xxx-1.0.8.jar</systemPath>
</dependency>
springboo-maven-plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.xxx.ManagementApplication</mainClass>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>