我可以使用Maven插件更新文件内容吗?

时间:2017-10-31 13:40:55

标签: java maven-3 maven-plugin velocity maven-resources-plugin

我想根据属性文件的值更新我的context.xml和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>

在我的web.xml文件中,我有以下内容

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

我更希望能够拥有environmentProperties文件夹中所需的尽可能多的属性文件,然后更新web.xml(和context.xml)文件以为每个环境添加适当的条目。这可能。

我看过Apache Velocity做循环结构,但不知道是否有Maven插件允许我使用它。

谢谢, 保罗

1 个答案:

答案 0 :(得分:0)

我很幸运使用stringtemplate http://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中对您的属性进行硬编码,因此您可以根据个人资料或其他内容阅读不同的文件)properties-maven-plugin at { {3}}

<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>
相关问题