Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
960 views
in Technique[技术] by (71.8m points)

使用loader.path后为何无法读取jar包内的配置文件?

我参照这个博客对springboot的jar包进行瘦身,瘦身成功,但发现无法读取到jar包里的配置文件了。我运行的命令如下:
java -Dloader.path="E:xxxmall-portalargetlib" -jar target/mall-portal-1.0-SNAPSHOT.jar
我看输出的日志里使用了8080端口启动(我配置文件里写的是其他端口),并且默认的配置文件环境变量为dev,推测是直接按照没有配置文件的形式启动的,事实上也完全没有读取到我配置文件里写的数据库url之类的信息。

我将配置文件一起放到lib目录下又可以正确运行了,通过命令行指定spring.profiles.active来切换配置文件也没有问题。

我不想把配置文件也放到lib下面,我觉得我哪里应该搞错了。

下图可以看到jar包中的配置文件是齐全的
image

以下是我的pom文件截取:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
                <layout>ZIP</layout>
                <includes>
                    <include>
                        <groupId>com.xxx.mall</groupId>
                        <artifactId>mall-portal</artifactId>
                    </include>
                </includes>
            </configuration>
        </plugin>

        <!--拷贝依赖到jar外面的lib目录-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <!--指定的依赖路径-->
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

emmm。。。好不容易试了一下,不过一般我是没有做过瘦身的操作,整了一个demo,按照你发的博客操作,我并没有遇到题主的问题哈

我也是按照类似题主的命名启动,
java -Dloader.path="E:xxxmall-portalargetlib" -jar target/mall-portal-1.0-SNAPSHOT.jar

最终是按照配置文件spring.profiles.active=dev的方式启动的。没有做把配置文件移到lib的操作哈。

通过命令行--spring.profiles.active也能动态修改配置文件。看起来没问题,如果配置文件真没生效,你可以做一个强制读取的操作,就是配置一个配置文件的读取路径
--spring.config.location=classpath:/
这样绝对能解决问题,因为这个是直接指定配置文件路径了,boot自己那套读取规则就被你这个覆盖掉了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...