Files got overwritten in maven project when building a war

六眼飞鱼酱① 提交于 2019-11-26 17:25:20

问题


I'm building a web application project using maven, and packaging is set to "war". I also use YUI compressor plugin to compress javascript codes in the webapp directory. I've set up the YUI compressor like this:

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

If I do: mvn process-resources, src/main/webapp will get copied over to target/webapp-1.0/ directory, and javacripts are compressed. However, when I run mvn install, all the compressed javascripts are overwritten, apparently the packaging process copies the content from main/webapp one time before building the war file.

How can I get around this?


回答1:


As you noticed, the /src/main/webapp dir (aka warSourceDirectory) contents is not copied into the project dir for packaging until the war plugin executes during the package phase. When the war plugin completes the archive is already built; too late to modify those resources. If the .js files you want to compress were moved into another directory (outside of /src/main/webapp) then you could do something like the below.

To test, I created a ${basedir}/src/play directory with a couple of files in it. I used the resource plugin for the example; you'd replace that config with the YUI compressor plugin config you needed and simply add the <webResource> element to your war plugin config as shown below; more info in the war plugin examples. My war ended up with the additional files right where I wanted them.

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


I think @user944849 answer is the correct answer, at least one of the correct answers. Another way of archiving this is to exclude the modified javascript directory from maven-war-plugin configuration, e.g.:

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

this will tell maven-war-plugin not to copy from the excluded directory, but since the modified javascript directory is already there, the war file still contains the javascript directory, BUT with the modified, in this case, compressed javascript codes.




回答3:


in your execution directive, set the phase for applying your compression and copying to be install and that will hopefully do the trick. the code should be something like this:

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



回答4:


Here is my solution, simply add an antrun plugin which updates the packaged war file using the processed outputs, which binds to the package phase:

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


来源:https://stackoverflow.com/questions/10133485/files-got-overwritten-in-maven-project-when-building-a-war

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!