I want to copy a file from src directory to test/resource directory during maven phase test, if and only if file does not exists in test/resource directory.Does any body know how we can achieve this ?Thanks in Advance
Asked
Active
Viewed 3,545 times
3 Answers
4
Here's an updated version of @Saif Asif's answer that is working for me on Maven3:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>test</phase>
<configuration>
<target>
<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="maven.dependency.classpath" />
<if>
<available file="/path/to/your/file "/>
<then>
<!-- Do something with it -->
<copy file="/your/file" tofile="/some/destination/path" />
</then>
</if>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b3</version>
<exclusions>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-nodeps</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
</plugin>
Thanks to https://stackoverflow.com/a/13701310/1410035 for the "adding dependencies to the plugin" solution.
The noteworthy changes in this example are:
- Updated the plugin to version 1.8 (latest at the time of writing)
- Updated the taskdef to have the classpathref as per https://stackoverflow.com/a/4372491/1410035
- Changed the taskdef classpathref to work with maven3 as per https://stackoverflow.com/a/13569218/1410035

Community
- 1
- 1

Tom Saleeba
- 4,031
- 4
- 41
- 36
-
Thank you, thank you, thank you. I wouldn't be able to do this without you. Even after 15y of being professional developer, I still cannot follow maven documentation(a.k.a complete disorganized garbage). The amount of time I wasted there trying to find something super trivial, which should be readily readable there, ... it will be at least weeks. – Martin Mucha Dec 31 '22 at 12:49
1
You can use copy-maven-plugin
with runIf
where check if the file exists.

Radio Rogal
- 462
- 4
- 9
-
Thanks @UR6LAD, This plugin done the job well.File copy is working as expected. – Prasobh.Kollattu Mar 12 '14 at 14:07
-
,Our client is not happy with this plugin... :( .They are saying that the plugin seems to introduce a sort-of procedural "scripting" to maven, which to me feels like just the opposite of what maven distinguishes from e.g. ant.Core logic of plugin i have used is as follows.Please let me know if their are any other alternative.
{{ !new File( "src/test/resources/systemConfig.properties" ).exists() }} src/test/resources development/env/systemConfig.properties
0
Use the Maven AntRun plugin to accomplish this. In your pom.xml, use something like
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>test</phase>
<configuration>
<tasks>
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<if>
<available file="/path/to/your/file "/>
<then>
<!-- Do something with it -->
<copy file="/your/file" tofile="/some/destination/path" />
</then>
</if>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Saif Asif
- 5,516
- 3
- 31
- 48
-
Thanks @Saif for the response.When i tried your code i am getting following exception.. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.6: run (default) on project *****: An Ant BuildException has occured: Problem: failed to create task or type if [ERROR] Cause: The name is undefined. [ERROR] Action: Check the spelling. [ERROR] Action: Check that any custom tasks/types have been declared. [ERROR] Action: Check that any
/ – Prasobh.Kollattu Mar 11 '14 at 11:04declarations have taken pl ace. -
Have you defined any task named as antcontrib.properties ? If not, remove that line – Saif Asif Mar 11 '14 at 12:02
-
AS you mentioned in your code ,
.This is the only enyrty i am having in my pom.xml.I have tired removing this entry from pom not build is executing fine.However copying is not working.Folowing piece of code i have used for file copy and its working fine.However its overriding destination file if their are some chnage in src.this hsoul not happen. -
-
-
Thanks @Saif Asif.My issue is resolved with copy-maven-plugin.Thank you very much for your time.:) – Prasobh.Kollattu Mar 12 '14 at 14:08