gpt4 book ai didi

jsp - 使用 Maven 配置文件在缩小/未压缩的 JS 之间切换?

转载 作者:行者123 更新时间:2023-12-04 23:21:10 24 4
gpt4 key购买 nike

我正在开发一个 Java Web 应用程序,它使用 Maven 进行构建过程,并且有很多 JavaScript。

我想要做的基本上是在我的 JSP 文件中包含这个:

<c:choose>
<c:when test="PRODUCTION">
<script type="text/javascript" src="/path/to/app.min.js"></script>
</c:when>
<c:otherwise>
<script type="text/javascript" src="/path/to/script1.js"></script>
<script type="text/javascript" src="/path/to/script2.js"></script>
</c:otherwise
</c:choose>

这可能与 Maven 配置文件有关吗?

最佳答案

看看maven resource filtering .您可以使用它来用实际值覆盖资源文件中的属性。

1) 使用包含环境名称的属性文件。说 环境.properties 其中有以下内容。

environment.name=${environment.name}

现在在您的 pom 文件中使用以下内容
<properties>
<environment.name>TEST</environment.name>
</properties>

<profiles>
<profile>
<id>PROD</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<environment.name>PROD</environment.name>
</properties>
</profile>
</profiles>

并在你的 pom 中指定资源过滤
  <resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>

如果您使用此配置文件运行构建,
mvn clean install -P PROD

environment.properties 将被处理为
environment.name=PROD

如果您不使用此配置文件
mvn clean install

它将被处理为
environment.name=TEST

现在在运行时,阅读 环境名称属性(property)来自 环境.properties 并根据属性值使用适当的 .js 文件。

或者

2)如果您不想从属性文件中读取,您可以过滤jsp本身。
<properties>
<js.suffix></js.suffix>
</properties>

<profiles>
<profile>
<id>PROD</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<js.suffix>min.</js.suffix>
</properties>
</profile>
</profiles>

并设置网络资源过滤
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>

并在您的jsp中添加此类内容
 <script type="text/javascript" src="/path/to/app.${js.suffix}js"></script>

(假设未压缩的为app.js,压缩的为app.min.js)

关于jsp - 使用 Maven 配置文件在缩小/未压缩的 JS 之间切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26474401/

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