2014-07-01 1 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> 
... 

그리고 글래스 피쉬-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> 

내가 치어에 정의 Xpto 값과 데이터베이스 이름 속성을 설정합니다. 다른 프로파일은 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를 호출 할 수 있습니다. : 빌드를 앞두고이 자원들을 복사하여 exploration을하면 lifecycle이되므로 maven-replacer-plugin에서 사용할 수 있습니다. 표준 패키지 사용은 WAR 이슈를 생성 할 때 수정 된 웹 리소스를 사용합니다.

pom.xmlmaven-replacer-plugin의 다음과 같은 구성을 추가

<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 단계에서 수행됩니다. 즉, 웹 응용 프로그램 내용이 분해 된 WAR 디렉토리에 복사 된 후 디렉토리가 WAR 파일로 패키지화되기 전에 수행됩니다.

교체가 작동하지 않는 경우 maven-war-plugin2.0.1으로 다운 그레이드해야 할 수 있습니다.

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

도 참조 :