使用maven将过滤器应用于html文件

时间:2013-06-19 12:26:05

标签: maven ant properties pom.xml

我正在尝试将属性文件中的属性读入我的父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" />

1 个答案:

答案 0 :(得分:0)

您需要在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文件,而不是哪些资源。