繁体   English   中英

无法找到或加载主类 gradle 7.0 spring-boot 2.5.6

[英]Could not find or load main class gradle 7.0 spring-boot 2.5.6

我刚刚将 Spring Boot 版本“2.2.1.RELEASE”更新为 2.5.6,同时我还将 gradle 版本更新为 7.0。 更新前一切正常,但更新后似乎 bootRun 任务找不到主类。

这是收到的错误:

此版本中使用了已弃用的 Gradle 功能,使其与 Gradle 8.0 不兼容。 使用“--warning-mode all”来显示单个弃用警告。 请参阅https://docs.gradle.org/7.0/userguide/command_line_interface.html#sec:command_line_warnings 12 个可操作任务:12 个已执行

错误:无法找到或加载主类 com.test.TestApplication

FAILURE:构建失败,出现异常。

我遵循了文档https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#running-your-application并且我有以下build.gradle

buildscript {
    ext {
        springBootVersion = '2.5.6'
    }
    repositories {
    // ..
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "org.springframework.boot:spring-boot-starter-jersey:${springBootVersion}"
        classpath("net.ltgt.gradle:gradle-apt-plugin:0.15")
        classpath 'gradle.plugin.com.palantir.gradle.gitversion:gradle-git-version:0.11.0'
    }
}

apply plugin: "java"
apply plugin: "war"
apply plugin: 'eclipse'
apply plugin: "idea"
apply plugin: "org.sonarqube"
apply plugin: "jacoco"
apply plugin: "maven-publish"
apply plugin: "net.ltgt.apt"
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.palantir.git-version'

def mapStructVersion = "1.3.0.Final"
def swaggerVersion = "1.6.3"
def junitVersion = "4.13.2"

sourceCompatibility = 1.8

sourceSets {
    main {
        java {
            srcDir "${buildDir.absolutePath}/generated/source/apt/main"
        }
    }
    test {
        java {
            srcDir "${buildDir.absolutePath}/generated/source/apt/main"
        }
    }
}

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, "seconds"
}

repositories {
    // ...
}

task preBuild {
    delete "${buildDir.absolutePath}/generated/source/apt/main"
}

build.dependsOn preBuild

configurations {
    developmentOnly.extendsFrom compile
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

/**
 * Fix extension/file too long issue under windows
 */
task pathingJar(type: Jar) {
    dependsOn configurations.developmentOnly
    archiveAppendix = 'pathing'

    doFirst {
        manifest {
            attributes "Class-Path": configurations.developmentOnly.files.collect {
                it.toURI().toString().replaceFirst(/file:\/+/, '/')
            }.join(' ')
        }
    }
}

/**
 * With gradle 7, a duplicate strategy must be set in order to not encountering error during copy operation.
 * Solution: EXCLUDE strategy do not allow duplicates by ignoring subsequent items to be created at the same path.
 */
processIntegrationTestResources {
    duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
}

bootRun {
    main = 'com.test.TestApplication'
    dependsOn pathingJar
    doFirst {
        classpath = files(sourceSets.main.output.files, pathingJar.archiveFile)
    }
    def debugPort = project.properties["${project.name}.debugPort"]
    if (debugPort) {
        jvmArgs = ["-Xdebug", "-Xnoagent", "-Djava.compiler=NONE", "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debugPort}"]
    }
    if (System.getProperty("LOG_PATH") == null) {
        System.setProperty("LOG_PATH", project.projectDir.getCanonicalPath() + "/log")
    }
    if (System.getProperty("spring.profiles.active") == null) {
        System.setProperty("spring.profiles.active", "some-profile")
    }
    if (System.getProperty("servicelayer.rrLogHumanReadable") == null) {
        System.setProperty("servicelayer.rrLogHumanReadable", "true")
    }
    systemProperties = System.properties
}

def compileDependencies = [
        "org.springframework.cloud:spring-cloud-starter-bootstrap:3.0.4",
        "org.springframework.boot:spring-boot-starter-web",
        "org.springframework.boot:spring-boot-starter-jersey",
        "org.springframework.boot:spring-boot-starter-security",
        "org.springframework.boot:spring-boot-starter-actuator",
        "org.springframework.boot:spring-boot-starter-data-jpa",
        "org.springframework.boot:spring-boot-starter-cache",
        "io.swagger:swagger-jersey2-jaxrs:${swaggerVersion}",
        "io.jsonwebtoken:jjwt:0.9.0",
        "org.bitbucket.b_c:jose4j:0.6.3",
        'org.flywaydb:flyway-core',
        "org.mapstruct:mapstruct-jdk8:${mapStructVersion}",
        "com.github.ben-manes.caffeine:caffeine",
        'org.bouncycastle:bcprov-jdk15on:1.60'
]

// defined some dependencies. Not relevant

dependencies {
    implementation compileDependencies
    providedRuntime providedRuntimeDependencies
    testImplementation testCompileDependencies
}

我尝试了两种方式,从命令行和从 ide (IntelliJ)

clean bootRun -Dspring.profiles.active="some-profile"

我找到了解决方案,问题出在任务 pathingJar 中

前:

  task pathingJar(type: Jar) {
    dependsOn configurations.developmentOnly
    archiveAppendix = 'pathing'

    doFirst {
        manifest {
            attributes "Class-Path": configurations.developmentOnly.files.collect {
                it.toURI().toString().replaceFirst(/file:\/+/, '/')
            }.join(' ')
        }
    }
}

后:

task pathingJar(type: Jar) {
    dependsOn configurations.runtimeClasspath
    archiveAppendix = 'pathing'

    doFirst {
        manifest {
            attributes "Class-Path": configurations.runtimeClasspath.files.collect {
                it.toURI().toString().replaceFirst(/file:\/+/, '/')
            }.join(' ')
        }
    }
} 

所以解决的办法是改下面两行代码

dependsOn configurations.developmentOnly -> dependsOn configurations.runtimeClasspath

attributes "Class-Path": configurations.developmentOnly.files.collect {...} -> attributes "Class-Path": configurations.runtimeClasspath.files.collect {...}

长话短说,不知何故,另一个任务在developmentOnly范围内干扰了这个任务,我不得不将它更改为runtimeClasspah

编辑:

另一个问题可能在bootRun任务内部。 您可以定义主类所在的包。

bootRun {
    main = 'edu.somepacke.MyMainClass'
    dependsOn pathingJar
    doFirst {
        classpath = files(sourceSets.main.output.files, pathingJar.archiveFile)
    }
    //extra logic
}  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM