2014-07-01 39 views
0

如何使用pom中的屬性在glassfish-resources.xml中設置jdbc-connection-pool屬性的值。從glassfish-resources.xml訪問pom屬性

例如,我的pom.xml

... 
<profiles> 
    <profile> 
    <id>dev</id> 
     <properties> 
     <database.dbname>Xpto</database.dbname> 
     ... 
     </properties> 
    </profile> 
    ... 
</profiles> 
... 

和GlassFish-resources.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1  Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"> 
<resources> 
<custom-resource res-type="java.util.Properties" jndi-name="jndi/iprofile" 
       factory-class="org.glassfish.resources.custom.factory.PropertiesFactory"> 
    <property name="name" value="${webapp.profile}" /> 
</custom-resource> 
<jdbc-resource enabled="true" jndi-name="jdbc/users" object-type="user" pool-name="MY-POOL"> 
    <description/> 
</jdbc-resource> 
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerXADataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="MY-POOL" non-transactional-connections="false" ping="false" pool-resize-quantity="2" pooling="true" res-type="javax.sql.XADataSource" statement-cache-size="0" statement-leak-reclaim="false" statement-leak-timeout-in-seconds="0" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false"> 
    <property name="serverName" value=""/> 
    <property name="PortNumber" value=""/> 
    <property name="DatabaseName" value=""/> 
    <property name="User" value=""/> 
    <property name="Password" value=""/> 
</jdbc-connection-pool> 

我想DatabaseName屬性與Xpto POM中定義的值。其他配置文件對於database.dbname屬性具有不同的值。

回答

1

您可以用maven-replacer-plugin來解決這類任務。它可以用來替換文件中的值。

下面是一個例子:

定義你的財產像你已經做了:

<properties> 
<database.dbname>Xpto</database.dbname> 
</properties> 

變化要在glassfish-resources.xml取代,看起來像這樣的屬性:

<property name="DatabaseName" value="DATABASENAME"/> 

使用值DATABASENAME d代替。您可以使用任何值,但在您要修改的文件中它必須是唯一的。

更改maven-war-plugin的配置看起來像這樣:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-war-plugin</artifactId> 
    <version>2.1.1</version> 
    <executions> 
     <execution> 
      <id>prepare-war</id> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>exploded</goal> 
      </goals> 
     </execution> 
     <execution> 
      <id>default-war</id> 
      <phase>package</phase> 
      <goals> 
       <goal>war</goal> 
      </goals> 
      <configuration> 
       <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

maven-replacer-plugin wiki (have a look at this page for details how the maven-replacer-plugin and the maven-war-plugin work together)

目前,替代品的插件需要訪問到目標資源 包裝之前,歸檔。但是,標準WAR包裝 不會公開其他插件中使用的網絡資源(在src/main/webapp下的任何東西) 並作爲單個執行運行。幸運的是,我們可以通過 調用war:exploded在構建 生命週期中的較早版本中複製這些資源,以便它們可以被maven-replacer插件使用。當 創建WAR僞像時, 標準包的使用將使用修改後的Web資源。

添加maven-replacer-plugin以下配置您pom.xml

<plugin> 
    <groupId>com.google.code.maven-replacer-plugin</groupId> 
    <artifactId>replacer</artifactId> 
    <version>1.5.3</version> 
    <executions> 
     <execution> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>replace</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <file>target/${project.build.finalName}/WEB-INF/glassfish-resources.xml</file> 
     <token>DATABASENAME</token> 
     <value>${database.dbname}</value> 
    </configuration> 
</plugin> 

正如你可以看到令牌DATABASENAME被替換的${database.dbname}值。這是在prepare-package階段完成的,即在將Web應用程序內容複製到分解的WAR目錄之後,但在將目錄打包爲WAR文件之前。

如果更換不起作用,您可能需要將maven-war-plugin降級到版本2.0.1

<replacements> 
    <replacement> 
     <token>DATABASENAME</token> 
     <value>${database.dbname}</value>    
    </replacement> 
</replacements> 

參見: