Search code examples
antmaven

adding file exists conditions to maven-antrun plugin


I want to do a "javac" inside an antrun plugin based on the availability of a file. How do we add conditions inside the maven-antrun plugin.


Solution

  • You can do this with help of Maven AntRun Plugin.

    In the sample ant script executes on clean phase and Ant-Contrib library was used:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>clean</phase>
                        <configuration>
                            <tasks>
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties" />
    
                                <if>
                                    <available file="d:\file_to_check.txt"/>
                                    <then>
                                        <echo>The file exists</echo>
                                    </then>
    
                                    <else>
                                        <echo>The file does not exist</echo>
                                    </else>
                                </if>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>ant-contrib</groupId>
                        <artifactId>ant-contrib</artifactId>
                        <version>20020829</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    

    You can also view this link: How to execute tasks conditionally using the maven-antrun-plugin?