mysql - 如何使用 jboss-as-maven-plugin 部署 mysql 数据源

标签 mysql maven jboss jboss7.x

这个问题是为了在我找到解决方案后给出答案,我从 jboss 找到了关于 Postgresql 和 H2 数据库的文档 website并了解如何通过此 website 手动完成操作,但是我似乎找不到太多关于如何使用 jboss-as-maven-plugin 部署 mysql 数据源的信息。

通过 maven 插件向 jboss-as 7 服务器正确注册 mysql 数据源所需的最低配置属性是什么?

我有这种依赖:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

以及maven插件的这个配置

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.jboss.as.plugins</groupId>
            <artifactId>jboss-as-maven-plugin</artifactId>
            <configuration>
                <execute-commands/>
                <executeCommands/>
                <properties>
                    <enable-welcome-root>false</enable-welcome-root>
                </properties>
            </configuration>
            <executions>
                ...
                <!-- deploy the mysql connectorj -->
                <execution>
                    <id>deploy-mysql-driver</id>
                    <phase>install</phase>
                    <goals>
                        <goal>deploy-artifact</goal>
                    </goals>
                    <configuration>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <name>mysql.jar</name>
                    </configuration>
                </execution>
                <execution>
                    <id>deploy</id>
                    <phase>install</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
                <execution>
                    <id>add-datasource</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <address>subsystem=datasources</address>
                        <resources>
                            <resource>
                                <address>xa-data-source=java:global/datasources/tncDS</address>
                                <enable-resource>true</enable-resource>
                                <properties>
                                    <jndi-name>java:jboss/datasources/tncDS</jndi-name>
                                    <enabled>true</enabled>
                                    <connection-url>jdbc:mysql://localhost:3306/tnc</connection-url>
                                    <driver-class>com.mysql.jdbc.Driver</driver-class>
                                    <driver-name>mysql.jar</driver-name>
                                </properties>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
            ...
        </plugin>
    </plugins>
</build>

运行命令 mvn jboss-as:run 导致此错误:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.241s
[INFO] Finished at: Tue Sep 24 21:37:28 EST 2013
[INFO] Final Memory: 16M/308M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.jboss.as.plugins:jboss-as-maven-plugin:7.4.Final:deploy-artifact (deploy-mysql-driver) on project ear: Could not execute goal deploy-artifact on null. Reason: I/O Error could not execute operation '{
[ERROR] "address" => [],
[ERROR] "operation" => "read-attribute",
[ERROR] "name" => "launch-type"
[ERROR] }': java.net.ConnectException: JBAS012144: Could not connect to remote://localhost:9999. The connection timed out
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

更新:

我开发了一个插件,可以在部署前将所需文件 (META-INF/services/java.sql.Driver) 注入(inject)到 jar 中:

<plugin>
    <groupId>com.thenaglecode</groupId>
    <artifactId>mysql-jdbc-compliance-maven-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <configuration>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.26</version>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>modify-connector</goal>
            </goals>
            <phase>package</phase>
        </execution>
    </executions>
</plugin>

但是我仍然收到无法连接到远程消息。是否有我遗漏或按错误顺序执行的步骤,因为我知道运行命令应该启动服务器。

更新 2:
经过一些摆弄和阅读 jboss-as 插件 website ,我意识到 jboss-as:run 目标也调用了 package 阶段。当我尝试运行绑定(bind)到 package 阶段的任何部署目标时,我主要收到此错误。

任何需要部署的东西都应该绑定(bind)到install阶段。

我现在收到关于我的持久性单元不存在的单独错误

最佳答案

问题是 MySQL 驱动程序不符合 JDBC 4。您需要将 META-INF/services/java.sql.Driver 文件添加到具有完全限定的 JDBC 驱动程序类名称的 JAR 中,将其作为模块安装。参见 https://community.jboss.org/wiki/DataSourceConfigurationInAS7了解更多详情。

关于mysql - 如何使用 jboss-as-maven-plugin 部署 mysql 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18980631/

相关文章:

java - 了解 JBOSS 及其服务

java - Cloudant 429 模型过多

jsf - 禁用网络浏览器密码保存

java - 如何指定外部 log4j 属性文件来启动 jboss 服务器?

MySQL 从列中返回几个最大值

java - 无法使用 --module-source-path 解析模块

java - 使用SpringMVC Maven构建项目启动Tomcat失败?

mysql - ORDER BY 分组字段

mysql_query() 没有按预期工作,但 phpmyadmin 是

mysql - 更改mysql中的表单元格数据