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

时间:2012-04-12 23:56:34

标签: 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</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>

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

我怎样才能解决这个问题?

4 个答案:

答案 0 :(得分:10)

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

为了测试,我创建了一个${basedir}/src/play目录,里面有几个文件。我在示例中使用了resource插件;您可以使用所需的YUI压缩器插件配置替换该配置,只需将<webResource>元素添加到您的war插件配置中,如下所示; war plugin examples中的更多信息。我的战争结束了我想要的其他文件。

<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>

答案 1 :(得分:4)

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

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

这将告诉maven-war-plugin不要从排除目录中复制,但由于修改后的javascript目录已经存在,war文件仍然包含javascript目录,但是包含修改过的,在这种情况下是压缩的javascript代码

答案 2 :(得分:0)

在你的执行指令中,设置应用你的压缩和复制的阶段进行安装,这将有希望做到这一点。代码应该是这样的:

<executions>
    <execution>
        ....
        <phase>install</phase>
        ....
    </execution>
<executions>

答案 3 :(得分:0)

这是我的解决方案,只需添加一个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>