【问题标题】:maven filtering replace multiple tokens during build?Maven过滤在构建期间替换多个令牌?
【发布时间】:2016-08-05 23:10:27
【问题描述】:

我可以用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>

【问题讨论】:

  • 不幸的是,maven war 插件不支持自定义分隔符:issues.apache.org/jira/browse/MWAR-225 你必须使用${...}
  • 如果你喜欢这个功能,请就@Tunaki 提到的问题提出请求/投票。
  • 如果您想使用与${*} 不同的分隔符,请查看我的回答。但是,如果您也可以在 JSP 页面中使用 ${*},请查看来自 Tunaki 的答案。这样就不会那么麻烦了。
  • 具有讽刺意味的是,自 maven-war-plugin 3.0.0 起,MWAR-225 已于 2016 年 4 月 15 日关闭

标签: java maven maven-resources-plugin


【解决方案1】:

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

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

假设以下结构。

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

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

【讨论】:

  • 除非这在构建真正的 WAR 时不起作用。 JSP 不应该在target/classes 之下。
  • @Tunaki 查看我更新的示例。关于文件The example only shows the idea and might need some tweaks ...的位置我更新了这部分以包含文件位置。
  • 是的,它确实需要很多调整 :) 但现在它确实可以工作了,感谢更新!
  • 非常感谢 SubOptimal 提供如此详细的回答
【解决方案2】:

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

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

<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 并且未解决),因此您必须坚持使用默认分隔符,which are ${...}@...@(但不是 @{...} )。

【讨论】:

  • 哼为什么要投反对票?它回答了这个问题(有一个错误,所以 OP 不能做他们想做的事)所以我提供了替代方案。没有重复提及特定的 maven-war-plugin 问题。而且我相当确定它在我测试后可以正常工作。
【解决方案3】:

这可能是一种方法:

<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 不是项目资源。它是一个网络资源。它不应该输出到target/classes
  • 标签outputDirectory可以指定放置位置。
  • 是的,那会是什么?看看 SubOptimal 的答案。这一点都不简单……
猜你喜欢
  • 2012-01-19
  • 2018-12-11
  • 2023-04-10
  • 1970-01-01
  • 2021-10-24
  • 2012-01-08
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多