-->

Is there a way to force maven to copy resource fol

2020-07-05 18:30发布

问题:

I'm using Maven 2.2.1 and m2eclipse.

I have two resource folders.

When I save a change to any file in any of the resource folders, the Maven incremental build kicks off and re-copies ALL files in both resource folders to the target folders.

This behavior would be fine if there were a relatively small number of files in the resource folders - but there are enough that the copy can take several minutes.

Is there a way to force maven to be more selective in its incremental build and copy only those resources that were changed?

回答1:

I chased the problem down to org.codehaus.plexus.util.FileUtils.copyFile() method. It is this method that is called by the maven-resource-plugin to ultimately copy the resource; the copyFile() method takes an 'overwrite' parameter, which the resource-plugin does pass in (and the default is indeed false), BUT...

The copyFile() method ignores the 'overwrite' parameter if the list of filter-wrappers passed is non-empty! And if you have filtering set to true for your resources, this list is indeed non-empty.

I can understand the reasoning behind copyFile() ignoring the 'overwrite': just because the destination file is newer does not mean that new filtered-file will be the same (i.e. the values for the variables in your resource file may have been changed since the last filtering).
Ignoring the 'overwrite' flag is "convenient" for the FileUtils implementor. But this comes at a great price; a single resource file unnecessarily updated can trigger off time-consuming but redundant processes (i.e. rebuilding a jar-with-dependencies in my case). This may only be a few seconds but can be enough to disrupt the flow of an intensive code-compile-test cycle.

I looked for open bug on FileUtils but did not find any. This was bugging me so I had to chase it down but I can't spend more time on it right now... in few days I would like to file a bug report (may just be quicker to implement the right solution); if anyone can post links to the appropriate bug-tracking/reporting system I would appreciate it.



回答2:

You can try updating to the latest version of m2eclipse and Maven 3. There is a new Maven plugin API that tells m2eclipse what files been affected by the Maven plugin. I believe resource plugin been updated to that API (but make sure overwrite setting is NOT set to true in resource plugin configuration).



回答3:

Try setting the overwrite parameter of the Maven resources plugin to false. http://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html#overwrite

Edited: this parameter exists since Maven resources plugin version 2.3. @Jared: Please check your version.



回答4:

There might be an option in the Maven resource plugin configuration to do that but I have not found any. If I were you I would split up the resources and move them out into separate modules that you depend on. Then you just rebuild the module that you changed files in and not the others and things will be much faster..

I would also try Maven 3.0 that was just released. It is a lot faster and will improve your situation in this and other aspects as well.



回答5:

Assuming you don't want to update, it appears that you'd need to write your own version of the resources plugin.

This doesn't look like it would be too hard (some incomplete source code is here: http://maven.apache.org/plugins/maven-resources-plugin/xref/index.html) and you could include your own custom configuration parameter to modify the overwrite behaviour of the MavenResourcesExecution class.

If you were feeling community minded, you could submit the patch back into the plugin repository to allow others to take advantage of the update.