使用Maven配置JBoss、Wildfly數據源

jopen 10年前發布 | 27K 次閱讀 Maven JBoss 應用服務器

大多數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>

我們開始使用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>

用這個插件,我們可以過濾包含在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>

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")

數據庫驅動作為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的日志看到以下輸出:

{"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 = 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

這段腳本是把驅動從你的服務器上刪除。允許 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}")

我們依然需要一個文件來定義這些屬性。

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中, 指定容器必須提供一個默認數據源。不要在程序中使用 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"}

服務器日志:

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")

上面是刪除數據源轉為Java EE 7 默認數據源的腳本。執行時用這個命令:mvn wildfly:execute-commands -P "remove-wow-auctions"。

總結

這篇博客展示了如何自動在Wildfly實例中添加刪除添加驅動和數據源。如果需要在不同數據庫之間切換或者打算重頭配置服務器,本文的內容會對你非常有幫助。在做持續集成(CI)時,這些腳本稍作調整就可以轉到其他驅動。

祝編程愉快!

原文鏈接: javacodegeeks 翻譯: ImportNew.com - 孫 彪彪
譯文鏈接: http://www.importnew.com/13718.html

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!