Maven copy local file to remote server using SSH

Maven 2SshDeployment

Maven 2 Problem Overview


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.

Maven 2 Solutions


Solution 1 - Maven 2

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

Solution 2 - Maven 2

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>

Solution 3 - Maven 2

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

Solution 4 - Maven 2

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

Solution 5 - Maven 2

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.

Solution 6 - Maven 2

Although this question is not exactly new, I found myself in a similar situation today. My goal is to upload files and run commands on a remote server to which I have to tunnel (through another server). I managed to forge a solution for that with ant (which again can be triggered from maven as mentioned here).

Ants sshsession task only creates a tunnel that you can use for the tasks within. The tasks within are not automatically run on the remote server but you can use the sshexec task together with the tunnel to achieve that. Also the scp task can now upload through the tunnel to the remote server. Here is an example:

<sshsession host="${jumphost}" port="22" username="${user}" password="${password}" trust="true">
	<localtunnel lport="${localTunnelPort}" rhost="${targethost}" rport="22"/>
	<sequential>
		<!-- run a command on the remote server (here mkdir) -->
		<sshexec host="localhost" port="${localTunnelPort}" username="${user.param}" password="${password.param}" command="mkdir ${home}/foobar" trust="true" />
		<!-- upload a file to the remote server -->
		<scp port="${localTunnelPort}" file="test_file.txt" todir="${user.param}:${password.param}@localhost:${home}/foobar/" trust="true" />
	</sequential>
</sshsession>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmgamerView Question on Stackoverflow
Solution 1 - Maven 2Rich SellerView Answer on Stackoverflow
Solution 2 - Maven 2PaoloCView Answer on Stackoverflow
Solution 3 - Maven 2Brian AgnewView Answer on Stackoverflow
Solution 4 - Maven 2holmis83View Answer on Stackoverflow
Solution 5 - Maven 2Aaron DigullaView Answer on Stackoverflow
Solution 6 - Maven 2SebastianHView Answer on Stackoverflow