Pages

Saturday, 10 November 2012

Sharing a Single Eclipse Workspace on 2 Computers via Dropbox

I use 2 computers for development; my main office desktop running Windows 7 and my MacBook Air running OS X. When in my office I prefer the desktop's big keyboard and display for working on, but then I like to be able just to grab my Air and take it with me and be able to continue development on that somewhere else like the nearby beach cafe, the lounge for coding while watching TV etc without having to manually copy files and such each time I switch between computers.

I came up with a little hack to allow me to relatively easily share my Eclipse workspace using Dropbox. For those who don't know, Dropbox is a serivce that allows to you to automatically sync files like documents and photos across all your devices; computers, smartphones etc.

The major caveat of sharing a single Eclipse workspace across two different computers with two different operating systems is many of the workspace's settings are path dependent relative to the host operating system (think Windows \ versus OS X/Linux /). All these configurations are saved in the .metadata folder within the top of your workspace. I got around these issues by having two copies of this folder, one for the Windows based workspace and one for the OS X based workspace. A wrapper script copies the relevant folder before starting Eclipse and backs up a copy to save any new changes after Eclipse exits. This approach means you cannot have both Eclipse's running simultaneously on both machines otherwise the workspace's configuration may get corrupted.

Generally any changes made to a project on one machine will then be reflected on the other machine as well. Try to avoid using hardcoded paths when configuring the build paths for a project. Instead use Eclipse's User Libraries as they will be saved unique to each operating system's workspace settings.

Some people may say why bother with such a setup like this and instead just use a version control system. I am intending to also use Git for my projects but for tasks that aren't ready to be committed I think this Dropbox approach is fine to allow semi-seamless transition between two development machines.

Steps to Share an Eclipse Workspace On Windows and OS X Machines

These steps should be applicable to a Windows/Linux combination as well, substituting Linux in place of OS X.
  1. Install the same version of Eclipse separately on each machine.
  2. Sign up to Dropbox and install the client software on both computers.
  3. Make a folder in your dropbox for your workspace (e.g. C:\User\soliax\Dropbox\eclipse-workspace or /Users/soliax/Dropbox/eclipse-workspace). If Dropbox is setup properly, you'll only have to create it on one computer and then it will automatically be sync'd (created) on the other computer.
  4. Launch Eclipse on one of your computers, lets say Windows, and specify the location of your workspace folder. Optionally, you may setup workspace configurations at this time like installing any plugins, setting classpaths etc. Exit Eclipse when you're finished.
  5. With Eclipse closed, rename the .metadata folder to .metadata-windows.
  6. Repeat steps 4. and 5. but on your OS X computer and rename the folder to -osx, not -windows.
  7. Create a batch script on your Windows computer and save it as something like eclipse-dropbox.bat in the Eclipse installation folder. This is the wrapper script to start Eclipse. Copy/paste the following contents in. Configure the WORKSPACE variable to point to your actual workspace folder in your dropbox.
    @echo off
    REM Eclipse startup script for workspaces shared amongst two computers on Dropbox.
    REM Set path of your Eclipse workspace
    set WORKSPACE=%USERPROFILE%\Dropbox\Dev\workspaces\aroha
    echo Restoring Windows metadata for Eclipse...
    @call xcopy /HIEQRY "%WORKSPACE%\.metadata-windows" "%WORKSPACE%\.metadata"
    call eclipse.exe
    echo Backing up Windows metadata for Eclipse...
    @call xcopy /HIEQRY "%WORKSPACE%\.metadata" "%WORKSPACE%\.metadata-windows"
    
  8. If you want to pin this script to your Windows taskbar, try the following steps:
    1. Rename the *.bat script to *.exe. Then drop the icon on to your taskbar. For some silly reason Windows doesn't allow bat/cmd files to easily be pinned.
    2. Rename the script back to *.bat.
    3. Right-click the pinned icon, then right click the filename at the top of the menu (e.g. eclipse-dropbox) and choose Properties from the next menu.
    4. Rename the .exe extension to .bat.
    5. Change the icon as well if you wish.
  9. Create a shell script on your OS X computer and save it in the anywhere (I used /usr/bin) with the name of something like eclipse-dropbox. This is the wrapper script to start Eclipse. Make sure you use the command chmod +x in a Terminal window or something similar to make the script executable. Copy/paste the following contents in.This assumes Eclipse is installed in your Applications folder. Configure the WORKSPACE variable to point to your actual workspace folder in your dropbox.
    #!/bin/sh
    # Eclipse startup script for workspaces shared amongst two computers on Dropbox.
    
    # Set path of your Eclipse workspace
    WORKSPACE=$HOME/Dropbox/Dev/workspaces/aroha
    
    # Copy the backup .metadata folder used for the OS X version of Eclipse to the actual working copy
    echo Restoring OS X metadata for Eclipse...
    rm -rf "${WORKSPACE}/.metadata"
    cp -Rfp "${WORKSPACE}/.metadata-osx" "${WORKSPACE}/.metadata"
    
    # Launch Eclipse
    open -W /Applications/Eclipse
    
    # Backup the working copy of .metadata for the OS X version of Eclipse when Eclipse closes
    echo Backing up OS X metadata for Eclipse...
    rm -rf "${WORKSPACE}/.metadata-osx"
    cp -Rfp "${WORKSPACE}/.metadata" "${WORKSPACE}/.metadata-osx"
    
  10. If you want to add the OS X shell script to your dock, use Automator to create an application that has a single job to execute a shell script and point it to the script you created in step 9. Then you can drop that application onto your dock.
Now use these wrappers to start Eclipse. Make sure Eclipse is closed on the other computer before starting it. As the workspaces are technically different, any changes you make to one you will need to also do to the other. If you open a project on Windows, it will not automatically mean that project will be open next time you launch the OS X workspace. Changes you make to source code files and project specific settings should automatically be reflected (assuming to allow enough time to transfer to/from Dropbox's servers).

4 comments:

  1. Instead of putting the whole of Eclipse workspace in Dropbox, put only the Projects. In Projects too - you can put only the source in Dropbox and let the rest remain on some other local folder outside of the Dropbox folder. Something like this: http://redwoodr.tumblr.com/post/9062600303/using-dropbox-with-eclipse

    With this approach the overhead of copying over the .metadata folder each time you open Eclipse and then saving them back when Eclipse is closed can be gotten rid of!

    ReplyDelete
    Replies
    1. Hello,Arun Mehra
      wondering your solution work for PC and Mac ? If yes, can you provide a more detailed steps

      Thank you

      Delete
  2. Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.
    Computer Repair Lakewood Ranch

    ReplyDelete