gpt4 book ai didi

java - maven过滤在构建过程中替换多个 token ?

转载 作者:行者123 更新时间:2023-12-02 09:19:08 24 4
gpt4 key购买 nike

我可以用模式 ${..} 替换 token 下src/main/resources 。我还想用模式 @{} 替换 token 在特定文件下 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>

最佳答案

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

因为无论如何你都想用一个值替换占位符。您可以在 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

测试.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

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

关于java - maven过滤在构建过程中替换多个 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36622396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com