Maven, yuicompressor, filter и maven-war-plugin: сжимайте только для 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 чистая установка

тогда ресурсы должны храниться в .war как есть. Текущая проблема заключается в том, что плагин maven war обнуляет вывод при фильтрации как в target, так и в .war в обоих случаях. Как обойти эту проблему?


person Green Root    schedule 02.08.2016    source источник


Ответы (1)


Я решил свою проблему, переместив «конфигурацию» фазы «пакет» maven-war-plugin в фазу «подготовка пакета». См. Рабочий пример ниже:

  <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>
person Green Root    schedule 03.08.2016