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

ngynwnxp  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(365)

我想根据属性文件的值更新context.xml和web.xml文件的内容。我想设置可由我的web应用使用的数据库连接。有没有可能以动态的方式添加这些内容?
我目前正在使用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)文件,为每个环境添加适当的条目。这可能吗。
我见过apachevelocity做循环构造,但不知道是否有maven插件允许我使用它。
谢谢,保罗

9gm1akwq

9gm1akwq1#

我很幸运使用了 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"]

>>

它呈现 CMD 使用要执行的jar的正确名称。
这并不是你想要的,但我用这个策略来做你想做的事。
您可以使用 properties-maven-plugin 在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>

相关问题