Files got overwritten in maven project when building a war

浪尽此生 提交于 2019-11-27 16:11:41

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>

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.

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>

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