在构建war时,maven项目中的文件被覆盖

在构建war时,maven项目中的文件被覆盖,maven,yui-compressor,maven-war-plugin,Maven,Yui Compressor,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</

我正在使用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>
<executions>
    <execution>
        ....
        <phase>install</phase>
        ....
    </execution>
<executions>

net.alchim31.maven
yuicompressor maven插件
1.3.0
过程资源
压缩
**/ext-2.0/***.js
**/lang/*.js
**/javascripts/flot/*.js
**/javascripts/jqplot/*.js
真的
真的
假的
如果我这样做:mvn process resources,src/main/webapp将被复制到target/webapp-1.0/目录,Javascripts将被压缩。但是,当我运行mvn install时,所有压缩的javascript都会被覆盖,显然打包过程在构建war文件之前会从main/webapp复制一次内容


我该如何解决这个问题呢?

在您的执行指令中,将应用压缩和复制的阶段设置为安装阶段,希望这能起到作用。代码应该是这样的:

<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>
<executions>
    <execution>
        ....
        <phase>install</phase>
        ....
    </execution>
<executions>

....
安装
....

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

为了测试,我创建了一个
${basedir}/src/play
目录,其中包含几个文件。我使用
资源
插件作为示例;您可以使用所需的YUI压缩插件配置替换该配置,只需将
元素添加到
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>

org.apache.maven.plugins
maven资源插件
复制资源
过程资源
复制资源
${project.build.directory}/tmpPlay
${project.basedir}/src/play
**/*
org.apache.maven.plugins
maven战争插件
默认战争
${project.build.directory}/tmpPlay
WEB-INF/yourLocationHere
**/*

我认为@user944849答案是正确的,至少是正确答案中的一个。另一种存档方法是从maven war插件配置中排除修改后的javascript目录,例如:

<plugin>
    <artifactId> maven-war-plugin </artifactId>
    <configuration>
        <warSourceExcludes>**/external/ dojo/**/*.js </warSourceExcludes>
    </configuration>
</plugin>

maven战争插件
**/外部/dojo/***.js

这将告诉maven war插件不要从排除的目录中复制,但由于修改后的javascript目录已经存在,war文件仍然包含javascript目录,但在本例中包含修改后的压缩javascript代码。

这是我的解决方案,只需添加一个antrun插件,该插件使用处理后的输出更新打包的war文件,该输出绑定到打包阶段:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>package</id>
                    <phase>package</phase>
                    <configuration>
                        <target>
                            <zip basedir="${project.build.directory}/${project.build.finalName}"
                                destfile="${project.build.directory}/${project.build.finalName}.war"
                                update="true">
                            </zip>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

org.apache.maven.plugins
maven antrun插件
包裹
包裹
跑

那就太晚了,我甚至尝试了打包,创建的war文件没有压缩的javascript代码。如果您在war插件中添加一个阶段指令,并将其设置为
package
install
,同时将压缩阶段设置为
prepare package
?基本上确保在压缩*.js文件后创建war?