2015-11-26 82 views
1

我正在使用下面顯示的pom.xml文件使用mvn assembly:single構建特定的JAR文件(myproduct-1.0-SNAPSHOT-jar-with-dependencies.jar)。如何在彙編後執行FTP上傳任務:在Maven中單個執行?

我希望能夠通過FTP上傳該文件,如another question中所述。

添加以下代碼部分,以便能夠做到使用mvn install:install上傳:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.7</version> 
    <executions> 
     <execution> 
      <phase>install</phase> 
      <goals> 
       <goal>install</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <target> 
      <property name="file_name" value="myproduct-1.0-SNAPSHOT-jar-with-dependencies.jar" /> 
      <ant antfile="${basedir}/build.xml"> 
       <target name="upload" /> 
      </ant> 
     </target> 
    </configuration> 
</plugin> 

我得到的錯誤Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.3.1:install (default-cli) on project ...: The packaging for this project did not assign a file to the build artifact -> [Help 1]

如何通過FTP上傳由mvn assembly:single生成的文件(或者使用單獨的調用,或者在生成myproduct-1.0-SNAPSHOT-jar-with-dependencies.jar之後)?

這裏的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.mycompany</groupId> 
    <artifactId>myproduct</artifactId> 
    <version>1.0-SNAPSHOT</version> 

    <repositories> 
     <repository> 
      <id>bukkit-repo</id> 
      <url>http://repo.bukkit.org/content/groups/public/</url> 
     </repository> 
     <repository> 
      <id>maven-restlet</id> 
      <name>Public online Restlet repository</name> 
      <url>http://maven.restlet.com</url> 
     </repository> 
    </repositories> 
    <dependencies> 
     [...] 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <configuration> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-antrun-plugin</artifactId> 
       <version>1.7</version> 
       <executions> 
        <execution> 
         <phase>install</phase> 
         <goals> 
          <goal>install</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <target> 
         <property name="file_name" value="myproduct-1.0-SNAPSHOT-jar-with-dependencies.jar" /> 
         <ant antfile="${basedir}/build.xml"> 
          <target name="upload" /> 
         </ant> 
        </target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

回答

1

你可以做到這一點使用wagon-maven-plugin及其upload-single目標。

在你的Maven的設置,你需要添加一個server declaration,這將申報憑據連接到您的FTP服務器:

<server> 
    <id>my-server-id</id> 
    <username>my-user-name</username> 
    <password>my-password</password> 
</server> 

然後你需要配置插件使用此服務器上傳文件。 Wagon與多個通過擴展添加的提供商合作:在這種情況下,我們需要添加wagon-ftp提供商。

<project> 
    [...] 
    <build> 
    [...] 
    <extensions> 
     <extension> 
     <groupId>org.apache.maven.wagon</groupId> 
     <artifactId>wagon-ftp</artifactId> 
     <version>2.10</version> 
     </extension> 
    </extensions> 
    [...] 
    <plugins> 
    <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>wagon-maven-plugin</artifactId> 
     <version>1.0</version> 
     <executions> 
     <execution> 
      <id>upload-assembly</id> 
      <phase>install</phase> 
      <goals> 
      <goal>upload-single</goal> 
      </goals> 
      <configuration> 
      <serverId>my-server-id</serverId> <!-- references the server declaration --> 
      <fromFile>${project.build.directory}/${project.build.finalName}-${project.version}-jar-with-dependencies.jar</fromFile> 
      <url>ftp://your.remote.host/</url> 
      <toFile>remote.file</toFile> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
    [...] 
</project> 

在這個例子中,Wagone執行被綁定到install相,從而來運行它,你需要用mvn clean install推出的Maven。您可能希望根據您的要求將其綁定到另一個階段。

+0

@DmitriPisarenko對不起,'fromDir'需要'fromFile'。你爲「url」指定了什麼? – Tunaki

+0

@DmitriPisarenko它應該是'toFile',而不是'toDir'。 – Tunaki

+0

@DmitriPisarenko您是否修改了Maven設置?你如何運行Maven? – Tunaki