Search

Dark theme | Light theme

February 3, 2014

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"
grails.project.work.dir = "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/
stacktrace.log

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")

setDefaultTarget("cleanWarFile")

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

Code written with Grails 2.3.5