2011
06.29

Have you played around with Grails Resources yet?  I just started looking at the plugin tonight and turns out it’s pretty slick!  So, I thought I would write a post to document the steps I took to get up and running!

First, you will want to grab the latest source for Grails 2.0.0.BUILD-SNAPSHOT:

  • git clone https://github.com/grails/grails-core.git
  • cd grails-core
  • ./gradlew install

Then you will want to create a new app and install the relevant plugins.  The resources plugin is installed by default with Grails 2.0, so you don’t have to worry about that one, but you will want to install the zipped-resources and cached-resources plugins.  Also, unless you’ve been living under a rock in the web development world, you know that dynamic stylesheet languages are all the rage right now, ha!  In Ruby on Rails 3.1, they will actually bundle SASS by default.  SASS looks pretty good but there currently isn’t a plugin that works with Grails resources, so I’ve started using the LESS plugin.  LESS looks just as good as SASS as far as I can tell and since it works with Grails resources, it’s definitely the way to go!  So, let’s dive in!

  • cd ~/workspace (or wherever you keep your code)
  • grails create-app sampleapp
  • cd sampleapp
  • grails install-plugin zipped-resources
  • grails install-plugin cached-resources
  • grails install-plugin lesscss-resources
  • grails install-plugin jquery-ui (jquery is installed by default)

Now that we have all the plugins installed, we need to create our LESS, CSS and JavaScript files.  So, let’s create these files.

  • sampleapp/web-app/less/main.less
  • sampleapp/web-app/css/main.css
  • sampleapp/web-app/js/main.js

And put this in the main.less file:


@color: #FF0000;
   
h1 {
   color: @color;
}

You can see that we are actually defining a variable in our CSS called color and using this variable in h1. This is obviously pretty basic programming, but until frameworks like SASS and LESS came along, this was not possible in CSS, so it’s actually pretty sweet! I love being able to define colors at the top of a LESS file and use variables throughout the rest of the file. It makes it so much easier to switch colors later on. No more find-and-replace to change color codes, ha!

You might be wondering why we created a LESS file and a CSS file though, right?  Well, I actually did this just for demonstration purposes to show that you can still obviously use regular CSS files while you are using LESS.  I’ll probably use LESS for all my styles going forward though.  Next we need to add a config block for resources.  Add the following to the bottom of sampleapp/grails-app/conf/Config.groovy


grails.resources.modules = {
   core {
      dependsOn 'jquery, jquery-ui'
      resource url:'/js/main.js'
      resource url:'/css/main.css'
      resource url:'/less/main.less',attrs:[rel: "stylesheet/less", type:'css'], bundle:'bundle_core'
   }
}

This is where the power of the Grails resources plugin really starts to show! In this resource module, we’re saying we depend on jquery and jquery-ui, so it will include these as well anywhere we say we require the module “core” in our app. Then we need to update our layout as per the resources instructions, so create a basic layout that looks like this:


<!doctype html>
<html>
   <head>
      <title><g:layoutTitle default="Sample App"/></title>
      <r:layoutResources/>
   </head>
   <body>
      <g:layoutBody/>
      <r:layoutResources/>
   </body>
</html>

And then this is how we include our resources module in a GSP.


<!doctype html>
<html>
   <head>
      <meta name="layout" content="main"/>
      <r:require module="core"/>
   </head>
   <body>
      <h1>Sample App</h1>
   </body>
</html>

If you look at the HTML source for the page, you’ll see that the resources plugin is renaming the files to hashes of the file so that it knows when the files have changed.  If everything is working correctly, the Sample App heading text on the page will show up in red as per our LESS style and if you view the generated CSS, you’ll see that the color code has been added in for the variable.  This is just a basic example, so make sure you take a look at the LESS website to get a feel for everything you can do with a dynamic stylesheet language.

So there we have it!  Our resources are zipped, cached and we’re using a dynamic stylesheet language in Grails 2.0!  I hope you enjoyed reading this post and please let me know if you have any feedback on the steps I used to get going with these plugins.

2011
06.21

Did you know that you can have a complete modular enterprise application up and running in under 5 minutes with Grails? The architecture will be a lot more flexible and maintainable than any “Enterprise Java” structure with Ant or Maven too!

First things first, how many different modules should an application have? Well, every application probably needs three essential things these days: website, mobile website and API. Wait, why do you need an API if you don’t necessarily have any plans of sharing data? Well, the answer is you might need native Android and iPhone apps in the future, so you might as well plan for them right away. The HTML5 frameworks are getting pretty good though! Take a look at Sencha Touch if you haven’t yet as it’s pretty cool!   So why can’t you just use one project for the API and the website?  Well, I don’t recommend having a controller that returns JSON or HTML based on the request. It seems cluttered to me plus you might want to return more or less data given the target and have a completely different security setup for each (basic, digest or token for API vs forms for web).  Also, versioning the API is a lot easier if it’s separate from the website. Plus as you’ll see in a bit, it’s really easy with Grails to separate out the API into a separate project and reuse code.

First, create a parent folder for the entire application. Then create three separate Grails applications in this parent folder.

  • grails create-app sampleapp-web
  • grails create-app sampleapp-api
  • grails create-app sampleapp-mobile

Next, create a Grails plugin for the core part of the application. This is where you’ll put all reusable code for your application like domain classes and services.

  • grails create-plugin sampleapp-core

Now we need to set a reference to our plugin in each of the three Grails applications we created. This is slightly different from installing regular Grails plugins. Since this plugin is going to be changing a lot as you develop the application, you don’t want to be constantly packaging and installing the plugin into each of the three apps. So, instead add this line to the end of grails-app/conf/BuildConfig.groovy for each of the three (web, api and mobile).

  • grails.plugin.location.’sampleapp-core’ = “../sampleapp-core”

Now you can add a class to “core” at any time and it’s automatically available to all three projects! You never have to compile the core plugin, but rather when you run or test any of the three main apps, it will automatically pick up changes made in core. Pretty slick, huh! We’re not done yet though! You obviously want to test all three at the same time, right? Ha! Ok, maybe not, but I do for some reason! Plus I like to run other stuff on 8080 anyway, like Jenkins for example, so let’s add this line to grails-app/conf/BuildConfig.groovy so that they run on different ports and there’s no conflict on port 8080.

  • sampleapp-web/grails-app/conf/BuildConfig.groovy:
    • grails.server.port.http = 8081
  • sampleapp-api/grails-app/conf/BuildConfig.groovy:
    • grails.server.port.http = 8082
  • sampleapp-mobile/grails-app/conf/BuildConfig.groovy:
    • grails.server.port.http = 8083

Next, I’m never nearly as good of programmer as I pretend to be, so I always add the code review plugin!

  • grails install-plugin codenarc

Finally, the last thing actually has nothing to do with setting up an application, but rather is just cause I’m kind of lazy, ha! Let’s shorten the typing required for run-app and test-app scripts. This may seem really trivial, but think about how many times you’ll type these commands, makes sense to make them as short as possible, doesn’t it?  Plus, it will also show how cool our setup is by being able to reuse scripts across all four Grails projects! You could also create scripts that are available to all Grails projects on your computer, but I think it’s better to keep each application completely independent.

Run the following command while in the core plugin (sampleapp-core):

  • grails create-script ra
  • grails create-script ta

This will create two files in sampleapp-core/scripts.  Erase everything from these two files and add this:

  • sampleapp-core/scripts/Ra.groovy: includeTargets << grailsScript(“RunApp”)
  • sampleapp-core/scripts/Ta.groovy: includeTargets << grailsScript(“TestApp”)

With these two scripts setup in core, you can now simply execute grails ra or grails ta instead of grails run-app and grails test-app in any of the four Grails projects!

Well, I hope you enjoyed my Grails Setup article!  It certainly is a better setup than any crazy stuff I used to do with Ant or Maven!  Let me know if you have any suggestions for ways to improve it.  Happy coding!

2011
06.20

Ok, I have to admit that the title is a little misleading…  This isn’t a traditional “Grails 101″ where you learn how to install the framework and create twitter, ha! This is instead an article about what it means to get started contributing back to the Grails community. I just started doing this a couple months ago, so I thought it would be interesting to blog about the steps required to do so for anyone else that wants to start contributing!

So, how do most people get started with Grails?  They go to grails.org and download the ZIP binary, right?  Sure, this is good, but if that’s all you do then you are kind of missing out.  I would argue that starting out with any new technology, the first thing you should do is build it from scratch!  Wait, what?  Why waste your time building from scratch?  The reason is you need to understand what’s happening under the hood to really become an expert so you can make modifications if needed for your application.  I’m certainly not an expert on Grails (yet!) but I have definitely increased my knowledge about the framework and become a better Grails developer by building Grails from source, examining the source code and contributing patches to the documentation!

So, let’s dive in!

  1. Login to GitHub (create an account if you don’t have one)
  2. Navigate to Grails-Core and Grails-Doc and fork both of them. You should now have your two forks similar to these Bobby Warner Grails-Core, & Bobby Warner Grails-Doc for your account
  3. Next you want to create your local copy (a clone in Git terms) and you can do this by executing these two commands:
    • git clone git@github.com:youraccount/grails-core.git
    • git clone git@github.com:youraccount/grails-doc.git
  4. There’s three sets of repositories now, the original ones at github.com/grails, your forks at github.com/youraccount and your local copies. We already have a reference from local to the GitHub forks, but we also want a reference from local to the originals so that we can pull in updates that are made by other users since we forked.
    • cd grails-core
    • git remote add upstream git://github.com/grails/grails-core.git
    • git fetch upstream
    • cd ../grails-doc
    • git remote add upstream git://github.com/grails/grails-doc.git
    • git fetch upstream
  5. Now we have our local repositories all set and it’s time to build them!
    • cd ../grails-core
    • ./gradlew install
    • cd ../grails-doc
    • ./gradlew docs  (Or  ./gradlew docs -Dgrails.home=../grails-core)
    • Set your $GRAILS_HOME to the grails-core directory
    • grails create-app newapp
  6. Now we’re rocking!  We have our own local copies of the Grails source-code and the documentation and are ready to start making our own modifications! We also created a new Grails application using our own local version of Grails we just compiled.
  7. Now it’s time to contribute something back to the community! First, we’ll have to find something to change. Then commit the change to our local repository and fork. Finally, we’ll open a pull request on the original Grails repository.  The example I’ll use just to explain was a simple change to the docs I made and opened a pull request for: https://github.com/grails/grails-doc/pull/46. You can see when clicking on that link that it is marked as “closed” status which means that someone on the core team (Burt Beckwith in this case) reviewed the pull request and committed to the master branch (my patch was “accepted” by the Grails team to the docs).  So, once you’ve found something you want to change, this is what you need to do:
    • git branch your-change-name
    • git checkout your-change-name
    • Edit the files you want to change 
    • git commit -a -m ‘Your Commit Message’
    • git push origin your-change-name
    • Then back on GitHub, click Pull Request to send it off for review! If it’s good, hopefully your pull request will be committed and if not, hopefully you’ll be given some comments about why not, ha!
  8. Lastly, you’ll want to make sure you keep your copies (remember there’s two, the GitHub fork and your local one) up to date with the original.
    • cd ../grails-core
    • git checkout master (if you made a branch here in step 7)
    • git fetch upstream
    • git merge upstream/master
    • git push origin master
    • ./gradlew install
    • cd ../grails-doc
    • git checkout master (if you made a branch here in step 7)
    • git fetch upstream
    • git merge upstream/master
    • git push origin master
    • ./gradlew docs  (Or  ./gradlew docs -Dgrails.home=../grails-core)
  9. Then just keep an eye on the GitHub pull request to see if it’s accepted!

Now that you’ve done a basic branch and pull request, it’s time to take a look at JIRA and see if you can tackle any open issues (or start opening your own!).  I personally haven’t solved too many issues yet other than some simple bug fixes, but hope to start contributing more in the future! 

I hope you enjoyed my “Getting Started With Grails” article.  Please let me know if you have any feedback or suggestions about the steps I outlined for getting started contributing back to Grails!

Switch to our mobile site