我正在开发一个 Java Web应用程序,它使用Maven来构建它,并且有很多 JavaScript. 我想要做的是基本上在我的JSP文件中有这个: c:choose c:when test="PRODUCTION" script type="text/javascript" src="/path/to/app
我想要做的是基本上在我的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)使用包含环境名称的属性文件.说具有以下内容的environment.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
现在,在运行时,从environment.properties中读取environment.name属性,并根据属性值使用相应的.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>
并设置Web资源过滤
<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)
