2

I would like to update the content of my context.xml and web.xml files based on the value of property files. I want to setup database connections the can be used by my web app. Is it possible to add thhis content in a dynamic way?

I currently have some updating going on using the following build section in my 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>

In my web.xml file I have the following

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

I would really prefer to be able to have as many properties files as required in the environmentProperties folder and then update the web.xml (and context.xml) files to add the appropriate entries for each environment. Is this possible.

I have seen Apache Velocity do looping constructs, but don't know if there is a Maven plugin to allow me to utilise this.

Thanks, Paul

ceepan
  • 87
  • 2
  • 8

1 Answers1

2

I have had good luck using stringtemplate http://www.stringtemplate.org/ to take a template and render it. I use it to create Docker files based on my current maven version so I don't have to update them every time I release a new jar version.

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

This plugin config takes this 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"]

>>

It renders the CMD line with the correct name of the jar to execute.

This is not exactly what you're asking for, but I have used this strategy to do what you want to do.

You can read a properties file (so you don't have to hard-code your properties in the POM, and so you can read different files based on profiles or whatever) with the properties-maven-plugin at 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>
mikeb
  • 10,578
  • 7
  • 62
  • 120
  • Thanks for getting back to me. The approach that I currently have is allowing me to read the values from the properties files by adding an entry in the filters section. What I would prefer though is to specify the folder (src/main/resources/environmentProperties) and have Maven workout and read all files, so that if another is added I don't need to modify the POM. I also want it to make the decision to create the correct number of entries within the web.xml and context.xml based on the number of properties files. This would involve some sort of looping in specifiying the template. – ceepan Oct 31 '17 at 16:30
  • Go look at StringTemplate's website, it's a template engine and will do what you want. http://www.stringtemplate.org/ - Also, I think the properties plugin can take wildcards, but it will definitely take multiple files. – mikeb Oct 31 '17 at 18:03