7

I'm trying to use "if" ant tasks within maven build.

I found many articles that suggest using "ant-nodeps" dependency. Eventually all this tricks did not work on maven3 + ant 1.8.1 + maven-antrun-plugin 1.6.

"An Ant BuildException has occured: Problem: failed to create task or type if"

Can anything help?

Here's real code (rather, it is not necessary, but just in case):

 <profiles>
    <profile>
        <id>smtpConfigurationProfile</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.6</version>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <tasks>
                                    <if>
                                        <isset property="${smtpFile}"/>
                                        <then>
                                            <delete file="${project.build.outputDirectory}/smtp.properties"/>
                                            <copy file="${smtpFile}"
                                                  tofile="${project.build.outputDirectory}/smtp.properties"/>
                                        </then>
                                        <elseif>
                                            <isset property="${smtpProfile}"/>
                                            <then>
                                                <delete file="${project.build.outputDirectory}/smtp.properties"/>
                                                <copy file="src/main/resources/${smtpProfile}.smtp.properties"
                                                      tofile="${project.build.outputDirectory}/smtp.properties"/>
                                            </then>
                                            <else>
                                                <delete file="${project.build.outputDirectory}/smtp.properties"/>
                                                <copy file="src/main/resources/production.smtp.properties"
                                                      tofile="${project.build.outputDirectory}/smtp.properties"/>
                                            </else>
                                        </elseif>
                                    </if>
                                </tasks>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.apache.ant</groupId>
                            <artifactId>ant-nodeps</artifactId>
                            <version>1.8.1</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
Oleg Chirukhin
  • 970
  • 2
  • 7
  • 22

2 Answers2

17

1) Add this line before ant tasks in target section:

<taskdef resource="net/sf/antcontrib/antlib.xml" 
 classpathref="maven.plugin.classpath" />

2) Add exactly the following dependencies to plugin:

                        <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>
Oleg Chirukhin
  • 970
  • 2
  • 7
  • 22
  • [taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found. – Sunil Garg Aug 30 '17 at 09:45
  • @sunilGarg Did you include the dependencies in the plugin or in the project's `dependencies` tag? I had to include it in the plugin i.e. https://stackoverflow.com/a/43853892/1062833 – Dan Mar 28 '18 at 20:48
2

See my question here where I had the same problem.

I solved it by moving my ant-contrib dependency from the plugin to the project.

Community
  • 1
  • 1
Qwerky
  • 18,217
  • 6
  • 44
  • 80