在建立战争时,文件在maven项目中被覆盖

Dav*_*hao 6 yui-compressor maven maven-war-plugin

我正在使用maven构建一个Web应用程序项目,并且打包设置为"war".我还使用YUI压缩器插件来压缩webapp目录中的javascript代码.我已经设置了YUI压缩器,如下所示:

<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.3.0</version>
    <executions>
        <execution>
        <phase>process-resources</phase>
        <goals>
            <goal>compress</goal>
        </goals>
        </execution>
    </executions>
    <configuration>
        <excludes>
        <exclude>**/ext-2.0/**/*.js</exclude>
        <exclude>**/lang/*.js</exclude>
        <exclude>**/javascripts/flot/*.js</exclude>
        <exclude>**/javascripts/jqplot/*.js</exclude>
        </excludes>
        <nosuffix>true</nosuffix>
        <force>true</force>
        <jswarn>false</jswarn>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

如果我这样做:mvn process-resources,src/main/webapp将被复制到target/webapp-1.0 /目录,并压缩javacripts.但是,当我运行mvn install时,所有压缩的javascripts都会被覆盖,显然打包过程会在构建war文件之前从main/webapp复制一次内容.

我怎么能绕过这个?

use*_*849 11

正如您所注意到的那样,/src/main/webappdir(aka warSourceDirectory)内容不会被复制到项目目录中进行打包,直到war包插件在包阶段执行.当war插件完成时,已经构建了存档; 修改这些资源为时已晚.如果要压缩的.js文件被移动到另一个目录(在...之外/src/main/webapp),那么您可以执行类似下面的操作.

为了测试,我创建了一个${basedir}/src/play包含几个文件的目录.我使用resource插件作为示例; 你需要用你想要的YUI压缩器插件配置替换那个配置,只需将<webResource>元素添加到你的war插件配置中,如下所示; 更多关于war插件示例的信息.我的战争结束了我想要的其他文件.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-resources</id>
      <phase>process-resources</phase>
      <goals><goal>copy-resources</goal></goals>
      <configuration>
        <outputDirectory>${project.build.directory}/tmpPlay</outputDirectory>
        <resources>
          <resource>
             <directory>${project.basedir}/src/play</directory>
             <includes>
                <include>**/*</include>
             </includes>
          </resource>
        </resources>
       </configuration>
    </execution>
  </executions>
</plugin>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <executions>
    <execution>
      <id>default-war</id>
      <configuration>
        <webResources>
          <resource>
            <directory>${project.build.directory}/tmpPlay</directory>
            <targetPath>WEB-INF/yourLocationHere</targetPath>
            <includes>
              <include>**/*</include>
            </includes>
          </resource>
        </webResources>
      </configuration>
    </execution>
  </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)