我正在使用maven-antrun-plugin在maven项目中调用ant build。 ant构建声明了一个taskdef,我需要将taskdef的类路径设置为由与maven-antrun-plugin(即maven.plugin.classpath)相关联的依赖项以及一些其他资源组成。我只能将类路径变为maven.plugin.classpath。
我的pom.xml的片段
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>test</phase>
<configuration>
<target>
<path id="my.classpath">
<pathelement location="src/test/resources" />
<path refid="maven.plugin.classpath" />
</path>
<property name="my_classpath" refid="my.classpath" />
<property name="plugin_classpath" refid="maven.plugin.classpath"/>
<echo message="my classpath: ${my_classpath}"/>
<echo message="plugin classpath: ${plugin_classpath}"/>
<!-- define the taskdef, specifying the classpathref from above -->
<taskdef name="sqlunit" classname="net.sourceforge.sqlunit.ant.SqlunitTask" classpathref="my.classpath"/>
<!-- invoke the tests -->
<sqlunit>
<fileset dir="src/test/db">
<include name="**/*.xml" />
</fileset>
</sqlunit>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
在SQLUnit中,我打印出类路径的URL:
ClassLoader cl = SQLUnit.class.getClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){
System.out.println(url.getFile());
}
执行“mvn test”,我可以看到my.classpath包含src / test / resources:
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
当执行sqlunit任务时,我发现类路径只包含maven.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
我查看了AntRunMojo的源代码,我看到的是构建一个Ant项目并添加maven.plugin.classpath作为项目的引用。
/* set maven.plugin.classpath with plugin dependencies */
antProject.addReference( "maven.plugin.classpath", getPathFromArtifacts( pluginArtifacts, antProject ) );
我认为正在发生的事情是taskdef能够使用添加到项目中的引用来解析其类,因此它忽略了与taskdef元素关联的classpathref。为了证明这个理论,我从taskdef中删除了classpathref属性,
<taskdef name="sqlunit" classname="net.sourceforge.sqlunit.ant.SqlunitTask"/>
任务仍然可以解决它的课程。当类加载器打印出它的资源时,它们就是由maven.plugin.classpath定义的。
===============
我最终更改了SQLUnit的构建,以便log4j.properties文件包含在sqlunit.jar中。现在一切都很开心。