Glory to Russia
Glory to Russia

Reputation: 18712

How to execute an FTP upload task after assembly:single in Maven?

I'm using the pom.xml file shown below to build a certain JAR file (myproduct-1.0-SNAPSHOT-jar-with-dependencies.jar) using mvn assembly:single.

I want to be able to upload that file via FTP as described in another question.

I added the following code part in order to be able to do the upload using 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>

I get the error 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].

How can I upload the file generated by mvn assembly:single via FTP (either using a separate call, or right after the myproduct-1.0-SNAPSHOT-jar-with-dependencies.jar has been generated) ?

Here's the pom.xml file:

<?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>

Upvotes: 1

Views: 3318

Answers (1)

Tunaki
Tunaki

Reputation: 137084

You can do this using wagon-maven-plugin and its upload-single goal.

In your Maven settings, you need to add a server declaration, which will declare the credentials to connect to your FTP server:

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

And then you need to configure the plugin to use this server to upload the file. Wagon works with several providers that are added through extensions: in this case we need to add wagon-ftp provider.

<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>

In this example, the Wagone execution is bound to the install phase so to run it, you need to launch Maven with mvn clean install. You may want to bind it to another phase depending on your requirement.

Upvotes: 4

Related Questions