When I’m developing I like to have my eclipse set up to rsync my code on every save. Since OSX has the rsync command available this is not a hard thing to do. I choose to implement an ant builder and have eclipse run that builder when every I save.
This eclipse builder expects two things:
- You have configured access to the remote server via passwordless SSH
- The key for the passwordless SSH connection is available on the computer that eclipse is installed on
Ok, I’ll break down what I did in the ant script and then explain how to configure eclipse to execute this script on every save.
Start the ant script with the XML declaration. Then we add the project element, make sure that the basedir attribute is set to the current directory “.” , and make sure the default attribute is set to rsync. The description element is not required.
<project name="Rsync On Save" basedir="." default="rsync">
<description>Builder for eclipse to rsync on save for OSX.</description>
Next I start declaring ant properties that will be used by the rsync ant target. This property is the location of the directory that will be the source for the rsync.
<property name="rsync.source.dir" value="${basedir}"/>
This property configuration for the destination host name.
<property name="rsync.destination.host" value="remote.example.com"/>
This property is the configuration for the destination directory.
<property name="rsync.destination.dir" value="/var/www/wp-content/plugins/rob-tester"/>
This property is the location of the SSH key that can be used to login to the destination server.
<property name="rsync.ssh.key" value="/Users/rmcfrazier/my-keys/remote.example.com.key"/>
This property is the user that will be used on for the SSH connection to the remote server.
<property name="rsync.ssh.user" value="ssh-user"/>
This is the heart of the ant builder, it is the target that actually perform the rsync. It does so by calling thru to the OS shell and performing an rsync.
<echo message="Rsync source:"/>
<echo message="${rsync.source.dir}"/>
<echo message="Rsync destination:"/>
<echo message="${rsync.ssh.user}@${rsync.destination.host}:${rsync.destination.dir}"/>
<exec dir="." executable="rsync">
<!-- rsync command options -->
<arg value="-arv"/>
<!-- exclude all hidden files and directories -->
<arg line="--exclude='.*'"/>
<!-- exclude build.xml and rsync.xml -->
<arg line="--exclude='build.xml'"/>
<arg line="--exclude='rsync.xml'"/>
<!-- variable that holds the filepath to the ssh key -->
<arg line="-e "ssh -i ${rsync.ssh.key}""/>
<!-- local directory that is the source for the rsync -->
<arg value="${rsync.source.dir}"/>
<!-- remote host and directory destination for rsync -->
<arg value="${rsync.ssh.user}@${rsync.destination.host}:${rsync.destination.dir}"/>
</exec>
</target>
It is configured to not rsync any hidden files or directories, and I have a short list of additional files I do not want rsync’d. You can edit this to exclude additional files.
<arg line="--exclude='.*'"/>
<!-- exclude build.xml and rsync.xml -->
<arg line="--exclude='build.xml'"/>
<arg line="--exclude='rsync.xml'"/>
Ok, now that the ant builder file is complete we can now configure eclipse to use this whenever a file is saved.
Copy the rsync.xml file to the root of an eclipse project
Next highlight the project and select properties. Then select builders and click the new button.
Enter the name of the builder, I choose example_rsync, you will need to do this for each project so pre-pending the project name to “_rsync” makes sure I don’t have any name collisions. Under the Buildfile setting, click the Browse Workspace button.
Select the location of the rsync.xml build file for this project, and click the OK button.
Now the important part we need to associate the the rsync with the save. Click the Targets tab, we want to configure the Auto Build event to run the default target on the ant script. Click the Set Targets button next to the Auto Build setting and make sure it is set to the rsync target which is the default. Click the OK button.
Click OK, to close the builder screen.
Turn on the auto build for the project. From the Eclipse menubar click Projects > Build Automatically
Once project auto build is on, whenever a file is saved in the project any associated builders will be ran.
That’s it when you save a file in the project it will now rsync the changes to your remote server. You need to perform these steps for each eclipse project you want to have rsync on save.
Here is the code all together, you can also down load from GitHub.
<project name="Rsync On Save" basedir="." default="rsync">
<description>Builder for eclipse to rsync on save for OSX.</description>
<!-- local source directory for rsync (read from this directory) -->
<property name="rsync.source.dir" value="${basedir}"/>
<!-- remote rsync host -->
<property name="rsync.destination.host" value="remote.example.com"/>
<!-- remote rsync directory (write to this directory) -->
<property name="rsync.destination.dir" value="/var/www/wp-content/plugins/rob-tester"/>
<!-- filepath to the ssh key-->
<property name="rsync.ssh.key" value="/Users/rmcfrazier/my-keys/remote.example.com.key"/>
<!-- ssh user to login to remote -->
<property name="rsync.ssh.user" value="ssh-user"/>
<target name="rsync">
<echo message="Rsync source:"/>
<echo message="${rsync.source.dir}"/>
<echo message="Rsync destination:"/>
<echo message="${rsync.ssh.user}@${rsync.destination.host}:${rsync.destination.dir}"/>
<exec dir="." executable="rsync">
<arg value="-arv"/>
<!-- exclude all hidden files and directories -->
<arg line="--exclude='.*'"/>
<!-- exclude build.xml and rsync.xml -->
<arg line="--exclude='build.xml'"/>
<arg line="--exclude='rsync.xml'"/>
<!-- variable that holds the filepath to the ssh key -->
<arg line="-e "ssh -i ${rsync.ssh.key}""/>
<!-- local directory that is the source for the rsync -->
<arg value="${rsync.source.dir}"/>
<!-- remote host and directory destination for rsync -->
<arg value="${rsync.ssh.user}@${rsync.destination.host}:${rsync.destination.dir}"/>
</exec>
</target>
</project>
Very good article on rsync. Here is one from around 1999 but still the very best rsync tutorial I’ve ever read. Explains it very well IMO: http://everythinglinux.org/rsync/
Yes, this is a very good tutorial about rsync in general. I was giving an example on how to integrate rsync with eclipse.
Thanks! This was super helpful. I was trying so many things which didn't work and finally stumbled upon your blog. Wish I had seen it before and saved some time.
This was very helpful! Thanks. Just 10 minutes to setup everything and I am a happy Mac User