2017-10-31 16 views
0

プロパティファイルの値に基づいて、私のcontext.xmlおよびweb.xmlファイルの内容を更新したいと思います。 Webアプリケーションで使用できるデータベース接続を設定したい。このコンテンツを動的に追加することは可能ですか?私は現在、いくつかの更新は私がMaven Pluginを使用してファイルコンテンツを更新できますか?

<resource-ref> 
    <description>Env1 Database Connection</description> 
    <res-ref-name>jdbc/Env1</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 

<resource-ref> 
    <description>Env2 Database Connection</description> 
    <res-ref-name>jdbc/Env2</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 

<resource-ref> 
    <description>Env3 Database Connection</description> 
    <res-ref-name>jdbc/Env3</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 

<resource-ref> 
    <description>Env4 Database Connection</description> 
    <res-ref-name>jdbc/Env4</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 

次している私のweb.xmlファイルでは、私のPOM

<build> 
    <finalName>ROOT</finalName> 
    <filters> 
     <filter>src/main/resources/environmentProperties/env1.properties</filter> 
     <filter>src/main/resources/environmentProperties/env2.properties</filter> 
     <filter>src/main/resources/environmentProperties/env3.properties</filter> 
     <filter>src/main/resources/environmentProperties/env4.properties</filter> 
    </filters> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>${maven-war-plugin-version}</version> 
      <configuration> 
       <webResources> 
        <resource> 
         <directory>${basedir}\src\main\resources\META-INF</directory> 
         <filtering>true</filtering> 
         <targetPath>META-INF</targetPath> 
         <includes> 
          <include>**\context.xml</include> 
         </includes> 
        </resource> 
       </webResources> 
       <warSourceDirectory>src/main/webapp</warSourceDirectory> 
       <webXml>src/main/webapp/WEB-INF/web.xml</webXml> 
      </configuration> 
     </plugin> 
    </plugins> 

</build> 

で次のビルドセクションを使用して起こっている

は、私が本当にできることを好むだろうenvironmentPropertiesフォルダに必要な数のプロパティファイルを作成し、web.xml(およびcontext.xml)ファイルを更新して各環境に適切なエントリを追加します。これは可能ですか?

私はApache Velocityのループ構造を見ましたが、これを利用できるようにするMavenプラグインがあるかどうかはわかりません。

おかげで、 ポール

答えて

0

は、私は、テンプレートを取り、それをレンダリングするstringtemplatehttp://www.stringtemplate.org/を使用して幸運を持っていました。現在のMavenバージョンに基づいてDockerファイルを作成するために使用していますので、新しいJARバージョンをリリースするたびに更新する必要はありません。

  <plugin> 
       <groupId>com.webguys</groupId> 
       <artifactId>string-template-maven-plugin</artifactId> 
       <version>1.1</version> 
       <configuration> 
        <templates> 
         <template> 
          <directory>${basedir}/src/main/resources/templates</directory> 
          <name>dockerfile</name> 
          <target> 
           ${basedir}/target/Dockerfile 
          </target> 
          <properties> 
           <jar>${project.build.finalName}</jar> 
          </properties> 
         </template> 
        </templates> 
       </configuration> 
       <executions> 
        <execution> 
         <phase>generate-sources</phase> 
         <goals> 
          <goal>render</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 

このプラグインの設定は、このdockerfileをとります

dockerfile(jar) ::= << 

FROM anapsix/alpine-java 
MAINTAINER saisols.com 
RUN mkdir /opt/app 
ADD . /opt/app 
WORKDIR /opt/app 
CMD ["java", "-jar", "<jar>.jar"] 

>> 

それは実行するjarファイルの正しい名前でCMDラインを描画します。

これはあなたが求めているものではありませんが、私はあなたがしたいことをするためにこの戦略を使用しました。

あなたは、プロパティファイルを読むことができます(あなたがPOMであなたの特性をハードコードする必要はありませんし、あなたがプロファイルまたは何に基づいて異なるファイル読むことができます)http://www.mojohaus.org/properties-maven-plugin/usage.html

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>properties-maven-plugin</artifactId> 
    <version>1.0.0</version> 
    <executions> 
     <execution> 
     <phase>initialize</phase> 
     <goals> 
      <goal>read-project-properties</goal> 
     </goals> 
     <configuration> 
      <files> 
      <file>etc/config/dev.properties</file> 
      </files> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
properties-maven-pluginとし
+0

私に戻ってくれてありがとう。私が現在持っているアプローチは、フィルタセクションにエントリを追加することによってプロパティファイルから値を読み取ることができるようにすることです。ただし、フォルダ(src/main/resources/environmentProperties)を指定してMavenのワークアウトを行い、すべてのファイルを読み込むことをお勧めします。別のファイルが追加された場合、POMを変更する必要はありません。また、プロパティファイルの数に基づいてweb.xmlとcontext.xml内に正しい数のエントリを作成することを決定したいと思っています。これは、テンプレートを特定する際に何らかのループを伴います。 – ceepan

+0

StringTemplateのWebサイトを見てみましょう。これはテンプレートエンジンであり、必要な処理を行います。 http://www.stringtemplate.org/ - また、私はプロパティプラグインはワイルドカードを取ることができると思うが、確かに複数のファイルを取るだろう。 – mikeb

関連する問題