Maven Cargo:找不到 JNDI 数据源

Posted

技术标签:

【中文标题】Maven Cargo:找不到 JNDI 数据源【英文标题】:Maven Cargo: Could not find JNDI datasource 【发布时间】:2014-09-17 16:49:49 【问题描述】:

这是我的persistence.xml 的样子

<?xml version="1.0" encoding="UTF-8"?>
<persistence
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/persistence"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">
    <persistence-unit name="earth">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:/jdbc/EarthDS</jta-data-source>
        <properties>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.id.new_generator_mappings" value="true"/>
            <property name="javax.persistence.lock.timeout" value="5000"/>
        </properties>
    </persistence-unit>
</persistence>

我正在使用maven-cargo-plugin 部署war 文件。根据他们的documentation,他们将JNDI的数据源设置为

        <configuration>
          <properties>
            <cargo.datasource.datasource.derby>
                cargo.datasource.driver=org.apache.derby.jdbc.EmbeddedDriver|
                cargo.datasource.url=jdbc:derby:derbyDB;create=true|
                cargo.datasource.jndi=jdbc/CargoDS|
                cargo.datasource.username=APP|
                cargo.datasource.password=nonemptypassword
            </cargo.datasource.datasource.derby>
          </properties>
        </configuration>

由于我使用的是H2数据库,所以我将其设置为

                      <properties>
                           <cargo.servlet.port>9090</cargo.servlet.port>
                           <properties>
                                <cargo.datasource.datasource>
                                     cargo.datasource.driver=org.h2.Driver|
                                     cargo.datasource.url=jdbc:h2:~/earth;create=true|
                                     cargo.datasource.jndi=jdbc/EarthDS|
                                     cargo.datasource.username=sa|
                                     cargo.datasource.password=
                                </cargo.datasource.datasource>
                           </properties>
                      </properties>
                 </configuration>

当我运行项目时,我在日志中看到错误

