使用Maven配置JBoss、Wildfly数据源

开发 后端
大多数Java EE应用在其业务逻辑层中会访问数据库,所以开发者会经常需要为应用服务器配置数据库驱动和数据库连接。这篇文章会讨论如何用Maven自动化JBoss、Wildfly和Postgre数据库的配置。

大多数Java EE应用在其业务逻辑层中会访问数据库,所以开发者会经常需要为应用服务器配置数据库驱动和数据库连接。这篇文章会讨论如何用Maven自动化JBoss、Wildfly和Postgre数据库的配置。

Maven 配置

让我们从下面的pom.xml 开始吧,

Wildfly Maven Plugin

<plugin> 
    <groupid>org.wildfly.plugins</groupid> 
    <artifactid>wildfly-maven-plugin</artifactid> 
    <version>1.0.2.Final</version> 
    <configuration> 
        <executecommands> 
            <batch>false</batch> 
            <scripts>%MINIFYHTML7db47c7a4774fb3aa46c5ca8120866ec8%</scripts> 
        </executecommands> 
    </configuration> 
    <dependencies> 
        <dependency> 
            <groupid>org.postgresql</groupid> 
            <artifactid>postgresql</artifactid> 
            <version>9.3-1102-jdbc41</version> 
        </dependency> 
    </dependencies> 
</plugin> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

我们开始使用Wildfly Maven Plugin在应用服务器执行命令脚本。我们已经添加了 Postgre的依赖, Maven会下载依赖, 因为我们将要在后面把它加到服务器中。这里有一个 ${cli.file} 属性, 将指明将执行哪一个脚本。

让我们在pom.xml中添加下面内容:

Maven Resources Plugin

<plugin> 
    <groupid>org.apache.maven.plugins</groupid> 
    <artifactid>maven-resources-plugin</artifactid> 
    <version>2.6</version> 
    <executions> 
        <execution> 
            <id>copy-resources</id> 
            <phase>process-resources</phase> 
            <goals> 
                <goal>copy-resources</goal> 
            </goals> 
            <configuration> 
                <outputdirectory>${basedir}/target/scripts</outputdirectory> 
                <resources> 
                    <resource> 
                        <directory>src/main/resources/scripts</directory> 
                        <filtering>true</filtering> 
                    </resource> 
                </resources> 
                <filters> 
                    <filter>${basedir}/src/main/resources/configuration.properties</filter> 
                </filters> 
            </configuration> 
        </execution> 
    </executions> 
</plugin> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

用这个插件,我们可以过滤包含在src/main/resources/scripts这个目录中的脚本。使用${basedir}/src/main/resources/configuration.properties这个文件中的属性进行替换。

最后添加一些 Maven属性到pom.xml文件中:

Maven Profiles

<profiles> 
    <profile> 
        <id>install-driver</id> 
        <properties> 
            <cli.file>wildfly-install-postgre-driver.cli</cli.file> 
        </properties> 
    </profile> 
 
    <profile> 
        <id>remove-driver</id> 
        <properties> 
            <cli.file>wildfly-remove-postgre-driver.cli</cli.file> 
        </properties> 
    </profile> 
 
    <profile> 
        <id>install-wow-auctions</id> 
        <properties> 
            <cli.file>wow-auctions-install.cli</cli.file> 
        </properties> 
    </profile> 
 
    <profile> 
        <id>remove-wow-auctions</id> 
        <properties> 
            <cli.file>wow-auctions-remove.cli</cli.file> 
        </properties> 
    </profile> 
</profiles> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

Wildfly Script Files

添加驱动

添加驱动的脚本:

wildfly-install-postgre-driver.cli

# Connect to Wildfly instance 
connect 
 
# Create Oracle JDBC Driver Module 
# If the module already exists, Wildfly will output a message saying that the module already exists and the script exits. 
module add \ 
    --name=org.postgre \ 
    --resources=${settings.localRepository}/org/postgresql/postgresql/9.3-1102-jdbc41/postgresql-9.3-1102-jdbc41.jar \ 
    --dependencies=javax.api,javax.transaction.api 
 
