2010-10-14 64 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運行。