使用Maven配置JBoss、Wildfly數(shù)據(jù)源
大多數(shù)Java EE應(yīng)用在其業(yè)務(wù)邏輯層中會(huì)訪問數(shù)據(jù)庫,所以開發(fā)者會(huì)經(jīng)常需要為應(yīng)用服務(wù)器配置數(shù)據(jù)庫驅(qū)動(dòng)和數(shù)據(jù)庫連接。這篇文章會(huì)討論如何用Maven自動(dòng)化JBoss、Wildfly和Postgre數(shù)據(jù)庫的配置。
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>
我們開始使用Wildfly Maven Plugin在應(yīng)用服務(wù)器執(zhí)行命令腳本。我們已經(jīng)添加了 Postgre的依賴, Maven會(huì)下載依賴, 因?yàn)槲覀儗⒁诤竺姘阉拥椒?wù)器中。這里有一個(gè) ${cli.file} 屬性, 將指明將執(zhí)行哪一個(gè)腳本。
讓我們?cè)趐om.xml中添加下面內(nèi)容:
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>
用這個(gè)插件,我們可以過濾包含在src/main/resources/scripts這個(gè)目錄中的腳本。使用${basedir}/src/main/resources/configuration.properties這個(gè)文件中的屬性進(jìn)行替換。
最后添加一些 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>
Wildfly Script Files
添加驅(qū)動(dòng)
添加驅(qū)動(dòng)的腳本:
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")
數(shù)據(jù)庫驅(qū)動(dòng)作為Wildfly的一個(gè)模塊(Module)。這樣數(shù)據(jù)庫驅(qū)動(dòng)可以被部署在服務(wù)器中的所有應(yīng)用使用。使用${settings.localRepository} 配置,我們指定數(shù)據(jù)庫驅(qū)動(dòng)下載到你的本地Maven倉庫。還記得我們加到 Wildfly Maven Plugin的依賴嗎,在你插件運(yùn)行的時(shí)候他將下載驅(qū)動(dòng)并加到服務(wù)器中。要運(yùn)行腳本(必須保證應(yīng)用服務(wù)器正在運(yùn)行中)可以執(zhí)行下面的命令:
- mvn process-resources wildfly:execute-commands -P "install-driver"
需要用process-resources生命周期替換腳本中的屬性。在這個(gè)例子中 ${settings.localRepository} 被替換為 /Users/radcortez/.m3/repository/. 。檢查target/scripts 文件夾。在運(yùn)行命令后,可以在Maven的日志看到以下輸出:
- {"outcome" => "success"}
服務(wù)器上的日志:
- 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
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
這段腳本是把驅(qū)動(dòng)從你的服務(wù)器上刪除。允許 mvn wildfly:execute-commands -P “remove-driver”,如果你已經(jīng)執(zhí)行了以前的命令就不需要再配置process-resource,除非腳本發(fā)生改變。
添加數(shù)據(jù)源
wow-auctions-install.cli
這個(gè)腳本使用命令添加了一個(gè)數(shù)據(jù)源
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}")
我們依然需要一個(gè)文件來定義這些屬性。
configuration.properties
- datasource.jndi=java:/datasources/WowAuctionsDS
- datasource.connection=jdbc:postgresql://localhost:5432/wowauctions
- datasource.user=wowauctions
- datasource.password=wowauctions
Java EE 7 默認(rèn)數(shù)據(jù)源
Java EE 7中, 指定容器必須提供一個(gè)默認(rèn)數(shù)據(jù)源。不要在程序中使用 java:/datasources/WowAuctionsDS JNDI 定義的數(shù)據(jù)源,我們將指定一個(gè)新創(chuàng)建的數(shù)據(jù)源 /subsystem=ee/service=default-bindings:write- attribute(name=”datasource”, value=”${datasource.jndi}”)。 這樣就無需改變程序中的任何配置。 執(zhí)行 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"}
服務(wù)器日志:
- INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data source
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")
上面是刪除數(shù)據(jù)源轉(zhuǎn)為Java EE 7 默認(rèn)數(shù)據(jù)源的腳本。執(zhí)行時(shí)用這個(gè)命令:mvn wildfly:execute-commands -P "remove-wow-auctions"。
總結(jié)
這篇博客展示了如何自動(dòng)在Wildfly實(shí)例中添加刪除添加驅(qū)動(dòng)和數(shù)據(jù)源。如果需要在不同數(shù)據(jù)庫之間切換或者打算重頭配置服務(wù)器,本文的內(nèi)容會(huì)對(duì)你非常有幫助。在做持續(xù)集成(CI)時(shí),這些腳本稍作調(diào)整就可以轉(zhuǎn)到其他驅(qū)動(dòng)。
你可以在這里得到代碼WoW Auctions Github repo。
原文鏈接: javacodegeeks 翻譯: ImportNew.com - 孫 彪彪
譯文鏈接: http://www.importnew.com/13718.html