Archive for category development

Adding WebFlow support to Maven driven Grails project

It’s just simple tip, but you could find it useful if you noob in Maven. WebFlow comes with Grails by default, but since we develop with Maven we should include this dependency manually. Just modify your Grails project pom.xml dependencies by including webflow reference:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    ...
    <dependencies>
        ...
        <dependency>
            <groupId>org.grails</groupId>
            <artifactId>grails-webflow</artifactId>
            <version>1.1.1</version>
        </dependency>
        ...
    </dependencies>
    ...
</project>

Tags: , ,

CSS convention for Grails

As far as our CSS styles grow I found that there should intuitive way to manage CSS inclusions. I’ve decided to follow great Grails fundamental idea and place them by some convention and include automagically.

We found useful to have separate CSS for each view (or controller action if you wish). Convention is simple: place each CSS in the same path as you place your views under /views folder.

For example, we have view: /views/picture/gallery.gsp and want to have gallery.css which contains all styles for this view. We should place it in the same path as our view located, in our example it should be /web-app/css/picture/gallery.css. Now to automatically include such CSSs by convention we should modify our main layout (/views/layouts/main.gsp) next way:

<head>
...
<% if (webRequest.controllerName && webRequest.actionName) { %>
    <link rel="stylesheet" href="${resource(dir: 'css/' + webRequest.getControllerName(), file: webRequest.getActionName() + '.css')}"/>
<% } %>
...
</head>

All further CSSs placed by this convention will be automatically included for each action (which uses main layout) . If you have more layouts you could modify them same way. Anyway their count will be much less than your actions/views count.

Hope this helps. If you have some ideas to improve this approach or you use some other approach, please share :)



                      

Tags: ,

Settin’ up Grails 1.1.1. with Grails Maven Plugin in Intellij IDEA + JetGroovy

Grails Maven Plugin is still non predictable and it’s 1.0 version supports only 1.1 version of Grails. 1.1-SNAPSHOT version supports grails 1.1.1 but there are still misunderstandings with Grails plugins are exist (Grails wants to store them in your user home, but  Grails Maven plugin wants them to be stored in your application dir).

As for now I’m trying to avoid using plugins from Grails, and include all libraries directly with Maven dependencies.

1. Make your maven know where to get grails-maven-plugin by editing your /.m2/settings.xml (create it if there is no one exist):

<settings>
    <pluginGroups>
        <pluginGroup>org.grails</pluginGroup>
    </pluginGroups>
</settings>

2. Go to secret place where you store you projects and run:

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate -DarchetypeGroupId=org.grails -DarchetypeArtifactId=grails-maven-archetype -DarchetypeVersion=1.0 -DgroupId=example -DartifactId=maven-grails-demo

3. Now open just generated pom.xml and make following changes:

  • change versions of grails-crud and grails-gorm from “1.1” to “1.1.1”
  • change grails-maven-plugin version from “1.0” to “1.1-SNAPSHOT”
4. Run “mvn initialize” from your project root.
5. One more trick, that should be applied for 1.1-SNAPSHOT version of grails-maven-plugin to make it work: add following dependency to your pom.xml (see http://jira.codehaus.org/browse/GRAILS-4574 for details):
<dependency>
    <groupId>org.tmatesoft.svnkit</groupId>
    <artifactId>svnkit</artifactId>
    <version>1.2.3.5521</version>
    <scope>runtime</scope>
</dependency>

6. Now, it’s time to import your maven project into Intellij IDEA (I recommend to use latest EAP http://www.jetbrains.net/confluence/display/IDEADEV/Maia+EAP). You can do it by “Create New Project → Import Project from External Model → Maven…”.

Pay attention to Facets that Idea will detect on project import finished. Hint: look in right bottom corner for round icon with “i” on it, if Idea won’t show you dialog window with detected facets. Facets detection is important to let Idea know that it’s Grails application.
Now close and reopen project again to let JetGroovy plugin use facets and treat application directories in a right way. You should see “Grails:maven-grails-demo” preselected in IDEA’s run/debug configurations combo. If it’s so – you on the right way and JetGroovy knows that it’s Grails application. If not – try to fix it in “module settings → facets”. If you didn’t make it – contact me, I’ll try to help.

You probably already tried to run application but all you’ve got was:
Exception in thread "main" java.lang.ClassNotFoundException: org.codehaus.groovy.grails.cli.support.GrailsStarter at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:87)
Don’t worry. It’s because grails-maven-plugin depends on grails-core which not contain GrailsStarter. It’s available in standalone Grails distribution. First of all you are still able to run grails application with “Maven Projects → maven-grails-demo → Plugins → grails → grails:run-app” or just by running “mvn grails:run-app” from project root. If you still want to run your application from run configuration in Idea you could or create run configuration from Maven goal, by clicking on with right button and selecting appropriate menu item or by installing standalone Grails and telling IDEA where it is.

Setting up standalone Grails for your application

  • Install Grails as described http://grails.org/Installation.
  • Go to module settings → Groovy → click “Add…” → Create new Grails SDK… → Tell Idea where your standalone Grails is → Click “Replace” (not “Add”, important) in dialog appeared
Now you able to run your Grails application directly from Idea with JetGroovy plugin. Enjoy ;)

Tags: , , , , ,