Spring Tools Suite和Gradle - 安装使用STS内部的正确资源 [英] Spring Tools Suite and Gradle - Setup to use correct resources from inside STS

查看:976
本文介绍了Spring Tools Suite和Gradle - 安装使用STS内部的正确资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Tools Suite(3.7.2 RELEASE)中使用以下源文件夹设置Spring Boot Gradle项目:

   -  src / integration-test / java 
- src / integration-test / resources
- src / main / java $ b $ s - src / main / resources $ b $ s - src / test / java
- src / test / resources`

每当我运行应用程序或单元测试时在STS中,我发现STS正在使用 src / integration-test / resources 下的资源。

我在STS中看到存在于所有3个资源源文件夹中的重复资源警告。例如,我在所有3个源文件夹中都有一个application.properties,我看到以下内容:

 该资源是src /integration-test/resources/application.properties并且未复制到输出文件夹

如果我运行应用程序作为JAR或单元测试/集成测试(通过gradle build),似乎一切都使用了正确的资源。这让我相信这是STS / Eclipse如何处理gradle的问题。



有谁知道我如何配置STS以使用正确的资源源我想我的问题可能与(或相同?) href =https://issuetracker.springsource.com/browse/STS-3882 =nofollow noreferrer> https://issuetracker.springsource.com/browse/STS-3882 https://issues.gradle.org/browse/GRADLE-1777



我也尝试了这里找到的解决方案,但是这似乎只修复了Maven构建:
Spring Tool Suite发现弹簧引导集成测试配置并且不会启动主应用程序




我的问题可能与...有关...是的,这是相关的,但在我看来是不一样的。该问题是由运行时类路径不正确引起的。这个问题是来自eclipse项目构建器的一个错误,所以这是一个编译时问题。



虽然这些问题密切相关。根据你的观点,你可以说它们是相同的(测试和编译时类路径混合不正确)。



这里,具体来说,问题是eclipse构建器会尝试将它在源文件夹中找到的所有资源复制到项目的单个输出文件夹中。每个源文件夹都有一个'application.properties'。该建筑商警告说,它不能复制其中的一些,因为会覆盖另一个。



我认为可能有解决这个问题的办法。但它确实应该来自Gradle +(BuildShip | STS Gradle Tooling)而不是您自己的解决方案。



Eclipse中可以分别配置每个源文件夹定位到特定的输出文件夹。 Maven + M2E正在做这件事,但Gradle +(BuildsShip | STS Gradle Tooling)combo不会。



例如,这是maven在配置测试资源文件夹时放入eclipse .classpath文件中的内容:

 < classpathentry excluded =**kind =srcoutput =target / test-classespath =src / test / resources> 
<属性>
< attribute name =maven.pomderivedvalue =true/>
< / attributes>
< / classpathentry>

请注意,它是如何为该条目显式设置输出文件夹(与项目的默认输出文件夹不同)。

您可以通过类似方式修改gradle项目的.classpath来解决问题。要么通过手动或从你的build.gradle。



我不确定这是否值得,但您仍然可能会遇到运行时类路径问题(因为这些文件夹仍将添加到您的运行时类路径,您的运行时类路径将以两个appication.properties资源结束,一个会影响另一个资源。请参阅: https://bugs.eclipse.org/bugs/show_bug.cgi?id=482315



我会说,正确的做法是对问题I 添加评论并希望他们尽快修复它,因为只有这么多,你可以通过黑客build.gradle文件来修改.classpath(这不能解决运行时类路径问题,但为了解决运行时类路径问题,他们将不得不配置源文件夹以定位与m2e类似的单个输出文件夹)。


I have a Spring Boot Gradle project setup in Spring Tools Suite (3.7.2 RELEASE) with the following source folders:

- src/integration-test/java
- src/integration-test/resources 
- src/main/java
- src/main/resources
- src/test/java
- src/test/resources`

Whenever I run the application or unit tests from within STS, I see that STS is using the resources found under src/integration-test/resources.

I see a duplicate resource warning in STS for files which exist in all 3 resource source folders. For example, I have an application.properties in all 3 source folders and I see following:

The resource is a duplicate of src/integration-test/resources/application.properties and was not copied to the output folder

If I run the application as a JAR or unit tests/integration tests from the command line (via gradle build), everything seems to use the correct resources. This makes me believe it is a problem with how STS/Eclipse is handling gradle.

Does anybody know of how I can configure STS to use the correct resource source folders when using gradle?

I think my problem may be related to (or the same as?) Spring Boot incorrectly loads test configuration when running from eclipse+gradle, https://issuetracker.springsource.com/browse/STS-3882, https://issues.gradle.org/browse/GRADLE-1777

I also tried the solution found here, but that seems to only fix Maven builds: Spring Tool Suite finds spring-boot integration test configuration and does not start main application

解决方案

I think my problem may be related to...

Yes, it is related but in my opinion not the same. That problem is caused by the runtime classpath being incorrect. This problem is an error coming from the eclipse project builder so it is a compile-time issue.

The problems are closely related though. Depending on your point of view, you could say they are the same (incorrect mixing of test and compile-time classpaths).

Here, specifically, the problem is that the eclipse builder tries to copy all the resources it finds in source folders to the project's single output folder. Each source folder has a 'application.properties'. The builder warns that it could not copy some of them because one would overwrite the other.

I think there may be a solution for this problem. But it is a solution that really should come from Gradle + ( BuildShip | STS Gradle Tooling) than from you.

It is possible in Eclipse to configure each source-folder individually to target a specific outputfolder. Maven + M2E are doing this correcty, but Gradle + (BuildsShip | STS Gradle Tooling) combdos do not.

For example this is what maven puts into the eclipse .classpath file when it configures a test resources folder:

    <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
         <attributes>
            <attribute name="maven.pomderived" value="true"/>
         </attributes>
     </classpathentry>

Notice how it explicitly sets the output folder for that entry (to something different from the project's default output folder).

You may be able to address the problem yourself by modifying the .classpath for a gradle project in a similar way. Either by doing it manually or from your build.gradle.

I'm not sure this is worth it however as you will then likely still get hit by the runtime classpath issue (since these folders will still be added to your runtime classpath, your runtime classpath will end-up with two appication.properties resources, one which will 'shadow' the other. See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=482315)

I would say, the right thing to do is add a comment to the issue I linked, and hope they fix it soon as there is only so much you can do yourself by hacking the build.gradle file to modify the .classpath (this can not solve the runtime classpath issue, but in order to solve the runtime classpath issue, they would have to configure source folders to target individual output folder similar to what m2e does).

这篇关于Spring Tools Suite和Gradle - 安装使用STS内部的正确资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