Groovy

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:

1
2
3
4
5
6
7
// 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:

1
2
3
4
5
6
7
8
$ 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:

1
2
3
4
$ 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:

1
2
3
4
$ 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:

1
2
3
4
// 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

 

Reference: Grails Goodness: Cleaning Up from our JCG partner Hubert Ikkink at the JDriven blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

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.
Subscribe
Notify of
guest


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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button