Grails Goodness: Cleaning Up

When we use for example the compile or war command Grails will create files and stores them by default in the project’s working directory. The location of the project working directory can be customized in our grails-app/conf/BuildConfig.groovy configuration file. We remove the generated files with the Grails clean command. This command will remove all compiled class files, the WAR file, cached scripts and test reports. But this doesn’t remove all files in the project working directory. For example plugins or a temporary web.xml file, which are stored in the project working directory are not removed. We must use the clean-all command to also remove those files from the project working directory completely.

Let’s take a look at the default settings in our grails-app/conf/BuildConfig.groovy configuration file when we create a new Grails application:

// File: grails-app/conf/BuildConfig.groovy
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports" = "target/work"

After we run the war command we see the following contents in our target directory:

$ grails war
| Compiling 10 source files
| Compiling 142 source files
| Done creating WAR target/cleanCmd-0.1.war
$ ls target/
classes   cleanCmd-0.1.war stacktrace.log  work

Let’s first run the clean command and check the contents of the target directory again:

$ grails clean
| Application cleaned.
$ ls target/
stacktrace.log work

Notice the target/work directory still exists. We now run clean-all and examine the contents of the target directory:

$ grails clean-all
| Application cleaned.
$ ls target/

Now the work directory is removed as well.

We can also write our own scripts to for example only remove the generated WAR file with a clean command. With the following script we add the command clean-war to our application, which will delete the generated WAR file from our project:

// File: scripts/CleanWar.groovy
includeTargets << grailsScript("_GrailsClean")

We can use the targets cleanCompiledSources, cleanTestReports and cleanWarFile in our own scripts.

  • Code written with Grails 2.3.5


Reference: Grails Goodness: Cleaning Up from our JCG partner Hubert Ikkink at the JDriven blog.

Hubert Ikkink

My name is Hubert A. Klein Ikkink also known as mrhaki. I work at the great IT company JDriven. Here I work on projects with Groovy & Grails, Gradle and Spring. At JDriven we focus on SpringSource technologies. All colleagues want to learn new technologies, support craftmanship and are very eager to learn. This is truly a great environment to work in.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button