How to stop Maven from overwriting resource files
Asked Answered
M

3

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
Mantegna answered 13/11, 2015 at 20:2 Comment(2)
Could you post your POM file? Maven should not overwrite the resources if they haven't changed.Laing
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...Touchstone
V
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).

Visor answered 13/11, 2015 at 20:9 Comment(8)
thanks, I tried setting a maven property, no luck. Is that what you mean? <properties> <maven.resources.overwrite>false</maven.resources.overwrite> </properties>Mantegna
@Mantegna 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.Visor
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 againMantegna
@Mantegna 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.Visor
the last modified date does not change, because the file is copied (its not touched)Mantegna
@Mantegna Not true. Did you try switching the property to true to see the difference in the timestamp?Visor
sorry, you are right. changing the flag to true updates the timestampMantegna
@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".Ralfston
R
4

Be aware:

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

Robinia answered 16/5, 2019 at 13:19 Comment(0)
C
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.

Contortionist answered 29/4, 2022 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.