简体   繁体   中英

Spring boot load test database when run from cmd

I have a spring boot project that load correctly with postgres when started from Eclipse but when started by the windows command window, the test database is loaded.

When I start the application from the cmd with java -jar happy_list-0.1.0.jar I have this error ( I removed part of the stacktrace) :

Caused by: java.lang.IllegalStateException: Driver for test database type [HSQL] is not available in the classpath
Caused by: java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver

I don't have any test or configuration for tests. It must be some auto-configuration done by Spring boot, but I don't understand why it behave differently when run from eclipse or windows cmd.

PersistentContext :

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "happy_listing" })
public class PersistentContext {

}

App.java :

@SpringBootApplication
@EnableAutoConfiguration
@Configuration
@ComponentScan(basePackages = { "happy_listing" })
@Import({ PersistentContext.class })
public class App {

    @Configuration
    @PropertySource("classpath:application.properties")
    static class ApplicationProperties {
    }

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

}

Build.gradle :

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
        classpath('se.transmode.gradle:gradle-docker:1.2')
    }
}

group = 'marcandregirard'

apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'docker'

jar {
    baseName = 'happy_list'
    version =  '0.1.0'
}

springBoot {
  mainClass = "happy_listing.App"
}
repositories {
    mavenCentral()
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.13'
    compile 'javax.ws.rs:javax.ws.rs-api:2.0'
    compile 'org.springframework:spring-core'
    compile 'org.springframework:spring-context'
    compile 'org.springframework:spring-jdbc'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1'
    compile 'org.springframework.data:spring-data-jpa:1.9.2.RELEASE'
    compile 'org.hibernate:hibernate-entitymanager'
    compile 'org.postgresql:postgresql:9.4-1206-jdbc42'
    compile 'org.springframework:spring-web'
    testCompile 'junit:junit:4.12'
}

task buildDocker(type: Docker, dependsOn: build) {
  push = true
  applicationName = jar.baseName
  dockerfile = file('src/main/docker/Dockerfile')
  doFirst {
    copy {
      from jar
      into stageDir
    }
  }
}

I created the jar with the command gradle build buildDocker which create the jar and image to run in Docker.

Everything is working fine when started form Eclipse.

For starters I would start cleaning up your dependencies instead of all separate jar files use the appropriate starters.

dependencies {
    compile 'javax.ws.rs:javax.ws.rs-api:2.0'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1'
    compile 'org.postgresql:postgresql:9.4-1206-jdbc42'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

Next it looks like you are trying to do a lot of manual configuration @SpringBootApplication already implies the other 3 and Spring Boot already loads the application.properties . Remove that

@SpringBootApplication
public class App {

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

}

Remove your PersistenceContext class as that is already done by Spring Boot for you.

After cleaning up your classes and dependencies make sure that no old classes are left. For that execute the Gradle clean task. gradle clean should do the trick.

So when removing classes also make sure you do a gradle clean build instead of a plain gradle build .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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