看看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>
并设置网络资源过滤
<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)