Sep 17, 2009

Dependency management in Grails 1.2

Graeme Rocher talks about the new DSL-based dependency management system in Grails 1.2, which is due to be released by the end of the month.

Don't know if it will be “done right”, but at least maven users are not forgotten :

If you're addicted to your pom.xml file then we have even added the ability to read dependencies from the pom.xml instead of using the DSL. All in all, Grails 1.2 will give you significantly better control over dependencies and how they are resolved.

Addicted to my pom.xml, that’s right ! ;) Thx Graeme.

Jun 22, 2009

Grails 1.1.1 : Maven Integration

This tutorial explains how to set up a clean and new Grails 1.1.1 mavenized project using the grails-maven-plugin.

Prepare Maven environment

Edit your ~/.m2/settings.xml as follows :

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

<profiles>
  <profile>
    <id>default</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <pluginRepositories>
      <pluginRepository>
        <id>snapshots.codehaus.org</id>
        <url>http://snapshots.repository.codehaus.org/</url>
      </pluginRepository>
    </pluginRepositories>
  </profile>
</profiles>

 

The minimal pom.xml for using Grails 1.1.1

The OSCache and HsqlDB are used by Grails by default, and can be changed with little configuration.

<?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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>test</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>test</name>
  <dependencies>
    <dependency>
      <groupId>org.grails</groupId>
      <artifactId>grails-gorm</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>hsqldb</groupId>
      <artifactId>hsqldb</artifactId>
      <version>1.8.0.10</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.5.5</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.tmatesoft.svnkit</groupId>
      <artifactId>svnkit</artifactId>
      <version>1.2.3.5521</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>opensymphony</groupId>
      <artifactId>oscache</artifactId>
      <version>2.4</version>
      <exclusions>
        <exclusion>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
        </exclusion>
        <exclusion>
          <groupId>javax.jms</groupId>
          <artifactId>jms</artifactId>
        </exclusion>
        <exclusion>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
  <build>
    <pluginManagement />
    <plugins>
      <plugin>
        <groupId>org.grails</groupId>
        <artifactId>grails-maven-plugin</artifactId>
        <version>1.1-SNAPSHOT</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <goals>
              <goal>init</goal>
              <goal>maven-clean</goal>
              <goal>validate</goal>
              <goal>config-directories</goal>
              <goal>maven-compile</goal>
              <goal>maven-test</goal>
              <goal>maven-war</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

 

Initialize a new Grails project

mvn initialize
You will then notice that the application version is not correct :
Application expects grails version [1.1], but GRAILS_HOME is version [1.1.1] - use the correct Grails version or run 'grails upgrade' if this Grails version is newer than the version your application expects.
Now edit <project>/application.properties
app.grails.version=1.1.1
A web.xml is missing : (since we are using a WAR packaging)
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
So let’s create an empty “web.xml” file, in “<project>/src/main/webapp/WEB-INF”

Compile the Grails project, and run it

mvn clean install
And so it should work…
mvn grails:run-app
Enjoy.

Apr 4, 2009

Problem Solved : “Application expects grails version [1.1], but GRAILS_HOME is version [null]”

I finally could diagnose the problem more specifically ; this error seems to be due to a conflict between the grails-maven-plugin and quartz. (at least with version 1.5.2)

I didn’t investigate more than that (And I really don’t want to) but adding/removing this section from the pom.xml made the error appear/disappear.

<dependency>
  <groupId>opensymphony</groupId>
  <artifactId>quartz</artifactId>
  <version>1.5.2</version>
</dependency>

Quartz 1.5.2 and 1.6.1 are 2 conflicts I found ; but there may be more. Be warned.

NB : Quartz 1.6.0 seems to be OK.

(This game is very tight.)

<dependency>
  <groupId>opensymphony</groupId>
  <artifactId>quartz</artifactId>
  <version>1.6.0</version>
</dependency>

Grails Maven Plugin fails : InvocationTargetException, Java heap space

The problem

Following the instructions from http://www.grails.org/Maven+Integration :

  • I didn’t set a GRAILS_HOME environment variable.
  • I created a default project usgin the following script :

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate -DarchetypeGroupId=org.grails -DarchetypeArtifactId=grails-maven-archetype -DarchetypeVersion=1.0 -DarchetypeRepository=http://snapshots.repository.codehaus.org -DgroupId=example -DartifactId=my-app

  • As required, I initialized the project :

cd my-app
mvn initialize

  • I tried to build my freshly created project :

mvn clean install

  • Uhuh..

[INFO] [grails:maven-functional-test {execution: default}]
Running pre-compiled script
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Unable to start Grails
Embedded error: java.lang.reflect.InvocationTargetException
Java heap space
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 minute 24 seconds
[INFO] Finished at: Sat Apr 04 20:51:40 CEST 2009
[INFO] Final Memory: 63M/63M
[INFO] ------------------------------------------------------------------------

The solution

The easy trick consists of adding this environment variable :

set MAVEN_OPTS=-Xmx128m

128m is a minimum to let the project build. But I guess 512m would be recommended for building Grails projects, since this value is configured by default in “startGrails”.

set MAVEN_OPTS=-Xmx512m