【问题标题】:Using Maven profiles to switch between minified/uncompressed JS?使用 Maven 配置文件在缩小/未压缩 JS 之间切换?
【发布时间】:2014-10-20 20:31:17
【问题描述】:

我正在开发一个使用 Maven 进行构建过程的 Java Web 应用程序,并且有很多 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 配置文件有关吗?

【问题讨论】:

    标签: jsp maven minify maven-profiles


    【解决方案1】:

    看看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)

    【讨论】:

      【解决方案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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-18
        • 2017-02-03
        • 2011-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多