Spring Boot incorrectly loads test configuration when running from eclipse+gradle

大憨熊 提交于 2019-12-07 10:26:57

问题


When I run Spring Boot application from Eclipse (STS) as 'Java Application' or as 'Spring Boot App' the app fails because it tries to load configurations (@Configuration) from test classpath.

The thing is that it works for maven based builds correctly, but not for gradle.

The only workaround I see is to rename test package to something else to hide it from classpath scanning.

Overall the question is - how to make it work properly? I'd assume that SpringToolSuite team encounters this problem on a daily basis or I do something incorrectly.

Here's a test project which works when imported as maven, but fails when imported as gradle: https://github.com/mabn/spring-boot-integration-test

  • Here's a year-old STS issue - is it still relevant?
  • Here's another one marked as resolved
  • Similar question about maven projects

I'm using STS 3.7.1


回答1:


The thing is that it works for maven based builds correctly, but not for gradle.

Yes, unfortunately that is the case. Gradle doesn't deal properly with configuring eclipse's runtime classpath at all. This is even the case with Gradle's own new BuildShip eclipse plugin.

See this issue I recently raised: https://bugs.eclipse.org/bugs/show_bug.cgi?id=482315

The resolved bug you found is indeed resolved, but it only concerned the maven/m2e case, which is in fact fixable (m2e does this correcty for "Run As >> Java Project" and we adopted a similar fix for "Run As >> Spring Boot App"). However, that fix is maven-specific as the gradle case is not really fixable until this Gradle tooling-api issue ticket is resolved:

https://issues.gradle.org/browse/GRADLE-1777




回答2:


I had to introduce separation with profiles for this. Having all test marking with @ActiveProfiles("srv-test") and main configuration with @Profile({"local", "docker"})



来源:https://stackoverflow.com/questions/33817251/spring-boot-incorrectly-loads-test-configuration-when-running-from-eclipsegradl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!