【问题标题】:Class in main/resources not found未找到主要/资源中的类
【发布时间】:2015-08-12 08:21:46
【问题描述】:

我正在使用 Spring Boot gradle 插件来构建一个可执行的战争。我在 src/main/resources 中有一个 FindResource.java 类来定位文件:

FindResource.class.getResource(templateName).toURI()

当我执行 gradle build 时,我收到一个错误,即类 FindResource 无法解析。我是否需要 Spring Boot gradle 插件,它还应该使用资源目录中的类。我该怎么做?

我的 build.gradle 如下所示:

buildscript {
    ext {
        springBootVersion = '1.2.5.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
        classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot' 
apply plugin: 'io.spring.dependency-management' 

jar {
    baseName = 'abc'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-jersey")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.apache.pdfbox:pdfbox:1.8.10")
    compile('org.apache.poi:poi-ooxml:3.12')
    compile('org.apache.poi:poi-scratchpad:3.12')
    runtime("org.hsqldb:hsqldb")
    testCompile("org.springframework.boot:spring-boot-starter-test") 
}


task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

【问题讨论】:

  • 为什么不把类放到src/main/java,它所属的地方呢? src/main/resources 用于资源文件(顾名思义)。

标签: java gradle spring-boot


【解决方案1】:

正如注释中提到的,要加载的类文件需要在 src/main/java/ 中,而不是在 src/main/resources 中。 This link 可能有助于为您提供有关此结构约定的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 2019-04-30
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多