簡體   English   中英

當包含Gradle Vaadin插件時,以Spring Boot“ bootRun”啟動的應用程序導致NoClassDefFoundError

[英]Application started with Spring Boot “bootRun” causes NoClassDefFoundError when including Gradle Vaadin plugin

我的目標是建立一個簡單的多模塊項目,該項目使用Spring Boot 2,Gradle 4.x和Vaadin 8.x作為UI。 Vaadin當前僅在一個子項目中使用,其他子項目提供服務或僅是庫。

我從這個非常不錯的Spring教程開始, 該教程 (盡管使用了較舊的Gradle語法)產生了一個可以運行的測試項目。 它包含一個“ libaray”子項目和一個“應用程序”子項目,具體取決於前者。 在我將某些配置集中在父build.grade中之后,“ bootRun”和“ bootJar” Gradle任務都像魅力一樣工作,並且仍然可以工作。

然后,我將Vaadin Gradle插件添加到“應用程序”項目中,並更改了端點以顯示簡單的vaadin標簽。

問題是,現在使用“ bootRun”時,啟動的應用程序不再能夠從其依賴的“庫”中訪問類。 如果刪除代碼中的任何依賴關系,該應用程序將運行,但是一旦我從“庫”(例如,“ MyService”)引用一個類,它就會崩潰並顯示“ java.lang.ClassNotFoundException”。

但是,當使用“ bootJar” 部署相同的應用程序並運行jar時,一切正常,因此可以解決模塊間的依賴關系。

現在我想知道我是否缺乏理解,而Vaadin Gradle插件是否需要對模塊依賴項進行其他/不同的配置? 還是這可能是Vaadin Gradle插件中的錯誤或我的配置中的問題?

我沒有獲得任何提示就已經閱讀了該插件的完整文檔,已經在網上搜索了,但是我既沒有在此特定上下文中找到“ NoClassDefFoundError”,也沒有在Spring Boot和Vaadin中找到有效的多模塊項目示例8。

以下是相關的示例代碼(不包括導入):

hello.app.DemoApplication

@SpringBootApplication(scanBasePackages = "hello")
@RestController
public class DemoApplication {
    //Service defined in dependent sub-project
    private final MyService myService;

    public DemoApplication(MyService myService) {
        this.myService = myService;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

hello.app.StartUI

/** Entry point for the Vaadin 8 UI */
@SpringUI
@SpringView
public class StartUI extends UI implements View {

    @Override
    protected void init(final VaadinRequest request) {
        setContent(new Label("Hello vaadin"));
    }
}

gradle.properties

plugins { id "io.spring.dependency-management" version "1.0.5.RELEASE" }
ext { springBootVersion = '2.0.3.RELEASE' }
jar {
    baseName = 'library'
    version = '0.0.1-SNAPSHOT'
}
dependencies {
    implementation('org.springframework.boot:spring-boot-starter')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
    imports { mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") }
}

應用程序gradle.properties

plugins {
    id "io.spring.dependency-management" version "1.0.5.RELEASE"
    id "org.springframework.boot" version "2.0.3.RELEASE"
    id 'com.devsoap.plugin.vaadin'version '1.3.1' //Add vaadin support
}
bootJar {
    baseName = 'application'
    version = '0.0.1-SNAPSHOT'
}
dependencies {
    implementation project(':library') // Contains "MyService"
    implementation('org.springframework.boot:spring-boot-starter-actuator')
    implementation('org.springframework.boot:spring-boot-starter-web')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

父(根)項目build.gradle

subprojects {
    //Only plugins configured here applied to sub-projects.
    apply plugin: "java-library"
    apply plugin: "eclipse"
    repositories { mavenCentral() } 
    sourceCompatibility = 1.8
}

當然,父settings.gradle引用子項目。

堆棧跟蹤是(有些縮短)

java.lang.IllegalStateException: Cannot load configuration class: hello.app.DemoApplication
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:414) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:254) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:128) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    ...
Caused by: java.lang.IllegalStateException: Unable to load cache item
    at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:79) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:337) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:138) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:110) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:403) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 12 common frames omitted
Caused by: java.lang.NoClassDefFoundError: hello/service/MyService
    at java.lang.Class.getDeclaredConstructors0(Native Method) ~[na:1.8.0_151]
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) ~[na:1.8.0_151]
    at java.lang.Class.getDeclaredConstructors(Class.java:2020) ~[na:1.8.0_151]
    at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:566) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ...
Caused by: java.lang.ClassNotFoundException: hello.service.MyService
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_151]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_151]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_151]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_151]
    ... 34 common frames omitted

更多信息:我在Eclipse Photon中將Buildship 2.2插件與Gradle 4.3結合使用。

我看到的幾件事:

  • 您不是在構建Java庫,而是在構建Java Web應用程序,因此省略“ java-library”插件。

  • 管理插件的一種更好的策略是使用apply false參數其全部注冊到項目中。 然后在子項目中正常應用插件。 這樣一來,您可以將所有插件依賴項版本集中管理

  • 實現配置是相當新的,許多插件不支持它(例如gretty)。 我將其改為使用compiletestCompile

  • Spring Dependency Management插件確實是侵入性的,並非所有插件都支持(Gradle Vaadin插件之一)。 我會跳過它,而是使用Gradle的BOM支持。 https://docs.gradle.org/4.6/userguide/managing_transitive_dependencies.html#sec:bom_import

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM