2010-10-14 138 views
6

我试图使用Ant任务上传文件。如果我使用Ant直接的文件上传,但如果我通过的Maven调用Ant任务(使用maven-antrun-plugin)我收到以下错误:通过Maven中的Ant FTP任务上传文件

的蚂蚁BuildException已发生:在执行这条线出现以下错误:

/home/me/proj/build.xml:15: Problem: failed to create task or type ftp 
Cause: the class org.apache.tools.ant.taskdefs.optional.net.FTP was not found. 
    This looks like one of Ant's optional components. 
Action: Check that the appropriate optional JAR exists in 
    -ANT_HOME/lib 

蚂蚁commonsnet.jar显然是可供Ant:

$ ls $ANT_HOME/lib | grep ant-commons-net 
ant-commons-net.jar 

是对Maven的antrun-插件单独定义的类路径蚂蚁,还是我失去了一些东西?

回答

4

ant-commons-net.jar is clearly available to Ant

是的,但Maven和该maven-antrun-plugin没有使用你的本地蚂蚁安装。

Is the Ant classpath defined separately for maven-antrun-plugin , or am I missing something?

使用不包含在Ant的默认罐子Ant任务的方式Using tasks not included in Ant's default jar是记录(这绝对应该帮助):

To use Ant tasks not included in the Ant jar, like Ant optional or custom tasks you need to add the dependencies needed for the task to run to the plugin classpath and use the maven.plugin.classpath reference if needed.

<project> 
    <modelVersion>4.0.0</modelVersion> 
    <artifactId>my-test-app</artifactId> 
    <groupId>my-test-group</groupId> 
    <version>1.0-SNAPSHOT</version> 

    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-antrun-plugin</artifactId> 
     <version>1.6</version> 
     <executions> 
      <execution> 
      <id>ftp</id> 
      <phase>deploy</phase> 
      <configuration> 
       <target> 

       <ftp action="send" server="myhost" remotedir="/home/test" userid="x" password="y" depends="yes" verbose="yes"> 
        <fileset dir="${project.build.directory}"> 
        <include name="*.jar" /> 
        </fileset> 
       </ftp> 

       <taskdef name="myTask" classname="com.acme.MyTask" classpathref="maven.plugin.classpath"/> 
       <myTask a="b"/> 

       </target> 
      </configuration> 
      <goals> 
       <goal>run</goal> 
      </goals> 
      </execution> 
     </executions> 
     <dependencies> 
      <dependency> 
      <groupId>commons-net</groupId> 
      <artifactId>commons-net</artifactId> 
      <version>1.4.1</version> 
      </dependency> 
      <dependency> 
      <groupId>ant</groupId> 
      <artifactId>ant-commons-net</artifactId> 
      <version>1.6.5</version> 
      </dependency> 
      <dependency> 
      <groupId>ant</groupId> 
      <artifactId>ant-nodeps</artifactId> 
      <version>1.6.5</version> 
      </dependency> 
     </dependencies> 
     </plugin> 
    </plugins> 
    </build> 
</project> 
+0

这是正确的做法。我建议做的唯一不同的事情是:1)将'ant' groupId指定为'org.apache.ant',因为这是Maven插件在内部引用的内容。 – 2010-10-14 22:26:30

+1

如果这是一个多模块项目,您还应该考虑将依赖关系添加到根项目pom中的pluginManagement部分。这将阻止其他引用到项目中的转义,从而破坏您的依赖关系。 – 2010-10-14 22:33:23

+0

@Tim事实上,'ant-commons-net'的[版本1.7.0](http://mvnrepository.com/artifact/org.apache.ant/ant-commons-net),'groupId'是'org.apache.ant',但上面的版本对于版本1.6.5是正确的。换句话说,如果你想使用更新的版本,就可以适应它。你当然对'pluginManagement'部分是正确的。我会更新我的答案,提及...明天:)感谢您的评论! – 2010-10-14 23:16:02

0

有一个classpath财产可以在<tasks>maven-antrun-plugin部分设置。

例如,

<property name="classpath" refid="maven.compile.classpath"/> 
1

帕斯卡已经提到的,Maven的antrun-插件没有使用您的$ ANT_HOME环境变量指定的ant,并且他提到的配置可能是从纯粹的maven视角持续执行此操作的最佳方式。

然而,罐子可以储存在$ USER_HOME /赵军阳张志利/ lib目录,而不是$ ANT_HOME/lib目录,这些罐子应该可以在类路径是由该用户运行蚂蚁的任何实例。

注意你的Ant脚本不能假设罐子都存在,而且罐子只放在在启动类路径,因此,如果脚本定义的设置目标下载罐子到$ USER_HOME /赵军阳张志利/ lib,那么这个目标必须在“分离 - 蚂蚁会话”中运行,并且再次被调用来执行依赖于jar的任务。

您可能从这种方法中获得的唯一潜在好处是Ant脚本可以从maven和Ant运行。