Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Resources Plugin: how to copy resources to target folder and keep time stamps?

Tags:

java

maven

When i execute a clean & build on a project, Maven copies all resources to the target folder but with the time stamp of the creation, not with the time stamps of the original files in the src/main/resources folder. Is it possible to somehow instruct it to keep the original time stamps?

The cause why this is problematic for us is that the software we are developing has a database migration in startup time and we like to keep it executing on developer machines. But because of the ever changing time stamps this introduce unnecessary resource reloads to the database.

like image 576
NagyI Avatar asked Jan 24 '13 14:01

NagyI


People also ask

How do I copy files from one directory to another in Maven?

Using the Copy Rename Maven Plugin The copy-rename-maven-plugin helps copying files or renaming files/directories during the Maven build lifecycle. Upon building the project, we'll see foo. txt in the target/destination-folder.

What is Maven resource filtering?

You can use Maven to perform variable replacement on project resources. When resource filtering is activated, Maven will scan resources for property references surrounded by ${ and }.

Which plugin is used to copy filter include and exclude non Java files into your final project?

Apache Maven Resources Plugin – Including and excluding files and directories.


2 Answers

I don't think it is possible using default functionality of Maven resources plugin.

Can you change the path where the files are loaded from at runtime (probably by making it configurable per environment)? If so, then perhaps you can use ant with maven and use ant's copy task which has a preservelastmodified property.

The intent being that Maven's resources handling would continue to work as usual, but the files would also be copied every build to a separate location used by the runtime.

like image 193
kaliatech Avatar answered Nov 15 '22 01:11

kaliatech


Solution is to use ant's copy task. This is done in 2 steps: 1) exlude from the normal processing that files for which we would like to preserve the date (in this example I process test-resources. The same applies for build resources):

<testResources>
    <testResource>
        <directory>src/test/resources</directory>
        <excludes>
            <exclude>**/some-files*/**</exclude>
        </excludes>
    </testResource>
</testResources>

2) the second step is to copy them with ant task using the mave ant plugin:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <phase>process-test-resources</phase>
            <configuration>
                <tasks>
                    <copy todir="${project.build.testOutputDirectory}" preservelastmodified="true">
                        <fileset dir="src/test/resources">
                            <include name="**/some-file*/**"/>
                        </fileset>
                    </copy>                            
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
like image 30
lvr123 Avatar answered Nov 15 '22 00:11

lvr123