4

我正在开发一个使用 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 配置文件有关吗?

4

2 回答 2

6

看看Maven 资源过滤。您可以使用它来用它们的实际值覆盖资源文件中的属性。

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)

于 2014-10-21T06:35:58.770 回答
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>
于 2014-12-11T12:04:38.710 回答