Jenkins无法创建entityManagerFactory

时间:2018-03-28 19:28:47

标签: spring spring-boot jenkins spring-security spring-data-jpa

我为我的应用程序创建了以下测试:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class exampleTests {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mvc;
    private WebClient webClient;

    @LocalServerPort
    int port;

    @Before
    public void setup() {
        this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
        this.webClient = MockMvcWebClientBuilder.webAppContextSetup(context, springSecurity()).build();
    }

    @Test
    public void mvcTest() throws Exception {
        mvc.perform(get("/")).andExpect(status().isOk());
    }

    @Test
    public void clientTest() throws FailingHttpStatusCodeException, MalformedURLException, IOException {
        HtmlPage loginPage = webClient.getPage("http://localhost:"+port+"/app/");
        List<DomElement> pageList = loginPage.getElementsByTagName("h1");
        DomElement page = pageList.get(0);
        String text = page.getTextContent();
        assertThat(text).isEqualTo("Please log in");
    }
}

当我在本地计算机上执行mvn安装时,一切正常。但Jenkins在构建之后返回以下堆栈跟踪:

  

org.springframework.beans.factory.BeanCreationException:错误   创建名为&entityManagerFactory&#39;的bean在类路径中定义   资源   [组织/ springframework的/引导/自动配置/ ORM / JPA / HibernateJpaConfiguration.class]:   调用init方法失败;嵌套异常是   javax.persistence.PersistenceException:无法解析持久性   单位根URL         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1710)   〜[弹簧豆-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)   〜[弹簧豆-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)   〜[弹簧豆-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:312)   〜[弹簧豆-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)   〜[弹簧豆-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)   〜[弹簧豆-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)   〜[弹簧豆-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1085)   〜[弹簧上下文5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858)   〜[弹簧上下文5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)   〜[弹簧上下文5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)   〜[弹簧引导2.0.0.RELEASE.jar:2.0.0.RELEASE]         在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)   〜[弹簧引导2.0.0.RELEASE.jar:2.0.0.RELEASE]         在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388)   〜[弹簧引导2.0.0.RELEASE.jar:2.0.0.RELEASE]         在org.springframework.boot.SpringApplication.run(SpringApplication.java:327)   〜[弹簧引导2.0.0.RELEASE.jar:2.0.0.RELEASE]         在org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:138)   [弹簧引导测试2.0.0.RELEASE.jar:2.0.0.RELEASE]         at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)   [弹簧引导测试自动配置-2.0.0.RELEASE.jar:2.0.0.RELEASE]         在org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)   [junit的-4.12.jar:4.12]         在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)[junit-4.12.jar:4.12]         在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)   [junit的-4.12.jar:4.12]         在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)   [junit的-4.12.jar:4.12]         在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)   [junit的-4.12.jar:4.12]         在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)   [junit的-4.12.jar:4.12]         在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.junit.runners.ParentRunner.run(ParentRunner.java:363)[junit-4.12.jar:4.12]         在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)   [弹簧测试5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)   [万无一失-junit4-2.20.1.jar:2.20.1]         在org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)   [万无一失-junit4-2.20.1.jar:2.20.1]         在org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)   [万无一失-junit4-2.20.1.jar:2.20.1]         在org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)   [万无一失-junit4-2.20.1.jar:2.20.1]         在org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)   [万无一失-足球运动员-2.20.1.jar:2.20.1]         在org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)   [万无一失-足球运动员-2.20.1.jar:2.20.1]         在org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)   [万无一失-足球运动员-2.20.1.jar:2.20.1]         在org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)   [万无一失-足球运动员-2.20.1.jar:2.20.1]       引发者:javax.persistence.PersistenceException:无法解析持久性单元根URL         在org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:637)   〜[弹簧ORM-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:459)   〜[弹簧ORM-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:440)   〜[弹簧ORM-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:328)   〜[弹簧ORM-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769)   〜[弹簧豆-5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706)   〜[弹簧豆-5.0.4.RELEASE.jar:5.0.4.RELEASE]         ...省略了44个常用帧       引起:java.io.FileNotFoundException:类路径资源[]无法解析为URL,因为它不存在         在org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:195)   〜[弹簧芯5.0.4.RELEASE.jar:5.0.4.RELEASE]         在org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:633)   〜[弹簧ORM-5.0.4.RELEASE.jar:5.0.4.RELEASE]         ...省略了49个常见帧

我的测试有什么问题,Jenkins无法执行它?

4 个答案:

答案 0 :(得分:0)

这可能是因为春季启动版本升级。我发现this thread与您的问题类似,我找到的解决方案就是这个。希望这对你有帮助。

  

@EnableAutoConfiguration(exclude =   必须在应用程序上设置HibernateJpaAutoConfiguration.class)   必须设置class和spring.data.jpa.repositories.enabled = false   应用程序属性/ yml。

答案 1 :(得分:0)

我在这里找到了解决方案:https://jira.spring.io/browse/SPR-15304

当我输入mvn clean install -f ./pom.xml而不是mvn clean install时,我可以在本地重现问题。在Jenkins中,我设置了pom文件的自定义路径,因为pom文件不在存储库的根目录中。 (路径类似于:./ pathToPomFile/pom.xml解决方案是删除Jenkins中pom.xml的自定义路径,并将整个代码结构移动到存储库的根目录。

答案 2 :(得分:0)

我也遇到了这个问题,但是重新组织我的存储库(例如在“已接受的答案”中)不是一个选择。我最终使用了“执行外壳”构建步骤,而不是内置的Maven步骤,我的脚本只是“ cd someDir; mvn clean install”。

像魅力一样工作!

答案 3 :(得分:0)

如果您想使用Replicas 2 read-write replicas in us-east4 (Northern Virginia) - default leader region 2 read-write replicas in us-east1 (South Carolina) ,则应该有一个多模块项目(只需将至少1个模块添加到您的项目中)