Spring 未找到引导 JNDI - 无法使用 Maven 编译 WAR

Spring Boot JNDI not found - can't compile WAR with Maven

我无法 mvn clean install 我的项目。 我需要在 WAR(不是 jar)中使用远程 JNDI,但是在 application.properties 中使用名称会在 mvn clean install 期间抛出错误(由于 "datasource jndi name not found")


如果我必须使用我无法控制的远程服务器的 jndi 名称,我如何在 WAR 中编译我的包而不查找失败,但让它在 [=39] 期间查找=] 服务器上的应用程序?非常感谢任何提示或方向...


在应用道具中,我有

spring.datasource.jndi-姓名=jdbc/ff-app

我试过了:Spring boot JNDI datasource lookup failure - Name comp/env/jdbc not found in context "java:"

尝试过,http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-connecting-to-a-jndi-datasource

以及 Whosebug 中关于远程 jndi 的几乎所有问题..

部分错误:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'jdbc/ff-app'; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] ... 69 common frames omitted Caused by: org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'jdbc/ff-app'; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup.getDataSource(JndiDataSourceLookup.java:48) ~[spring-jdbc-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration.dataSource(JndiDataSourceAutoConfiguration.java:62) ~[spring-boot-autoconfigure-1.5.1.RELEASE.jar:1.5.1.RELEASE] at org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration$$EnhancerBySpringCGLIB$09801a.CGLIB$dataSource[=13=]() ~ ........

我的POM.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
</dependencies>

重点是,当您尝试使用 maven 进行编译时,maven 也会尝试执行 Junit 测试;在您的情况下,您在 src/resources/java 中有一些单元测试,因此 Maven 会尝试执行它们。由于您使用的是 JNDI 数据源,这意味着 servlet 容器也必须处于活动状态才能执行测试,否则您将无法获得连接。

在这种情况下,当 maven 尝试执行测试时,可能没有 servlet 容器(因此,没有 JNDI 数据源)处于活动状态并且您的测试抛出错误。

要解决此问题(并编译 Maven 工件),您必须:

  • 或排除测试执行:这是通过在 pom.xml 中添加 <maven.test.skip>true</maven.test.skip> 来完成的。不会执行任何测试
  • 或忽略测试错误,这是通过在 pom.xml 中添加 <maven.test.failure.ignore>true</maven.test.failure.ignore> 来完成的。在这种情况下,将执行测试,但任何测试错误都不会停止 Maven 编译

我给出的配置不仅告诉 maven 不执行测试,而且告诉 maven 不要在任何测试错误时停止

我希望我说得更清楚了...我的英语不是最好的