【问题标题】:Can I update file content using Maven Plugin?我可以使用 Maven 插件更新文件内容吗?
【发布时间】:2017-10-31 13:40:55
【问题描述】:

我想根据属性文件的值更新我的 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 插件可以让我使用它。

谢谢, 保罗

【问题讨论】:

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


    【解决方案1】:

    我很幸运使用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 行。

    这并不是你想要的,但我已经使用这个策略来做你想做的事情。

    您可以使用properties-maven-pluginhttp://www.mojohaus.org/properties-maven-plugin/usage.html 读取属性文件(因此您不必在 POM 中硬编码您的属性,因此您可以根据配置文件或其他方式读取不同的文件)

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

    【讨论】:

    • 感谢您回复我。我目前拥有的方法是允许我通过在过滤器部分添加一个条目来从属性文件中读取值。我更喜欢指定文件夹(src/main/resources/environmentProperties)并进行 Maven 锻炼并读取所有文件,这样如果添加了另一个文件,我就不需要修改 POM。我还希望它根据属性文件的数量决定在 web.xml 和 context.xml 中创建正确数量的条目。这将涉及到指定模板时的某种循环。
    • 去看看StringTemplate的网站,它是一个模板引擎,会做你想做的。 stringtemplate.org - 另外,我认为属性插件可以带通配符,但肯定会带多个文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    相关资源
    最近更新 更多