jboss maven_使用Maven配置JBoss / Wildfly数据源
jboss maven
大多數Java EE應用程序在其業務邏輯中使用數據庫訪問,因此開發人員經常面臨在應用程序服務器中配置驅動程序和數據庫連接屬性的需求。 在本文中,我們將使用Maven為JBoss / Wildfly和Postgre數據庫自動化該任務。 這項工作是根據我從以前的魔獸拍賣批量應用世界崗位 。
Maven配置
首先,將以下內容添加到我們的pom.xml :
Wildfly Maven插件
org.wildfly.pluginswildfly-maven-plugin1.0.2.Finalfalseorg.postgresqlpostgresql9.3-1102-jdbc41我們將使用Wildfly Maven插件在應用程序服務器中使用命令執行腳本。 請注意,我們還向Postgre驅動程序添加了依賴項。 這是供Maven下載的依賴項,因為我們稍后將需要它來將其添加到服務器中。 還有一個${cli.file}屬性將被分配給一個配置文件。 這是為了指示我們要執行哪個腳本。
讓我們還將以下內容添加到pom.xml :
Maven資源插件
org.apache.maven.pluginsmaven-resources-plugin2.6copy-resourcesprocess-resourcescopy-resources${basedir}/target/scriptssrc/main/resources/scriptstrue${basedir}/src/main/resources/configuration.properties使用Resources Maven插件,我們將過濾src/main/resources/scripts包含的腳本文件,并用${basedir}/src/main/resources/configuration.properties文件中包含的屬性替換它們。
最后,使用我們要運行的腳本將一些Maven配置文件添加到pom.xml :
Maven個人資料
install-driverwildfly-install-postgre-driver.cliremove-driverwildfly-remove-postgre-driver.cliinstall-wow-auctionswow-auctions-install.cliremove-wow-auctionswow-auctions-remove.cliWildfly腳本文件
添加驅動
帶有用于添加驅動程序的命令的腳本:
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")數據庫驅動程序作為模塊添加到Wildfly 。 在這種情況下,驅動程序可廣泛用于服務器中部署的所有應用程序。 使用${settings.localRepository}我們指向下載到本地Maven存儲庫的數據庫驅動程序jar。 還記得我們添加到Wildfly Maven插件中的依賴嗎? 當您運行插件時,將下載驅動程序并將其添加到服務器。 現在,要運行我們執行的腳本(您需要運行應用程序服務器):
mvn process-resources wildfly:execute-commands -P "install-driver"
需要process-resources生命周期來替換腳本文件中的屬性。 在我的情況下, ${settings.localRepository}被/Users/radcortez/.m3/repository/代替。 檢查target/scripts文件夾。 運行命令后,您應該在Maven日志中看到以下輸出:
{"outcome" => "success"}在服務器上:
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 = postgrewildfly-刪除-postgre-driver.cli
# Connect to Wildfly instance connectif (outcome == success) of /subsystem=datasources/jdbc-driver=postgre:read-attribute(name=driver-name)# Remove Driver/subsystem=datasources/jdbc-driver=postgre:removeend-if# Remove Oracle JDBC Driver Module module remove --name=org.postgre該腳本用于從應用程序服務器中刪除驅動程序。 執行mvn wildfly:execute-commands -P "remove-driver" 。 如果您之前已經執行過命令,則不需要process-resources ,除非您更改了腳本。
添加數據源
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}")我們還需要一個文件來定義屬性:
configuration.properties
datasource.jndi=java:/datasources/WowAuctionsDS datasource.connection=jdbc:postgresql://localhost:5432/wowauctions datasource.user=wowauctions datasource.password=wowauctions默認的Java EE 7數據源
Java EE 7指定容器應提供默認的數據源。 與其在應用程序中定義JNDI名稱java:/datasources/WowAuctionsDS將新創建的數據源指向具有/subsystem=ee/service=default-bindings:write-attribute(name="datasource", value="${datasource.jndi}") 。 這樣,我們無需在應用程序中進行任何更改。 使用mvn wildfly:execute-commands -P "install-wow-auctions"執行腳本。 您應該獲得以下Maven輸出:
org.jboss.as.cli.impl.CommandContextImpl printLine INFO: {"outcome" => "success"} {"outcome" => "success"} org.jboss.as.cli.impl.CommandContextImpl printLine INFO: {"outcome" => "success"} {"outcome" => "success"}在服務器上:
INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data sourcewow-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")這是用于刪除數據源并還原Java EE 7默認數據源的腳本。 通過執行mvn wildfly:execute-commands -P "remove-wow-auctions"運行它
結論
這篇文章演示了如何自動向Wildfly實例添加/刪除驅動程序以及如何添加/刪除數據源。 如果要在數據庫之間切換,或者要從頭開始配置服務器,這將很有用。 考慮一下CI環境。 這些腳本也可以輕松調整為其他驅動程序。
- 您可以從使用此設置的WoW Auctions Github存儲庫中獲取代碼。
請享用!
翻譯自: https://www.javacodegeeks.com/2014/10/configure-jboss-wildfly-datasource-with-maven.html
jboss maven
總結
以上是生活随笔為你收集整理的jboss maven_使用Maven配置JBoss / Wildfly数据源的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 带宽是什么意思?(带宽DDOS)
- 下一篇: osgi 如何引入包_OSGi Test