0

I am new to spring boot and gradle. I am creating a spring boot application and want to use JDBC Template connection. My build.gradle is :

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


apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    jcenter()
}

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.2.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '1.5.2.RELEASE'

    compile project(':supplier-commons')
    runtime project(':supplier-commons')

    compile 'org.springframework:spring-context:4.2.6.RELEASE'
    compile 'org.springframework:spring-webmvc:4.2.6.RELEASE'
    compile 'org.springframework.data:spring-data-jpa:1.10.1.RELEASE'
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.7'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.7'
    compile group: 'com.typesafe.akka', name: 'akka-actor_2.11', version: '2.4.1'
    compile 'io.jsonwebtoken:jjwt:0.7.0'
    compile group: 'javax.mail', name: 'mail', version: '1.4'
    compile group: 'freemarker', name: 'freemarker', version: '2.3.9'
    compile group: 'org.springframework.security', name: 'spring-security-crypto', version: '4.2.2.RELEASE'
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.4'
    compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.9'
    compile group: 'org.jmockit', name: 'jmockit', version: '1.8'
    compile 'io.jsonwebtoken:jjwt:0.7.0'
    compile group: 'com.ning', name: 'async-http-client', version: '1.9.40'
    compile group: 'org.mockito', name: 'mockito-all', version: '1.8.4'
    compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.0.7'

    compile('org.springframework.boot:spring-boot-starter-jdbc'){
        exclude module : 'spring-boot-starter-logging'
        exclude module: "logback-classic"
    }
    runtime('org.springframework.boot:spring-boot-devtools')
    compileOnly('org.springframework.boot:spring-boot-configuration-processor')

    testCompile 'junit:junit:4.12'

}

springBoot {
    executable = true
}

eclipse {

  wtp {
    facet {
        facet name: 'jst.web', version: '2.4'
    }

    component {
      contextPath = 'aggregator-api'
    }

  }
}

When I run the application, I get following error :

java.lang.NoClassDefFoundError: javax/persistence/EntityManagerFactory
    at org.springframework.data.jpa.util.BeanDefinitionUtils.<clinit>(BeanDefinitionUtils.java:53) ~[spring-data-jpa-1.10.1.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.EntityManagerBeanDefinitionRegistrarPostProcessor.postProcessBeanFactory(EntityManagerBeanDefinitionRegistrarPostProcessor.java:54) ~[spring-data-jpa-1.10.1.RELEASE.jar:na]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:181) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at com.etl.aapi.springboot.main.AggregatorApplication.main(AggregatorApplication.java:14) [bin/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_112]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_112]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_112]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.2.RELEASE.jar:1.5.2.RELEASE]
Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityManagerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_112]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_112]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_112]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_112]
    ... 18 common frames omitted

2017-05-05 11:46:05.465  INFO 10960 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@67a280dd: startup date [Fri May 05 11:46:03 IST 2017]; root of context hierarchy
2017-05-05 11:46:05.468  WARN 10960 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Exception thrown from LifecycleProcessor on context close

java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@67a280dd: startup date [Fri May 05 11:46:03 IST 2017]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:415) [spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:975) [spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:934) [spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:794) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:325) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at com.etl.aapi.springboot.main.AggregatorApplication.main(AggregatorApplication.java:14) [bin/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_112]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_112]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_112]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

But when I remove

compile('org.springframework.boot:spring-boot-starter-jdbc'){
        exclude module : 'spring-boot-starter-logging'
        exclude module: "logback-classic"
    }

I don't get any error but than I can't use jdbc connection.

Any solution?

Vivek Mangal
  • 532
  • 1
  • 8
  • 24
  • try [this](http://stackoverflow.com/questions/25639515/java-lang-classnotfoundexception-javax-persistence-persistence-cannot-be-found) – XtremeBaumer May 05 '17 at 06:33

1 Answers1

1

For starters cleanup your build, you are trying to be too smart in there. Spring Boot has a lot of starters which you are including but also trying to bypass.

dependencies {
    compile project(':supplier-commons')

    compile ('org.springframework.boot:spring-boot-starter') {
      exclude module : 'spring-boot-starter-logging'
    }
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-freemarker'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2'

    compile group: 'mysql', name: 'mysql-connector-java'
    compile group: 'com.typesafe.akka', name: 'akka-actor_2.11', version: '2.4.1'
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.4'
    compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.9'
    compile 'io.jsonwebtoken:jjwt:0.7.0'
    compile group: 'com.ning', name: 'async-http-client', version: '1.9.40'
    compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.0.7'

    testCompile group: 'org.jmockit', name: 'jmockit', version: '1.8'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'

    runtime('org.springframework.boot:spring-boot-devtools')
    compileOnly('org.springframework.boot:spring-boot-configuration-processor')
}

Something like that. This will pull in all the versions as defined by Spring Boot (notice a lot of the dependencies don't specify a version) and for the technologies you had declared you want to use the starters (so that you get the Spring Boot managed version).

M. Deinum
  • 115,695
  • 22
  • 220
  • 224