Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding file exists conditions to maven-antrun plugin

Tags:

maven

ant

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.

like image 748
Kasun Indrasiri Avatar asked Feb 23 '23 17:02

Kasun Indrasiri


2 Answers

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?

like image 83
Alex K Avatar answered Mar 03 '23 12:03

Alex K


Here is an other approach without Ant contrib.

The trick is to first run a target checking for file existence. This target will store its checking result in a property. This property is then exported among Maven properties thanks to maven-antrun-plugin option exportAntProperties.

A second target can then run based on the exported property value.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>check-file-exists</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <available 
                                file="path/to/file-to-check"
                                property="fileExists"
                            />
                        </target>
                        <exportAntProperties>true</exportAntProperties>
                    </configuration>
                </execution>

                <execution>
                    <id>perform-actual-task</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target if="${fileExists}">
                            <echo message="File exists... let's go !" />
                            <!-- Your tasks here ... -->
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
like image 24
Stephan Avatar answered Mar 03 '23 11:03

Stephan