Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using antcontrib <if> task via maven-antrun-plugin

My maven java project uses the maven-antrun-plugin to execute a deploy.xml ant script that deploys my app. The deploy.xml uses the <if> task and this seems to be causing the problem;

[INFO] Executing tasks
[taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.

deploy:
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An Ant BuildException has occured: The following error occurred while executing this line:
E:\My_Workspace\xxxxxx\xxxxxx\xxxxxxx\deploy.xml:24: Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

Here is the antrun plugin config from my pom;

<plugin>
    <inherited>false</inherited>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>remote-deploy</id>
            <phase>install</phase>
            <configuration>
                <tasks>
                    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.plugin.classpath"/>

                        <property name="compile_classpath" refid="maven.compile.classpath" />
                        <property name="runtime_classpath" refid="maven.runtime.classpath" />
                        <property name="plugin_classpath" refid="maven.plugin.classpath" />
                                
                        <echo message="compile classpath: ${compile_classpath}"/>
                        <echo message="runtime classpath: ${runtime_classpath}"/>
                        <echo message="plugin classpath: ${plugin_classpath}"/>

                        <ant antfile="${basedir}/deploy.xml">
                            <target name="deploy" />
                        </ant>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-jsch</artifactId>
            <version>1.7.1</version>
        </dependency>
    </dependencies>
</plugin>

.. and here is the relevant section from my deploy.xml;

<target name="deploy" if="deploy">
    <if>    <!-- line 24 -->
        <and>

Why I look in my maven repo I can see ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar and when I look inside the jar I can see net/sf/antcontrib/antcontrib.properties so no problem there.

When I check the values of maven.compile.classpath, maven.compile.classpath and maven.compile.classpath I can't see any reference to antcontrib, could this be the problem? Why don't they appear when antcontrib is defined as a dependancy?

like image 686
Qwerky Avatar asked Jan 18 '11 12:01

Qwerky


People also ask

What is Maven AntRun plugin used for?

This plugin provides the ability to run Ant tasks from within Maven. You can even embed your Ant scripts in the POM! It is not the intention of this plugin to provide a means of polluting the POM, so it's encouraged to move all your Ant tasks to a build.

Can we use Maven and Ant together?

You can use the maven-antrun-plugin to invoke the ant build. Then use the build-helper-maven-plugin to attach the jar produced by ant to the project. The attached artifact will be installed/deployed alongside the pom. If you specify your project with packaging pom , Maven will not conflict with the ant build.

What is ant contrib?

The Ant-Contrib project is a collection of user supplied task (like an <if> task) and a development playground for experimental tasks like a C/C++ compilation task for different compilers. Compatibility: 1.4.1 and above. URL: http://ant-contrib.sourceforge.net/


3 Answers

I think it is not a very good idea to add ant to compile classpath in order to run maven plugin.

I use Maven 3.0.4 and it worked by specifying namespace for ant-contrib tags, for example:

<configuration>
  <target>
    <echo message="The first five letters of the alphabet are:"/>
    <ac:for list="a,b,c,d,e" param="letter" xmlns:ac="antlib:net.sf.antcontrib">
      <sequential>
        <echo>Letter @{letter}</echo>
      </sequential>
    </ac:for>
  </target>
</configuration>

My maven-antrun-plugin dependencies:

<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>
like image 90
Vasyl Shchukin Avatar answered Oct 18 '22 02:10

Vasyl Shchukin


I found that you need to include the ant-contrib dependency inside the plugin which will enable the taskdef tag to find antcontrib.properties

  <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>ant-contrib</groupId>
                    <artifactId>ant-contrib</artifactId>
                    <version>20020829</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>copy-and-rename-template-files</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target name = "copy-and-rename-template-files">
                            <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                            <if>
                                <available file="src/main/resources/docker/templates" type="dir"/>
                                <then>
                                    <copy todir="${project.build.directory}/docker">
                                        <fileset dir="src/main/resources/docker/templates">
                                            <include name="**/*"/>
                                        </fileset>
                                    </copy>


                                    <move todir="${project.build.directory}/docker">
                                        <fileset dir="${project.build.directory}/docker">
                                            <include name="*"/>
                                        </fileset>
                                        <mapper>
                                            <regexpmapper from="(.*)project(.*)" to="\1${project.artifactId}\2"/>
                                        </mapper>
                                    </move>
                                </then>

                                <else>
                                    <echo>src/main/resources/docker/templates does not exist, skipping processing docker templates</echo>
                                </else>
                            </if>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
like image 38
Gapmeister66 Avatar answered Oct 18 '22 04:10

Gapmeister66


OK, I've solved it.

Moving the dependencies out of the <build><plugin> tag and putting them in with the other project dependencies seems to have done the trick.

like image 34
Qwerky Avatar answered Oct 18 '22 04:10

Qwerky