63

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.

Igor Brejc
  • 18,714
  • 13
  • 76
  • 95
mgamer
  • 13,580
  • 25
  • 87
  • 145

6 Answers6

39

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

Community
  • 1
  • 1
Rich Seller
  • 83,208
  • 23
  • 172
  • 177
  • 2
    One drawback of using `maven-deploy-plugin` is that it always uploads a `maven artifact`, it is, it will upload to a `groupId/artifactId/version/artifactId-version.ext` path location, if you just want to upload a file to some specific server location `antrun` plugin with `scp` task gets the work done. – Jaime Hablutzel Jul 13 '14 at 13:06
  • This answer is obsolete. There is a straight way to copy a file using Wagon plugin. See http://stackoverflow.com/a/29208672/318054. – ᄂ ᄀ Feb 25 '17 at 20:12
  • @fnt could you please elaborate? Looking at the linked documentation, it seems like this is indeed the most "official" way. – Zero3 Jul 09 '17 at 00:00
  • 2
    @Zero3 It does not matter how "official" it is when it is all pain and tears. I wrote a comprehensive answer on the matter—https://stackoverflow.com/a/42468595/318054 – ᄂ ᄀ Jul 09 '17 at 09:27
  • @fnt I'm not sure that is an explanation to why this answer is obsolete, but I ended up using Wagon's `upload-single` (to deploy a .war to Jetty). Here, take an upvote anyway! – Zero3 Jul 16 '17 at 01:45
  • @Zero3 The answer suggests to use the facility not intended for copying files. `maven-deploy-plugin` is for deploying **artifacts** to **repositories**. The fact that under some circumstances you can get a file copied via `maven-deploy-plugin` does not make it the right tool. This is explained in my answer I referred to above (if you cared to read it). Current answer is obsolete because since the time it was has been written we got a proper tool for the task. – ᄂ ᄀ Jul 30 '17 at 09:38
21

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>
ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
PaoloC
  • 3,817
  • 1
  • 23
  • 27
15

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

martin clayton
  • 76,436
  • 32
  • 213
  • 198
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
8

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
holmis83
  • 15,922
  • 5
  • 82
  • 83
5

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>
SebastianH
  • 2,172
  • 1
  • 18
  • 29
5

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820