【问题标题】:applying filters to html file using maven使用 Maven 将过滤器应用于 html 文件
【发布时间】:2013-06-15 23:34:43
【问题描述】:

我正在尝试将属性文件中的属性读入我的父级pom.xml。从那里我需要将读取的属性放入我的 html 文件中定义的变量中。

包含属性文件的代码:

  <plugins>
            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <executions>
               <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
              <execution>
                <phase>initialize</phase>
                <goals>
                  <goal>read-project-properties</goal>
                </goals>
                <configuration>
                  <files>
                    <file>${basedir}/build.${build.env}.properties</file>
                  </files>
                </configuration>
              </execution>
            </executions>
          </plugin>


            <!-- Maven War file generator plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>${basedir}/src/main/java</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <includes>
                                <include>**/*.properties</include>
                                <filtering>false</filtering>
                                <include>**/*.xml</include>
                                <filtering>false</filtering>
                                <include>**/*.css</include>
                                <filtering>false</filtering>
                                <include>**/*.html</include>
                                <filtering>true</filtering>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
                <version>2.3</version>
            </plugin>

            <!-- Maven compiler plugin -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
                <version>2.3</version>
            </plugin>
           </plugins>

我之前使用过 ant,我使用 filterset 标签来应用这些过滤器,例如

build.xml 中的代码:

   <filterset>
            <filter token="HOSTNAME_PREFIX" value="${hostname.prefix}"/>
            <filter token="MIN_SUFFIX" value="${min.suffix}"/>
            <filter token="APP_VERSION" value="${build.app.version}"/>
        </filterset>

但我不知道如何使用 maven 实现相同的目标。

index.html 中要替换的代码是:

<link rel="shortcut icon" href="@HOSTNAME_PREFIX@appimages/app-logo.ico" type="image/png"/>
<link rel="stylesheet" href="@HOSTNAME_PREFIX@css/jquery.mobile-1.2.0@MIN_SUFFIX@.css" />

【问题讨论】:

    标签: maven ant properties pom.xml


    【解决方案1】:

    您需要在 war 插件配置中使用多个资源元素来实现这一点,一个用于您要过滤的资源,一个用于您不想过滤的资源。

    <webResources>
        <resource>
            <directory>${basedir}/src/main/java</directory>
            <targetPath>WEB-INF/classes</targetPath>
            <filtering>false</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.css</include>
            </includes>
        </resource>
        <resource>
            <directory>${basedir}/src/main/java</directory>
            <targetPath>WEB-INF/classes</targetPath>
            <filtering>true</filtering>
            <includes>
                <include>**/*.html</include>
            </includes>
        </resource>
    </webResources>
    

    另外,我建议不要把你的资源放在/src/main/java;该目录是用于 Java 文件的,而不是资源。

    【讨论】:

    • 感谢您的回答。只是想知道这将如何插入所需属性的值。正如我在蚂蚁的例子中提到的......
    • 提供属性值的方法有多种。请查看Maven POM reference docs。您可能还会发现resource filtering overview 很有帮助。
    • 它没有解决我替换 index.html 中标记的目的。参考文献中提到的解决方案是创建一个单独的 filter.properties 文件,然后将值从它传递到 application.properties 文件,但该解决方案对我不起作用
    • application.properties 只是他们在文档中使用的示例。您不需要使用该文件。如果您使用了单独的 filter.properties 文件,您是否也将 &lt;filters&gt; 元素添加到 &lt;build&gt; 中,如示例中所示?如果您不想将它们放在单独的文件中,我链接的另一个文档提到了在 POM(&lt;properties&gt; 块)中定义属性的其他方法。
    • 是的,我在 中添加了所需的文件。实际上我有 3 个属性文件,其中我根据要构建的环境加载 1 个文件。所以我想要做的是从该属性文件中读取属性并替换我在问题中提到的 html 代码中设置的过滤器。问题是我能够从属性文件中读取属性,但无法将其应用于 html 文件..
    猜你喜欢
    • 2018-01-09
    • 1970-01-01
    • 2011-09-07
    • 2019-03-09
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 2011-11-01
    相关资源
    最近更新 更多