Prasobh.Kollattu
Prasobh.Kollattu

Reputation: 1701

Maven 3 :How to copy a file to test directory , when the file does not exist

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

Upvotes: 2

Views: 3942

Answers (3)

Tom Saleeba
Tom Saleeba

Reputation: 4217

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:

Upvotes: 4

Radio Rogal
Radio Rogal

Reputation: 457

You can use copy-maven-plugin with runIf where check if the file exists.

Upvotes: 1

Saif Asif
Saif Asif

Reputation: 5668

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>

Upvotes: 0

Related Questions