【问题标题】:Files got overwritten in maven project when building a war构建战争时文件在maven项目中被覆盖
【发布时间】:2012-04-12 23:56:33
【问题描述】:

我正在使用 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 时,所有压缩的 javascript 都会被覆盖,显然打包过程会在构建 war 文件之前从 main/webapp 复制一次内容。

我该如何解决这个问题?

【问题讨论】:

    标签: maven yui-compressor maven-war-plugin


    【解决方案1】:

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

    为了测试,我创建了一个${basedir}/src/play 目录,其中包含几个文件。我以resource 插件为例;您可以将该配置替换为您需要的 YUI 压缩器插件配置,然后将 &lt;webResource&gt; 元素添加到您的 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>
    

    【讨论】:

      【解决方案2】:

      我认为@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 代码.

      【讨论】:

        【解决方案3】:

        在您的执行指令中,将应用压缩和复制的阶段设置为安装,这有望解决问题。代码应该是这样的:

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

        【讨论】:

        • 来不及了,我什至试过package,创建的war文件没有压缩的javascript代码。
        • 如果你在你的战争插件中添加一个相位指令并将它设置为packageinstall,同时将你的压缩相位设置为prepare-package会怎样?基本上确保在压缩 *.js 文件后发生战争?
        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 2015-04-10
          • 2011-08-03
          • 2012-06-17
          • 2013-02-13
          • 2023-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多