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.
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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With