Maven copy local file to remote server using SSH

Can Maven copy local file to a remote server using SSH?

I want to specify location in maven configuration file and to copy that file (or files) to server each time deploy phase is executed.


The maven-deploy-plugin allows you to configure the deploy phase to deploy to a server using scp. There is a page in the documentation that describes how it can be done.

I believe this will replace the normal deployment instead of add to it, so it may not be what you're after.

If you need to deploy to a traditional Maven repository as well as deliver the file to the remote server, you will need to use the scp task as the other answers suggest.

In this answer I've described how to configure the ftp task, the scp task is almost identical except you may need to add the keyfile and passphrase attributes (and change the task name from ftp to scp obviously).


Have a look at Maven Wagon plugin

To try it manually with a simple command line: mvn org.codehaus.mojo:wagon-maven-plugin:1.0:upload -Dwagon.url=scp://username:userpassword@myserver -Dwagon.fromDir=target -Dwagon.includes=*.ear -Dwagon.toDir=/home/elisabetta

In both cases, be sure to add the SSH extension for Wagon to your pom.xml:

<extensions>
    <extension>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-ssh</artifactId>
        <version>2.8</version>
    </extension>
</extensions>

Why not use the Ant SCP task, which you can run within Maven?


Same idea as PaoloC, using Maven Wagon plugin with the wagon-ssh extension, but configuration in pom file and run it on specified phase, this examples copies the war file to a remote server with SSH:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>wagon-maven-plugin</artifactId>
      <version>1.0</version>
      <executions>
        <execution>
          <id>upload-to-myserver</id>
          <phase>deploy</phase>
          <goals>
            <goal>upload-single</goal>
          </goals>
          <configuration>
            <fromFile>${project.build.directory}/${project.build.finalName}.war</fromFile>
            <url>scp://username@myserver/path</url>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <!-- other plugins ... -->
  </plugins>
  <extensions>
    <extension>
      <groupId>org.apache.maven.wagon</groupId>
      <artifactId>wagon-ssh</artifactId>
      <version>2.8</version>
    </extension>
  </extensions>
</build>

The <phase> tag is optional. You can run just the upload execution with the command:

mvn wagon:upload-single@upload-to-myserver

Maven is not a generic tool, it's a tool to make your build process reusable. I suggest to use an embedded antrun build step. In this step, you can do anything using the normal ant syntax which you'd use in build.xml.