Saturday, February 03, 2007

Maven, Windows and Deploying to a Remote Location

If you are a programmer on Windows, some tasks take longer to perform than others. Attempting to set up maven so that you can deploy files to a remote machine is one of those tasks!

Here is what you need to get the job done:
  1. Download plink
  2. Download puttygen
  3. Download pscp
If the above links do not work, use Google and search for "putty download".

Once you download them, place them in a folder called putty and move that folder to your root drive (C:\). The next thing that you should do is add that folder to the PATH. Not sure how to do that?
  1. press the windows key and 'pause break' at the same time.
  2. A system properties panel will appear. Click on the 'Advanced' tab.
  3. Click on the 'Environment Variables' button.
  4. Locate the PATH variable from the System variables portion of the panel and choose 'Edit'.
  5. Append, without quotations, ';c:\putty;' to the end of the PATH value.
  6. close the panels and you are done!

Now you need to create a private key from the remote server that you would like to deploy your files to.
  1. ssh into the remote machine
  2. execute the following command on the remote machine
    ssh-keygen -b 1024 -f ssh_host_key -N '' -t rsa
    This will create a file called ssh_host_key.
  3. On your local machine, create a folder called c:\ssh and place the file, ssh_host_key, in that folder from the remote machine.

The key that we generated on the remote machine will now be used to create one that is compatible with putty. We will now use puttygen to create a 'putty' private key.
  1. Double click the file puttygen.
  2. Make sure that you select you select 'ssh-2 RSA' at the bottom of the page.
  3. Next choose, Conversion -> import.
  4. Select c:\ssh\ssh_host_key.
  5. Save the generated key (save both the public/private key) in c:\ssh.

Most of the 'hard' stuff is now done. Next we will add our remote server to our local maven configuration. To do this, we will have to modify the file settings.xml.

The settings.xml file is usually located in
c:\Documents and Settings\<user>\.m2.

Before proceeding, you should back up the file. Open settings.xml in Wordpad and add the following fragment of xml into the file:

<server>
<id>remote-repository-id</id>
<username>your_username_here</username>
<password>your password here</password>
<privateKey>c:/ssh/your_private_key.pkk</privateKey>
<configuration>
<sshExecutable>plink</sshExecutable>
<scpExecutable>pscp</scpExecutable>
</configuration>
</server>
That piece of xml goes in the servers portion of the file. Add it and modify the bold pieces of the text.

That's really all there is to do. However, if you are lazy, like myself, then create a windows batch file that does the deploying for you!

Below is one that I use. You will have to modify the lines in bold. Run it without arguments first, to have it display the usage!

@echo off
if "%1"=="" GOTO ERROR
if "%2"=="" GOTO ERROR
GOTO DEPLOY

:ERROR
@echo off
echo There was an error deploying file.
echo usage:
echo %0 jar pom [repository_id]
goto END

:DEPLOY
setlocal
set REPO=%3
if "%3"=="" set REPO=remote-repository-id
@echo on
REM should be on one line!
mvn deploy:deploy-file -Dfile=%1 -DpomFile=%2 -Durl=scpexe://remote_server_domain/:/remote/path/to/maven/ -DrepositoryId=%REPO%
REM end one line
endlocal
@echo off
GOTO END

:END
@echo off

8 comments:

Anonymous said...

Hello,

All seems to be clear, but I follow our text and have always an error ...

Is it possible to have example ? Is it the same if server is a Windows with cygwin-openssh (copssh) ?

Anonymous said...

Sorry,

My effective mail is fxmathieu_at_hotmail_dot_com (see previous post)

Ed said...

Hi Francois,

I am not sure about cygwin ... I have windows and cygwin installed, but I did have to download and install the PUTTY tools. I don't think that I got it working with just cygwin.

Good luck.

Ed said...

Francois, I have never deployed to a machine that is windows. What kind of errors are you getting?

Anonymous said...

Ed,

Here follows my config:

Client: Win XP, maven 2.0.9, Putty
Server: Win 2003 server, CygWin, OpenSSH

The Putty suite working fine (Putty, Plink, Pscp) but the maven deployement failed.

mvn deploy:deploy-file -D... -Durl=scpexe://....

In fact I need a howto that I canot find on the Internet ...

Thanks for your help.

Anonymous said...

My last error is :

Error deploying artifact: Unexpected end of data

Anonymous said...

Hi,

I have found something ...

I add "sshArgs" tag

<configuration>
<sshExecutable>plink</sshExecutable>
<scpExecutable>pscp</scpExecutable>
<sshArgs>-v bru0270a</sshArgs>
</configuration>


Where bru0270a is my remote hostname.

And download the last version of Putty tools.

All seems to work except that it do not create directories on remote server if the structure do not exist.

Ed said...

So you got it working? I am glad to hear it.