使用Maven配置JBoss、Wildfly資料來源

ImportNew - 孫彪彪發表於2014-11-19

大多數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)時,這些指令碼稍作調整就可以轉到其他驅動。

祝程式設計愉快!

相關文章