使用Maven配置JBoss、Wildfly数据源的方法

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


Maven 配置
让我们从下面的pom.xml 开始吧,
Wildfly Maven Plugin

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


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

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


用这个插件,我们可以过滤包含在src/main/resources/scripts这个目录中的脚本。使用${basedir}/src/main/resources/configuration.properties这个文件中的属性进行替换。
最后添加一些 Maven属性到pom.xml文件中:
Maven Profiles

  1. <profiles>
  2.     <profile>
  3.         <id>install-driver</id>
  4.         <properties>
  5.             <cli.file>wildfly-install-postgre-driver.cli</cli.file>
  6.         </properties>
  7.     </profile>

  8.     <profile>
  9.         <id>remove-driver</id>
  10.         <properties>
  11.             <cli.file>wildfly-remove-postgre-driver.cli</cli.file>
  12.         </properties>
  13.     </profile>

  14.     <profile>
  15.         <id>install-wow-auctions</id>
  16.         <properties>
  17.             <cli.file>wow-auctions-install.cli</cli.file>
  18.         </properties>
  19.     </profile>

  20.     <profile>
  21.         <id>remove-wow-auctions</id>
  22.         <properties>
  23.             <cli.file>wow-auctions-remove.cli</cli.file>
  24.         </properties>
  25.     </profile>
  26. </profiles>
复制代码


Wildfly Script Files
添加驱动
添加驱动的脚本:


wildfly-install-postgre-driver.cli

  1. # Connect to Wildfly instance
  2. connect

  3. # Create Oracle JDBC Driver Module
  4. # If the module already exists, Wildfly will output a message saying that the module already exists and the script exits.
  5. module add \
  6.     --name=org.postgre \
  7.     --resources=${settings.localRepository}/org/postgresql/postgresql/9.3-1102-jdbc41/postgresql-9.3-1102-jdbc41.jar \
  8.     --dependencies=javax.api,javax.transaction.api

  9. # Add Driver Properties
  10. /subsystem=datasources/jdbc-driver=postgre: \
  11.     add( \
  12.         driver-name="postgre", \
  13.         driver-module-name="org.postgre")
复制代码


数据库驱动作为Wildfly的一个模块(Module)。这样数据库驱动可以被部署在服务器中的所有应用使用。使用${settings.localRepository} 配置,我们指定数据库驱动下载到你的本地Maven仓库。还记得我们加到 Wildfly Maven Plugin的依赖吗,在你插件运行的时候他将下载驱动并加到服务器中。要运行脚本(必须保证应用服务器正在运行中)可以执行下面的命令:
mvn process-resources wildfly:execute-commands -P "install-driver"需要用process-resources生命周期替换脚本中的属性。在这个例子中 ${settings.localRepository} 被替换为 /Users/radcortez/.m3/repository/. 。检查target/scripts 文件夹。在运行命令后,可以在Maven的日志看到以下输出:

  1. {"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)
  2. INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) JBAS010417: Started Driver service with driver-name = postgre
复制代码


wildfly-remove-postgre-driver.cli

  1. # Connect to Wildfly instance
  2. connect

  3. if (outcome == success) of /subsystem=datasources/jdbc-driver=postgre:read-attribute(name=driver-name)

  4.     # Remove Driver
  5.     /subsystem=datasources/jdbc-driver=postgre:remove

  6. end-if

  7. # Remove Oracle JDBC Driver Module
  8. module remove --name=org.postgre
复制代码


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


添加数据源
wow-auctions-install.cli
这个脚本使用命令添加了一个数据源
wow-auctions-install.cli

  1. # Connect to Wildfly instance
  2. connect

  3. # Create Datasource
  4. /subsystem=datasources/data-source=WowAuctionsDS: \
  5.     add( \
  6.             jndi-name="${datasource.jndi}", \
  7.             driver-name=postgre, \
  8.             connection-url="${datasource.connection}", \
  9.             user-name="${datasource.user}", \
  10.             password="${datasource.password}")

  11. /subsystem=ee/service=default-bindings:write-attribute(name="datasource", value="${datasource.jndi}")
复制代码


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


configuration.properties

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


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”,就可以得到以下输出:

  1. org.jboss.as.cli.impl.CommandContextImpl printLine
  2. INFO: {"outcome" => "success"}
  3. {"outcome" => "success"}
  4. org.jboss.as.cli.impl.CommandContextImpl printLine
  5. INFO: {"outcome" => "success"}
  6. {"outcome" => "success"}
复制代码


服务器日志:

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


wow-auctions-remove.cli

  1. # Connect to Wildfly instance
  2. connect

  3. # Remove Datasources
  4. /subsystem=datasources/data-source=WowAuctionsDS:remove

  5. /subsystem=ee/service=default-bindings:write-attribute(name="datasource", value="java:jboss/datasources/ExampleDS")
复制代码


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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值