[INFO] [talledLocalContainer] 09:43:01,335 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-12) JBAS015876: Starting deployment of "cargocpc.war" (runtime-name: "cargocpc.war")
[INFO] [talledLocalContainer] 09:43:01,335 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-11) JBAS015876: Starting deployment of "earth.war" (runtime-name: "earth.war")
[INFO] [talledLocalContainer] 09:43:01,478 INFO  [org.jboss.ws.common.management] (MSC service thread 1-3) JBWS022052: Starting JBoss Web Services - Stack CXF Server 4.2.3.Final
[INFO] [talledLocalContainer] 09:43:01,753 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-6) JBAS017534: Registered web context: /cargocpc
[INFO] [talledLocalContainer] 09:43:01,779 INFO  [org.jboss.as.jpa] (MSC service thread 1-16) JBAS011401: Read persistence.xml for earth
[INFO] [talledLocalContainer] WildFly 8.x started on port [9090]
[INFO] 
[INFO] --- cargo-maven2-plugin:1.4.8:stop (stop-container) @ integration ---
[INFO] [talledLocalContainer] 09:43:01,865 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "earth.war")]) - failure description: "JBAS014771: Services with missing/unavailable dependencies" => ["jboss.persistenceunit.\"earth.war#earth\".__FIRST_PHASE__ is missing [jboss.naming.context.java.jdbc.EarthDS]"]
[INFO] [talledLocalContainer] 09:43:01,898 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 29) JBAS018559: Deployed "earth.war" (runtime-name : "earth.war")
[INFO] [talledLocalContainer] 09:43:01,898 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 29) JBAS018559: Deployed "cargocpc.war" (runtime-name : "cargocpc.war")
[INFO] [talledLocalContainer] 09:43:01,899 INFO  [org.jboss.as.controller] (Controller Boot Thread) JBAS014774: Service status report
[INFO] [talledLocalContainer] JBAS014775:    New missing/unsatisfied dependencies:
[INFO] [talledLocalContainer]       service jboss.naming.context.java.jdbc.EarthDS (missing) dependents: [service jboss.persistenceunit."earth.war#earth".__FIRST_PHASE__] 
[INFO] [talledLocalContainer] 
[INFO] [talledLocalContainer] 09:43:01,908 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
[INFO] [talledLocalContainer] 09:43:01,909 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
[INFO] [talledLocalContainer] 09:43:01,909 ERROR [org.jboss.as] (Controller Boot Thread) JBAS015875: WildFly 8.0.0.Final "WildFly" started (with errors) in 2415ms - Started 262 of 318 services (2 services failed or missing dependencies, 92 services are lazy, passive or on-demand)
[INFO] [talledLocalContainer] WildFly 8.x is stopping...
[INFO] [talledLocalContainer] 09:43:02,137 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-8) JBAS015877: Stopped deployment earth.war (runtime-name: earth.war) in 14ms
[INFO] [talledLocalContainer] 09:43:02,185 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018558: Undeployed "earth.war" (runtime-name: "earth.war")
[INFO] [talledLocalContainer] 09:43:02,186 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 2) JBAS014774: Service status report
[INFO] [talledLocalContainer] JBAS014775:    New missing/unsatisfied dependencies:
[INFO] [talledLocalContainer]       service jboss.persistenceunit."earth.war#earth".__FIRST_PHASE__ (missing) dependents: [service jboss.deployment.unit."earth.war".POST_MODULE] 
[INFO] [talledLocalContainer] 
[INFO] [talledLocalContainer] INFO  [org.jboss.modules] JBoss Modules version 1.3.0.Final
[WARNING] [talledLocalContainer] WARN: can't find jboss-cli.xml. Using default configuration values.
[INFO] [talledLocalContainer] INFO  [org.xnio] XNIO version 3.2.0.Final
[INFO] [talledLocalContainer] INFO  [org.xnio.nio] XNIO NIO Implementation Version 3.2.0.Final
[INFO] [talledLocalContainer] INFO  [org.jboss.remoting] JBoss Remoting version 4.0.0.Final
[INFO] [talledLocalContainer] 09:43:02,806 INFO  [org.jboss.as.controller] (management-handler-thread - 3) JBAS014774: Service status report
[INFO] [talledLocalContainer] JBAS014776:    Newly corrected services:
[INFO] [talledLocalContainer]       service jboss.naming.context.java.jdbc.EarthDS (no longer required)
[INFO] [talledLocalContainer]       service jboss.persistenceunit."earth.war#earth".__FIRST_PHASE__ (no longer required)
[INFO] [talledLocalContainer] 
[INFO] [talledLocalContainer] INFO  [org.jboss.as.cli.CommandContext] "outcome" => "success"
[INFO] [talledLocalContainer] "outcome" => "success"
[INFO] [talledLocalContainer] 09:43:02,813 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-10) JBAS017535: Unregistered web context: /cargocpc

有人可以帮我理解这里有什么不对吗?

谢谢

【问题讨论】:

【参考方案1】:

在文档 sn-p 中,名称有 4 个部分 在您的示例中,它只有 3 个部分

既然 cargo 可以配置多个数据源,那么第 4 部分是否需要唯一标识它们?

【讨论】:

【参考方案2】:

使用与 persistence.xml 中相同的 jndi 字符串,并在 jdbc 字符串中指定 file 或 mem:

<cargo.datasource.datasource.h2>
    cargo.datasource.jndi=java:/jdbc/EarthDS|
    cargo.datasource.driver=org.h2.Driver|
    cargo.datasource.url=jdbc:h2:mem:earth;create=true|
    cargo.datasource.username=sa|
    cargo.datasource.password=sa
</cargo.datasource.datasource.h2>

确保容器具有对 h2 驱动程序的依赖项:

<container>
    ...
    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
    </dependencies>
</container>

并且 h2 驱动程序列在 maven 依赖项中:

<dependencies>
    ...
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.182</version>
    </dependency>
</dependencies>

【讨论】:

以上是关于Maven Cargo:找不到 JNDI 数据源的主要内容,如果未能解决你的问题,请参考以下文章

无法安装 cargo-binutils 库:找不到工具。是不是安装了`gcc.exe`?

如何为 Jetty 的 Maven Cargo 插件指定 jetty-env.xml 文件?

带有纯 JDBC-JNDI:javax.naming.NameNotFoundException 的 Tomcat 8.5:名称 [jdbc/KDB] 未绑定在此上下文中。找不到 [jdbc]

找不到Maven包

maven编译时候提示找不到符号怎么办

maven target 怎么运行 找不到或无法加载主类