6

I have the default maven structure:

main
--java
--resources
--webapp

I see that every mvn compile copies resources even though they were not changed. What should I do to make build copy only changed files?

<build>
         <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>
    <dependencies>
        ...
    </dependencies>

    <properties>
        <maven.resources.overwrite>false</maven.resources.overwrite>
    </properties>

Here is the output in debug mode:

[INFO] Using 'cp1252' encoding to copy filtered resources.
[DEBUG] resource with targetPath null
directory C:\core\src\main\resources
excludes []
includes []
[DEBUG] ignoreDelta true
[INFO] Copying 190 resources
[DEBUG] file batch.bat has a filtered file extension
[DEBUG] copy C:\core\src\main\resources\batch.bat to C:\core\target\classes\batch.bat
[DEBUG] file DataObject.hbm.xml has a filtered file extension
[DEBUG] copy C:\core\src\main\resources\com\data\DataObject.hbm.xml to C:\core\target\classes\com\data\DataObject.hbm.xml
lili
  • 1,866
  • 8
  • 29
  • 50
  • Could you post your POM file? Maven should not overwrite the resources if they haven't changed. – Tunaki Nov 13 '15 at 20:26
  • Have you solved this problem? I'm suffering from same problem. maven resources plugin always copies my src/main/resources/application.properties. overwrite=false did not work. It results in creating new jar file again, so new docker image created, and so docker service has to pull new image, even if there is no change... – gypark Apr 17 '18 at 00:57

3 Answers3

4

Use the property -Dmaven.resources.overwrite=false on the Maven command. See the overwrite parameter of the resources:resources goal.

However the documentation mentions this is the default behavior so check if this parameter is set to true somewhere in your project configuration.

EDIT:

As mentioned in the comments, it seems that even though the log indicates copying, in fact the files are not being changed (the timestamps remain the same when maven.resources.overwrite is false).

M A
  • 71,713
  • 13
  • 134
  • 174
  • thanks, I tried setting a maven property, no luck. Is that what you mean? false – lili Nov 13 '15 at 21:09
  • 2
    @lili Yes you can do this or just `-Dmaven.resources.overwrite=false` via the command line. Did you check if the file was actually modified (with a newer last modified timestamp)? Maybe the debug output is not significant here. – M A Nov 13 '15 at 21:32
  • thats an interesting idea. how can I see if its copying the file or just writing the log? If I delete one of the files, it appears in the target folder again – lili Nov 13 '15 at 21:37
  • @lili Don't delete the file: of course in this case it will copy it again. Just keep it and see on the second run if the last modified time of the file is changed. Use the `dir` command on Windows or `ll` on Unix. – M A Nov 13 '15 at 21:42
  • the last modified date does not change, because the file is copied (its not touched) – lili Nov 13 '15 at 21:45
  • @lili Not true. Did you try switching the property to true to see the difference in the timestamp? – M A Nov 13 '15 at 21:48
  • sorry, you are right. changing the flag to true updates the timestamp – lili Nov 13 '15 at 21:59
  • 1
    @manouti Thanks! Although the answer is old, you might want to modify it so that it contains the information from your comments. Especially the point that the files are not actually copied even if the plugin says "Copying X resources". – tomorrow Oct 17 '18 at 21:41
4

Be aware:

The maven-resources-plugin ignores the overwrite flag if filtering resources is activated.

Sven Döring
  • 3,927
  • 2
  • 14
  • 17
1

using version 3.2.0 I had to add the following to make it work:

<configuration>
  <useBuildFilters>false</useBuildFilters>
  <overwrite>false</overwrite>
  ...
</configuration>

see also maven-resources-plugin docu

likely background:

since filtering is enabled by default, it may not know if the potentially contained properties for filtering have changed and thus overwrites (ignores the implicit overwrite:false.
disabling the filtering is thus needed and additionally with the version above I had to explicitely turn overwrite off. :-/
inconsistent docu, I would say.

Andreas Covidiot
  • 4,286
  • 5
  • 51
  • 96