Maven、yuicompressor、filter 和 maven-war-plugin:仅为 PROD 压缩

Maven, yuicompressor, filter and maven-war-plugin: compress only for the PROD

我关注pom.xml:

  <build>
    <finalName>edrive</finalName>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory> 
            <includes>
                <include>**/*xml</include>
                <include>**/*properties</include>
            </includes>
        </resource>
    </resources>

    <plugins>

      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>prepare</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>exploded</goal>
            </goals>
          </execution>
          <execution>
            <id>default-war</id>
            <phase>package</phase>
            <goals>
              <goal>war</goal>
            </goals>
            <configuration>
              <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <filters>
            <filter>src/main/filters/filter.properties</filter>
          </filters>
          <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
          <webResources>
            <resource>
              <directory>${project.build.directory}/${project.build.finalName}/resources</directory>
              <filtering>true</filtering>
              <targetPath>resources</targetPath>
            </resource>
          </webResources>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
            </manifest>
          </archive>
          <archiveClasses>true</archiveClasses>
        </configuration>
      </plugin>

    </plugins>
  </build>

  <profiles>
    <profile>
      <id>prod</id>
      <activation>
        <property>
          <name>prodEnabled</name>
          <value>true</value>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <version>1.5.1</version>
            <executions>
              <execution>
                <phase>process-resources</phase>
                <goals>
                  <goal>compress</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <jswarn>false</jswarn>
              <nosuffix>true</nosuffix>
              <force>true</force>
              <sourceDirectory>WebContent/resources</sourceDirectory>
              <outputDirectory>${project.build.directory}/${project.build.finalName}/resources</outputDirectory>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>

我想压缩资源,但只针对一个配置文件。说,我执行

mvn clean install -DprodEnabled=true

预期结果应该是.war中的压缩资源。如果我执行

mvn clean install

那么资源应该按原样保存在 .war 中。当前的问题是 Maven war 插件在过滤目标和 .war 时将输出清零,在这两种情况下。如何解决这个问题?

我通过将 maven-war-plugin 的 "package" 阶段的 "configuration" 移动到 "prepare-package" 阶段解决了我的问题。请参阅下面的工作示例:

  <build>
    <finalName>edrive</finalName>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory> 
            <includes>
                <include>**/*xml</include>
                <include>**/*properties</include>
            </includes>
        </resource>
    </resources>

    <plugins>

      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>prepare</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>exploded</goal>
            </goals>
            <configuration>
              <filters>
                <filter>src/main/filters/filter.properties</filter>
          </filters>
          <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
          <webResources>
            <resource>
              <directory>WebContent/resources</directory>
              <filtering>true</filtering>
              <targetPath>resources</targetPath>
            </resource>
          </webResources>
          <warSourceDirectory>WebContent</warSourceDirectory>
            </configuration>

          </execution>
          <execution>
            <id>default-war</id>
            <phase>package</phase>
            <goals>
              <goal>war</goal>
            </goals>
            <configuration>
              <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
            </configuration>
          </execution>
        </executions>
        <configuration>

          <failOnMissingWebXml>false</failOnMissingWebXml>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
            </manifest>
          </archive>
          <archiveClasses>true</archiveClasses>
        </configuration>
      </plugin>

    </plugins>
  </build>

  <profiles>
    <profile>
      <id>prod</id>
      <activation>
        <property>
          <name>prodEnabled</name>
          <value>true</value>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <version>1.5.1</version>
            <executions>
              <execution>
                <phase>prepare-package</phase>
                <goals>
                  <goal>compress</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <jswarn>false</jswarn>
              <nosuffix>true</nosuffix>
              <force>true</force>
              <sourceDirectory>${project.build.directory}/${project.build.finalName}/resources</sourceDirectory>
              <outputDirectory>${project.build.directory}/${project.build.finalName}/resources</outputDirectory>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>