maven 过滤在构建期间替换多个标记?

maven filtering replace multiple tokens during build?

我可以在 src/main/resources 下用模式 ${..} 替换令牌。我还想用特定文件下的模式 @{} 替换标记 src/main/webapp/WEB-INF/content/test.jsp 或目录 src/main/webapp/WEB-INF/content.

我尝试添加 <delimiter>@{test.version}</delimiter> 但我不确定 如何制作特定目录src/main/webapp/WEB-INF/content

test.version 将在运行时出现,如 mvn clean install -Dtest.version=100

<build>
    <resources>
    <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*</include>
            </includes>
        </resource> 

    </resources>
 </build>



<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.5</version>
    <inherited>true</inherited>
    <configuration>
        <encoding>UTF-8</encoding>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>xls</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
         <delimiters>
          <delimiter>${*}</delimiter>
          <delimiter>@{*}</delimiter>
        </delimiters>
    </configuration>
</plugin>

文件 src/main/webapp/WEB-INF/content/test.jsp 是一个网络资源:它在 src/main/webapp 下,因此它由 maven-war-plugin 而不是 maven-resources-plugin.

处理

您还可以 filter web resources 使用此插件,借助 filtering 属性。您可以使用以下配置来过滤 src/main/webapp/WEB-INF/content:

下的所有 Web 资源
<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <webResources>
      <resource>
        <directory>src/main/webapp/WEB-INF/content</directory>
        <filtering>true</filtering>
      </resource>
    </webResources>
  </configuration>
</plugin>

如果你只想过滤test.jsp,你也可以通过只包含这个文件来限制它,并且有

<resource>
  <directory>src/main/webapp/WEB-INF/content</directory>
  <filtering>true</filtering>
  <includes>
    <include>test.jsp</include>
  </includes>
</resource>

但是,您目前无法使用此插件设置自定义分隔符(问题 MWAR-225 and unresolved), so you will have to stick to the default ones, which are ${...}@...@(但不是 @{...})。

这可能是一种方法:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
        <execution>
            <id>Filter specific resources</id>
            <phase>process-resources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>
                    [Place to put filtered result]
                </outputDirectory>
                <resources>
                    <resource>
                        <directory>
                            src/main/webapp/WEB-INF/content
                        </directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
                <delimiters>
                    <delimiter>${*}</delimiter>
                    <delimiter>@{*}</delimiter>
                </delimiters>
                <!--<useDefaultDelimiters>false</useDefaultDelimiters>-->
            </configuration>
        </execution>
    </executions>
</plugin>

占位符 @{*} 似乎不适用于替换插件。

因为您无论如何都想用一个值替换占位符。您可以在 JSP 文件中使用 #{test.version}。对于过滤,您可以定义不同的目录。请参阅下面的代码段。

假设结构如下。

pom.xml
src/main/resources/hello.txt
src/main/webapp/WEB-INF/content/test.jsp
src/main/webapp/WEB-INF/web.xml

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sub.optimal.example</groupId>
    <artifactId>superapp</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>superapp</name>

    <properties>
        <test.version>world</test.version>
    </properties>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <targetPath>../webapp/WEB-INF/content</targetPath>
                <filtering>true</filtering>
                <directory>src/main/webapp/WEB-INF/content</directory>
            </resource>
        </resources>
        <plugins>      
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <inherited>true</inherited>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <delimiters>
                        <delimiter>${*}</delimiter>
                        <delimiter>#{*}</delimiter>
                        <!-- this delimiter is not recognized -->
                        <delimiter>@{*}</delimiter>
                    </delimiters>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>target/webapp</directory>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

hello.txt

Hello $ ${test.version} txt
Hello # #{test.version} txt
Hello @ @{test.version} txt

test.jsp

Hello $ ${test.version} jsp
Hello # #{test.version} jsp
Hello @ @{test.version} jsp

构建 WAR 文件

mvn clean package

输出

> jar tf superapp-1.0-SNAPSHOT.war
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/classes/
WEB-INF/content/
WEB-INF/classes/hello.txt
WEB-INF/content/test.jsp
WEB-INF/web.xml
META-INF/maven/sub.optimal.example/superapp/pom.xml
META-INF/maven/sub.optimal.example/superapp/pom.properties

# copy the file into a temp directory and extract it
# jar xf superapp-1.0-SNAPSHOT.war

>cat WEB-INF/classes/hello.txt
Hello $ world txt
Hello # world txt
Hello @ @{test.version} txt

>cat WEB-INF/content/test.jsp
Hello $ world jsp
Hello # world jsp
Hello @ @{test.version} jsp

该示例仅展示了想法,可能需要进行一些调整以排除文件、文件位置等。