title: How to override classpath for taskdef when using maven-antrun-plugin
body: <p>I'm invoking an ant build from within a maven project using the maven-antrun-plugin.  The ant build declares a taskdef and I need to set the classpath of the taskdef to be composed of the dependencies associated with the maven-antrun-plugin (i.e. maven.plugin.classpath) as well as some additional resources.  I can only get the classpath to be maven.plugin.classpath.</p>
  
  <p>A snippet of my pom.xml</p>
  
  <pre><code>            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-antrun-plugin&lt;/artifactId&gt;
              &lt;version&gt;1.8&lt;/version&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;phase&gt;test&lt;/phase&gt;
                      &lt;configuration&gt;
                          &lt;target&gt;
  
                              &lt;path id="my.classpath"&gt;
                                  &lt;pathelement location="src/test/resources" /&gt;
                                  &lt;path refid="maven.plugin.classpath" /&gt;
                              &lt;/path&gt;
  
                              &lt;property name="my_classpath" refid="my.classpath" /&gt;
                              &lt;property name="plugin_classpath" refid="maven.plugin.classpath"/&gt;
  
                              &lt;echo message="my classpath:  ${my_classpath}"/&gt;
                              &lt;echo message="plugin classpath:  ${plugin_classpath}"/&gt; 
  
                              &lt;!-- define the taskdef, specifying the classpathref from above --&gt;
                              &lt;taskdef name="sqlunit" classname="net.sourceforge.sqlunit.ant.SqlunitTask" classpathref="my.classpath"/&gt;
  
                               &lt;!-- invoke the tests --&gt;
                              &lt;sqlunit&gt;
                                &lt;fileset dir="src/test/db"&gt;
                                  &lt;include name="**/*.xml" /&gt;
                                &lt;/fileset&gt;
                              &lt;/sqlunit&gt;                                
                          &lt;/target&gt;
                      &lt;/configuration&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;run&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
  </code></pre>
  
  <p>Inside of SQLUnit, I'm printing out the urls of the classpath:</p>
  
  <pre><code>    ClassLoader cl = SQLUnit.class.getClassLoader();
  
      URL[] urls = ((URLClassLoader)cl).getURLs();
  
      for(URL url: urls){
          System.out.println(url.getFile());
      }
  </code></pre>
  
  <p>Executing "mvn test", I can see the my.classpath includes src/test/resources:</p>
  
  <pre><code>main:
  [echo] my classpath:  /Users/szalwinb/playpen/bszalwin/DbDmsApp/src/test/resources:/Users/szalwinb/.m2/repository/org/apache/maven/plugins/maven-antrun-plugin/1.8/maven-antrun-plugin-1.8.jar:/Users/szalwinb/.m2/repository/net/sourceforge/sqlunit/5.0/sqlunit-5.0.jar:/Users/szalwinb/.m2/repository/ant-contrib/ant-contrib/20020829/ant-contrib-20020829.jar:/Users/szalwinb/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/szalwinb/.m2/repository/org/jdom/jdom/1.1.3/jdom-1.1.3.jar:/Users/szalwinb/.m2/repository/postgresql/postgresql/8.4-701.jdbc4/postgresql-8.4-701.jdbc4.jar:/Users/szalwinb/.m2/repository/backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar:/Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.11/plexus-interpolation-1.11.jar:/Users/szalwinb/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar:/Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar:/Users/szalwinb/.m2/repository/org/apache/ant/ant/1.9.4/ant-1.9.4.jar:/Users/szalwinb/.m2/repository/org/apache/ant/ant-launcher/1.9.4/ant-launcher-1.9.4.jar
  [echo] plugin classpath:  /Users/szalwinb/.m2/repository/org/apache/maven/plugins/maven-antrun-plugin/1.8/maven-antrun-plugin-1.8.jar:/Users/szalwinb/.m2/repository/net/sourceforge/sqlunit/5.0/sqlunit-5.0.jar:/Users/szalwinb/.m2/repository/ant-contrib/ant-contrib/20020829/ant-contrib-20020829.jar:/Users/szalwinb/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/szalwinb/.m2/repository/org/jdom/jdom/1.1.3/jdom-1.1.3.jar:/Users/szalwinb/.m2/repository/postgresql/postgresql/8.4-701.jdbc4/postgresql-8.4-701.jdbc4.jar:/Users/szalwinb/.m2/repository/backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar:/Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.11/plexus-interpolation-1.11.jar:/Users/szalwinb/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar:/Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar:/Users/szalwinb/.m2/repository/org/apache/ant/ant/1.9.4/ant-1.9.4.jar:/Users/szalwinb/.m2/repository/org/apache/ant/ant-launcher/1.9.4/ant-launcher-1.9.4.jar
  </code></pre>
  
  <p>And when sqlunit task is executed, I see that the classpath only contains those artifacts found in maven.plugin.classpath.</p>
  
  <pre><code>/Users/szalwinb/.m2/repository/org/apache/maven/plugins/maven-antrun-plugin/1.8/maven-antrun-plugin-1.8.jar
  /Users/szalwinb/.m2/repository/net/sourceforge/sqlunit/5.0/sqlunit-5.0.jar
  /Users/szalwinb/.m2/repository/ant-contrib/ant-contrib/20020829/ant-contrib-20020829.jar
  /Users/szalwinb/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar
  /Users/szalwinb/.m2/repository/org/jdom/jdom/1.1.3/jdom-1.1.3.jar
  /Users/szalwinb/.m2/repository/postgresql/postgresql/8.4-701.jdbc4/postgresql-8.4-701.jdbc4.jar
  /Users/szalwinb/.m2/repository/backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar
  /Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.11/plexus-interpolation-1.11.jar
  /Users/szalwinb/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
  /Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar
  /Users/szalwinb/.m2/repository/org/apache/ant/ant/1.9.4/ant-1.9.4.jar
  /Users/szalwinb/.m2/repository/org/apache/ant/ant-launcher/1.9.4/ant-launcher-1.9.4.jar
  </code></pre>
  
  <p>I looked at the source to AntRunMojo, and I see that is is building an Ant Project and adds maven.plugin.classpath as a reference to the project.  </p>
  
  <pre><code>        /* set maven.plugin.classpath with plugin dependencies */
          antProject.addReference( "maven.plugin.classpath", getPathFromArtifacts( pluginArtifacts, antProject ) );
  </code></pre>
  
  <p>I think what is happening is that the taskdef is able to resolve its class using the references added to the project, so it ignores the classpathref associated with the taskdef element.  To prove out this theory, I removed the classpathref attribute from the taskdef,</p>
  
  <pre><code> &lt;taskdef name="sqlunit" classname="net.sourceforge.sqlunit.ant.SqlunitTask"/&gt;
  </code></pre>
  
  <p>And the task was still able to resolve its class.  And when the classloader printed out its resources, they were the ones defined by maven.plugin.classpath.</p>
  
  <p>===============</p>
  
  <p>I ended up changing the build for SQLUnit so that the log4j.properties file is included in the sqlunit.jar.  Everything is happy now.</p>