Spring工具套件和Gradle-设置以使用STS内部的正确资源

Spring工具套件和Gradle-设置以使用STS内部的正确资源,gradle,spring-tool-suite,Gradle,Spring Tool Suite,我在Spring Tools Suite(3.7.2版本)中有一个Spring Boot Gradle项目设置,其中包含以下源文件夹: - src/integration-test/java - src/integration-test/resources - src/main/java - src/main/resources - src/test/java - src/test/resources` 每当我从STS内部运行应用程序或单元测试时,我都会看到STS正在使用src/integr

我在Spring Tools Suite(3.7.2版本)中有一个Spring Boot Gradle项目设置,其中包含以下源文件夹:

- src/integration-test/java
- src/integration-test/resources 
- src/main/java
- src/main/resources
- src/test/java
- src/test/resources`
每当我从STS内部运行应用程序或单元测试时,我都会看到STS正在使用
src/integration test/resources
下的资源

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

The resource is a duplicate of src/integration-test/resources/application.properties and was not copied to the output folder
如果我从命令行(通过gradlebuild)以JAR或单元测试/集成测试的形式运行应用程序,那么一切似乎都使用了正确的资源。这让我相信STS/Eclipse如何处理gradle是一个问题

有人知道在使用gradle时如何配置STS以使用正确的资源源文件夹吗?

我认为我的问题可能与(或与?)有关

我还尝试了这里找到的解决方案,但这似乎只修复了Maven构建:

我想我的问题可能与

是的,这是相关的,但在我看来不一样。该问题是由运行时类路径不正确引起的。这个问题是来自eclipseprojectbuilder的错误,因此它是一个编译时问题

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

具体来说,这里的问题是eclipsebuilder试图将它在源文件夹中找到的所有资源复制到项目的单个输出文件夹中。每个源文件夹都有一个“application.properties”。生成器警告无法复制其中的一些,因为其中一个将覆盖另一个

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

在Eclipse中,可以将每个源文件夹单独配置为目标特定的outputfolder。Maven+M2E正在正确地执行此操作,但Gradle+(Buildship | STS Gradle Tooling)combdos没有

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

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

)


我想说,正确的做法是解决我链接的问题,并希望他们尽快解决它,因为您自己只能通过破解build.gradle文件来修改.classpath(这无法解决运行时类路径问题,但为了解决运行时类路径问题,他们必须将源文件夹配置为与m2e类似的目标单个输出文件夹)。

我会将此作为注释添加到@Kris的答案中,但它太长了

通过将下面的代码添加到我的
build.gradle
文件中,我已经解决了运行时类路径问题。该代码为Spring Boot应用程序类生成Eclipse启动配置,并且只包括
运行时
类路径(即没有测试JAR)

我的项目使用Gradle
'eclipse'
插件生成eclipse项目文件(然后将其导入eclipse)。运行
eclipsepasspath
Gradle目标将在项目的根目录中生成启动文件

def mainClassName = "com.example.MyApplication"
task eclipseApplicationLaunch {
    group "IDE"
    description "Generate an Eclipse launch configuration file for the Spring Boot application class"
}
eclipseApplicationLaunch << {
    def writer = new FileWriter("${mainClassName.substring(mainClassName.lastIndexOf(".")+1)}.launch")
    def xml = new groovy.xml.MarkupBuilder(writer)
    xml.doubleQuotes = true 

    xml.launchConfiguration(type: "org.eclipse.jdt.launching.localJavaApplication") { 
        listAttribute(key:"org.eclipse.debug.core.MAPPED_RESOURCE_PATHS") {
            listEntry(value:"/${project.name}/src/main/java/${mainClassName.replace(".","/")}.java")
        }
        listAttribute(key:"org.eclipse.debug.core.MAPPED_RESOURCE_TYPES") {
            listEntry(value:"1")
        }
        listAttribute(key:"org.eclipse.jdt.launching.CLASSPATH") {
            listEntry(value:"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n<runtimeClasspathEntry containerPath=\"org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/\" javaProject=\"${project.name}\" path=\"1\" type=\"4\"/>\r\n")
            listEntry(value:"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n<runtimeClasspathEntry path=\"3\" projectName=\"${project.name}\" type=\"1\"/>\r\n")
            configurations.runtime.resolvedConfiguration.resolvedArtifacts.each { artifact ->
                def filePath = artifact.file.canonicalPath.replace("\\","/")
                listEntry(value:"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n<runtimeClasspathEntry externalArchive=\"${filePath}\" path=\"3\" type=\"2\"/>\r\n")
            }
        }
        booleanAttribute(key:"org.eclipse.jdt.launching.DEFAULT_CLASSPATH", value:"false")
        stringAttribute(key:"org.eclipse.jdt.launching.MAIN_TYPE", value:"${mainClassName}")
        stringAttribute(key:"org.eclipse.jdt.launching.PROGRAM_ARGUMENTS", value:"--spring.profiles.active=local --spring.config.location=conf/")
        stringAttribute(key:"org.eclipse.jdt.launching.PROJECT_ATTR", value:"${project.name}")
        stringAttribute(key:"org.eclipse.jdt.launching.VM_ARGUMENTS", value:"-Djava.net.preferIPv4Stack=true")
    }
    writer.close()
}
eclipseClasspath.dependsOn eclipseApplicationLaunch
def mainClassName=“com.example.MyApplication”
任务Eclipse应用程序启动{
组“IDE”
描述“为Spring Boot应用程序类生成Eclipse启动配置文件”
}
eclipseApplicationLaunch
def filePath=artifact.file.canonicalPath.replace(“\\”,“/”)
listEntry(值:“\r\n\r\n”)
}
}
booleanAttribute(键:“org.eclipse.jdt.launching.DEFAULT_CLASSPATH”,值:“false”)
stringAttribute(键:“org.eclipse.jdt.launching.MAIN_TYPE”,值:“${mainClassName}”)
stringAttribute(键:“org.eclipse.jdt.launching.PROGRAM_ARGUMENTS”,值:“--spring.profiles.active=local--spring.config.location=conf/”)
stringAttribute(键:“org.eclipse.jdt.launching.PROJECT_ATTR”,值:“${PROJECT.name}”)
stringAttribute(键:“org.eclipse.jdt.launching.VM_参数”,值:“-Djava.net.preferIPv4Stack=true”)
}
writer.close()
}
eclipseClasspath.dependsOn eclipseApplicationLaunch

我没有按照Kris的建议修改Eclipse
.classpath
文件。相反,我将
@Profile(“test”)
添加到我的测试应用程序类和
@ActiveProfiles(“test”)
添加到我的测试类中。

我在Eclipse bugzilla罚单中添加了一条注释,引用了以下问题:我看到您已经与这个运行时类路径问题斗争了很长一段时间。感谢您验证了这个问题。我认为我的设置肯定有问题,因为查找相关问题并不容易,我会在创建新项目时立即看到这个问题。目前我只是从STS外部运行。