为什么 Spring Boot 在打包时(使用 Gradle)不加载应用程序的 yml 配置文件?

huangapple go评论58阅读模式
英文:

why spring boot does not load application yml when packaged (With gradle)?

问题

迄今为止还没有找到有趣的东西,所以我在这里,提一个问题。

我有一个运行良好的 Spring Boot 应用程序。问题是:自定义配置!
我希望它在 8081 端口上运行(而不是默认的 8080 端口)。所以我在 src/main/resources 目录中添加了 application.yml,打包它... 但它仍然在 8080 端口上运行。
当我从 IntelliJ 中运行 @SpringBootApplication 类时,它会在 8081 端口上运行(我刚刚添加了 application.yml 文件)。那么为什么呢?

这是一个 Gradle 多模块项目。我将 application.yml 文件添加到了实际打包为 jar 文件的模块中。它使用 Gradle Shadow 插件打包,并且在 jar 文件的根目录中包含了 application.yml 文件。

application.yml 文件内容如下:

server:
    port: 8081

我不太清楚你需要哪些信息,所以请随意提问!

欢迎提出任何想法。

编辑 1:
在启动服务器时的“堆栈跟踪”:

java -jar serverApp.jar 

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_.__/__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::

août 29, 2020 5:50:54 PM org.apache.coyote.AbstractProtocol init
INFOS: Initializing ProtocolHandler ["http-nio-8080"]
août 29, 2020 5:50:54 PM org.apache.catalina.core.StandardService startInternal
INFOS: Starting service [Tomcat]
août 29, 2020 5:50:54 PM org.apache.catalina.core.StandardEngine startInternal
INFOS: Starting Servlet engine: [Apache Tomcat/9.0.37]
août 29, 2020 5:50:54 PM org.apache.catalina.core.ApplicationContext log
INFOS: Initializing Spring embedded WebApplicationContext
août 29, 2020 5:50:55 PM org.apache.coyote.AbstractProtocol start
INFOS: Starting ProtocolHandler ["http-nio-8080"]
英文:

Not found anything interesting so far, so here am I, asking a question.

I got a spring boot application, that runs fine. Problem is : custom configuration !
I want it to run on port 8081 (and not default port 8080). So I added the application.yml in the src/main/resources directory, packaged it... And it run on port 8080
When I run the @SpringBootApplication class from intellij, it does run on port 8081 (I just added the application.yml file.) So why ?

It's a gradle multi module project. T added the application.yml file into the module that is actually packaged into a jar file. It is packaged using tha gradle shadow plugin, and does contain the application.yml file at the root of the jar file.

The application.yml is like this :

server:
    port: 8081

I don't really know which information you'll need, so feel free to ask !

Any idea is welcome.

EDIT 1 :
"Stacktrace" when I start the server :

java -jar serverApp.jar 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                        

août 29, 2020 5:50:54 PM org.apache.coyote.AbstractProtocol init
INFOS: Initializing ProtocolHandler ["http-nio-8080"]
août 29, 2020 5:50:54 PM org.apache.catalina.core.StandardService startInternal
INFOS: Starting service [Tomcat]
août 29, 2020 5:50:54 PM org.apache.catalina.core.StandardEngine startInternal
INFOS: Starting Servlet engine: [Apache Tomcat/9.0.37]
août 29, 2020 5:50:54 PM org.apache.catalina.core.ApplicationContext log
INFOS: Initializing Spring embedded WebApplicationContext
août 29, 2020 5:50:55 PM org.apache.coyote.AbstractProtocol start
INFOS: Starting ProtocolHandler ["http-nio-8080"]

答案1

得分: 2

没有看到实际代码,我可以推荐尝试以下两件事情。

1- 你是否忘记在主方法中使用参数 -

public static void main(String[] args) {
    SpringApplication.run(ServerApplication.class, args);
}

2- 尝试使用参数启动 Spring Boot 服务器 -

java -jar -Dserver.port=8081 ServerApplication.jar
英文:

Without seeing the actual code, I can recommend 2 things to try.

1- Have you missed to use the argument in main method-

public static void main(String[] args) {
    SpringApplication.run(ServerApplication.class, args);
}

2- Try to start the springboot server with arguments-

java -jar -Dserver.port=8081 ServerApplication.jar

答案2

得分: 0

看起来根据这个评论,Shadow Plugin 在使用 Spring Boot 时似乎不是合适的选择:

> 这个引导框架有自己的 Gradle 插件和方法,用于打包可分发的 jar 文件。我不认为我会在同一个项目中同时使用 shadow 和 boot 插件。它们会发生冲突。

还有这个评论。

英文:

It seems that Shadow Plugin is not the right thing to use with Spring Boot according to this comment:

> the boot framework has it's own gradle plugin and method for packaging ditributable jar files. I dont' think I would combine shadow and the boot plugin in the same project. They will collide.

and also this.

huangapple
  • 本文由 发表于 2020年8月29日 23:18:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63648589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定