2014-10-20 39 views
4

我正在研究一个使用Maven构建过程的Java Web应用程序,并且有很多JavaScript。使用Maven配置文件在缩小/未压缩的JS之间切换?

我想要做的基本上是有这个在我的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配置文件?

回答

5

看一看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.name属性environment.properties并根据属性值使用相应的.js文件。

OR

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)

2

我的解决方案有点不同。我使用了minify-maven-plugin,并且在开发过程中我使用它创建了一个合并但未缩小的app.js,当我在制作时,我使用它创建缩小文件。我将它们都命名为app.js,因此我不必使用Maven更改我的JSP文件。

例如:

<!-- Resource minification --> 
<plugin> 
    <groupId>com.samaxes.maven</groupId> 
    <artifactId>minify-maven-plugin</artifactId> 
    <version>1.7.2</version> 
    <executions> 
    <!-- JavaScript --> 
    <execution> 
     <id>minify-js</id> 
      <phase>generate-sources</phase> 
     <configuration> 
     <charset>utf-8</charset> 
     <skipMerge>false</skipMerge> 
     <skipMinify>${javascript.skipMinify}</skipMinify> 
     <nosuffix>true</nosuffix> 
     <webappSourceDir>${project.basedir}/src/main/webapp</webappSourceDir> 
     <jsSourceDir>assets/js</jsSourceDir> 
     <jsSourceIncludes> 
      <jsSourceInclude>*.js</jsSourceInclude> 
     </jsSourceIncludes> 
     <jsFinalFile>app.js</jsFinalFile> 
     </configuration> 
     <goals> 
     <goal>minify</goal> 
     </goals> 
    </execution> 
    </executions> 
</plugin> 

然后,我使用Maven型材切换${javascript.skipMinify}属性,例如:

<profile> 
    <id>development</id> 
    <activation> 
    <activeByDefault>true</activeByDefault> 
    </activation> 
    <properties> 
    <javascript.skipMinify>true</javascript.skipMinify> 
    </properties> 
</profile> 
<profile> 
    <id>production</id> 
    <properties> 
    <javascript.skipMinify>false</javascript.skipMinify> 
    </properties> 
</profile> 
相关问题