# Add Driver Properties 
/subsystem=datasources/jdbc-driver=postgre: \ 
    add( \ 
        driver-name="postgre", \ 
        driver-module-name="org.postgre"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

数据库驱动作为Wildfly的一个模块(Module)。这样数据库驱动可以被部署在服务器中的所有应用使用。使用${settings.localRepository} 配置,我们指定数据库驱动下载到你的本地Maven仓库。还记得我们加到 Wildfly Maven Plugin的依赖吗,在你插件运行的时候他将下载驱动并加到服务器中。要运行脚本(必须保证应用服务器正在运行中)可以执行下面的命令:

mvn process-resources wildfly:execute-commands -P "install-driver" 
  • 1.

需要用process-resources生命周期替换脚本中的属性。在这个例子中 ${settings.localRepository} 被替换为 /Users/radcortez/.m3/repository/. 。检查target/scripts 文件夹。在运行命令后,可以在Maven的日志看到以下输出:

{"outcome" => "success"
  • 1.

服务器上的日志:

INFO  [org.jboss.as.connector.subsystems.datasources] (management-handler-thread - 4) JBAS010404: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 9.3
INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) JBAS010417: Started Driver service with driver-name = postgre 
  • 1.
  • 2.

wildfly-remove-postgre-driver.cli

# Connect to Wildfly instance 
connect 
 
if (outcome == success) of /subsystem=datasources/jdbc-driver=postgre:read-attribute(name=driver-name) 
 
    # Remove Driver 
    /subsystem=datasources/jdbc-driver=postgre:remove 
 
end-if 
 
# Remove Oracle JDBC Driver Module 
module remove --name=org.postgre 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

这段脚本是把驱动从你的服务器上删除。允许 mvn wildfly:execute-commands -P “remove-driver”,如果你已经执行了以前的命令就不需要再配置process-resource,除非脚本发生改变。

添加数据源

wow-auctions-install.cli

这个脚本使用命令添加了一个数据源

wow-auctions-install.cli

# Connect to Wildfly instance 
connect 
 
# Create Datasource 
/subsystem=datasources/data-source=WowAuctionsDS: \ 
    add( \ 
        jndi-name="${datasource.jndi}", \ 
        driver-name=postgre, \ 
        connection-url="${datasource.connection}", \ 
        user-name="${datasource.user}", \ 
        password="${datasource.password}"
 
/subsystem=ee/service=default-bindings:write-attribute(name="datasource", value="${datasource.jndi}"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

我们依然需要一个文件来定义这些属性。

configuration.properties

datasource.jndi=java:/datasources/WowAuctionsDS 
datasource.connection=jdbc:postgresql://localhost:5432/wowauctions 
datasource.user=wowauctions 
datasource.password=wowauctions 
  • 1.
  • 2.
  • 3.
  • 4.

Java EE 7 默认数据源

Java EE 7中, 指定容器必须提供一个默认数据源。不要在程序中使用 java:/datasources/WowAuctionsDS JNDI 定义的数据源,我们将指定一个新创建的数据源 /subsystem=ee/service=default-bindings:write- attribute(name=”datasource”, value=”${datasource.jndi}”)。 这样就无需改变程序中的任何配置。 执行 mvn wildfly:execute-commands -P “install-wow-auctions”,就可以得到以下输出:

org.jboss.as.cli.impl.CommandContextImpl printLine 
INFO: {"outcome" => "success"
{"outcome" => "success"
org.jboss.as.cli.impl.CommandContextImpl printLine 
INFO: {"outcome" => "success"
{"outcome" => "success"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

服务器日志:

INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data source 
  • 1.

wow-auctions-remove.cli

# Connect to Wildfly instance 
connect 
 
# Remove Datasources 
/subsystem=datasources/data-source=WowAuctionsDS:remove 
 
/subsystem=ee/service=default-bindings:write-attribute(name="datasource", value="java:jboss/datasources/ExampleDS"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

上面是删除数据源转为Java EE 7 默认数据源的脚本。执行时用这个命令:mvn wildfly:execute-commands -P "remove-wow-auctions"。

总结

这篇博客展示了如何自动在Wildfly实例中添加删除添加驱动和数据源。如果需要在不同数据库之间切换或者打算重头配置服务器,本文的内容会对你非常有帮助。在做持续集成(CI)时,这些脚本稍作调整就可以转到其他驱动。

你可以在这里得到代码WoW Auctions Github repo

原文链接: javacodegeeks 翻译: ImportNew.com 孙 彪彪
译文链接: http://www.importnew.com/13718.html

 

 

责任编辑:张伟 来源: ImportNew
相关推荐

2009-06-15 13:24:46

JBoss数据源

2009-06-16 16:20:58

JBoss4配置Inf

2010-12-27 09:59:11

ODBC数据源

2024-10-30 10:22:17

2023-09-07 08:39:39

copy属性数据源

2020-12-31 07:55:33

spring bootMybatis数据库

2015-12-25 16:37:02

RxJava数据源数据

2009-06-26 17:37:37

JBOSS配置

2012-06-17 13:04:45

2010-05-14 15:32:51

配置MySQL

2023-01-04 09:33:31

SpringBootMybatis

2017-09-04 14:52:51

Tomcat线程数据源

2009-08-14 10:26:27

ibatis多数据源

2023-11-27 09:16:53

Python数据源类型

2012-09-04 13:48:57

云计算数据源Odata API

2014-08-15 09:55:09

SSIS

2009-07-14 09:55:33

MyEclipse W

2022-12-19 07:21:35

Hutool-db数据库JDBC

2017-06-14 23:42:27

大数据数据源架构

2009-09-15 17:15:33

Linq排序
点赞
收藏

51CTO技术栈公众号