Jstyler v0.0.3 – code formatting extension for BuildR

There I’m going to post information related to Jstyler – code formatting extension for BuildR.
Some time ago the v0.0.3 was released and is ready to use.
The main goal of this gem is to provide functionality to perform java code formatting for buildr. I’ve made a little research but it had no luck and decided to make own extension with such functionality. But there was a requirement to not to implement whole staff by my self.  I knew there is a cool functionality built-in Eclipse, but I’d like to have it outside of IDE. After research I’ve found out a lot of libraries, but most of them were not feed my needs and I decided to concentrate my attention on functionality provided by Eclipse. That can be ran through command line, actually here you can find out how it can be configured. But this was not what I wanted also, but I believed in Eclipse “magic”.
Then I’ve tried to extract classes required to perform formatting and had a luck. Now I have a jar files to for this and my runners – 2 java classes to prepare context and invoke format action using  Eclipse classes. All that staff I packed into one jar file.
Now it’s a Ruby‘s turn…
With Ruby things were a bit complicated as I don’t know to much of this language, but have a big desire to make it closer to me. At the end I have several Ruby files with BuildR extension definition.
That’s all about pre-history of this gem.
To use this extension you need to install BuildR and gem with extension itself.
sudo gem install buildr
sudo gem install jstyler
After installation you should add several lines of code into your project build-file (buildfile).
require 'jstyler' # this is inclusion of extension's code
And if you agree with Java convention about code formatting that’s it. To perform formatting you need to run command:
buildr format
But if you are disagree with Java conventions and want to use Eclipse convention you need to define a configuration options like following:
jstyler.options[:convention] = 'eclipse'
After all if you would like to have own style you should create it in Eclipse and save under some directory, then define path to it in your buildfile like following:
jstyler.options[:config] = 'path_to_your_org.eclipse.jdt.core.prefs'
Also you can define following configuration options:
  • jstyler.options[:config]  – receives path to config file with formatting style definition (Eclipse generated).
  • jstyler.options[:verbose] – if is specified output should be more detailed

  • jstyler.options[:convention] – java or eclipse – which convention should be used if config path was not specified, by default java is used.

Those options can be defined in ~/.buildr/settings.yaml or per project folder in the build.yaml files.
Next I give you a buildfile for sample project to start with.
# Sample project buildfile
require 'jstyler' # include extension
jstyler.options[:config] = "path_to_config"
define 'testsrc' do
  # extend Jstyler::Beautify #to extend buildr only for this project
  project.version = 1.0
  project.group = 'name.webdizz.jstyler'
  compile.options.target = '1.5'
  package :jar
Source code of the gem can be find here http://github.com/webdizz/jstyler. And the gem itself http://rubygems.org/gems/jstyler
If you have any question related to gem ask here.
That’s it.
Have a fun formatting.

Leave a Reply

Your email address will not be published. Required fields are marked *