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