Last updated by dbwhitaker 1 year ago

Checking Projects into SVN or Git

Author: Marc Palmer

Author: James Lorenzen

Table of Contents

Why a page dedicated to adding Grails projects to source control? Because you want to make sure that only realy source files rather than generated ones are added to the repository. There's nothing more annoying than being told by your SCM tool that such and such a file has been changed locally simply because it has been regenerated.

The key to successful use of source control with Grails is to make sure your SCM tool ignores the right files. Grails is pretty configurable, so this can depend on the project. But for a default Grails project you should ensure these files and directories are included in any ignore list:

  • target/
  • web-app/plugins/
  • web-app/WEB-INF/classes/ - common if you use Eclipse or STS
For old versions of Grails (pre-1.2), you'll also want to add:
  • plugins/
  • test/reports/
Do not ignore the .project and .classpath files. These files are required by Eclipse and STS to function. Even if you do not use Eclipse or STS, someone else who wants to check out the project may. Ignoring these files in your SCM will make it very difficult for Eclipse and STS to recognize the project.

Creating a new project and putting it into SVN

This approach is for use when starting a new Grails project - i.e. it does not yet exist on your disk at all, nor in SVN. It will talk you through your first check in, including the relevant svn:ignore properties, and avoids any check in of these ignored files that may or may not exist locally.

With SVN installed, create your project. We will use "MyProject" as the name for the purposes of demonstration:

grails create-app MyProject
cd <projname>
Now you must create an empty SVN directory under your SVN repository using whatever appropriate tools you have. This directory should be called the same as MyProject but it doesn't have to be. We will call this SVN repository directory path "emptysvndir" for this example.

Note: The WEB-INF instructions below are optional. If you perform those steps, you must run "grails upgrade" after checking out your repository.

Next you do:

svn checkout <svn-server-url>/emptysvndir/ .
svn add *
svn propset svn:ignore "WEB-INF" web-app/
svn propset svn:ignore "target" .
svn rm --force web-app/WEB-INF
svn rm --force target
svn commit -m "First commit"
That should have your project committed, and changes to ./plugins/core and ./web-app/WEB-INF/ should be ignored by SVN. You will not need to check out this project as we have used the "in place import" technique.

Why is this such a hassle? Well SVN prevents files or directories being considered for version control by checking an "ignores" list that is set via an SVN property. This property is set on specific directories within your SVN working copy. Therein lies the problem - until you have a working copy you cannot tell it to ignore files.

Fixing an existing project that is already in SVN

This approach is for use when you have optimistically imported your entire project into SVN already and have started getting errors or are being annoyed by the warning messages relating to locally changed files.

We will use "MyProject" as the name for the purposes of demonstration:

cd MyProject
svn propset svn:ignore "WEB-INF" web-app/
svn propset svn:ignore "target" .
svn rm web-app/WEB-INF
svn rm target
svn commit -m "Fixing SVN"
That should have your project committed, and changes to ./plugins/core and ./web-app/WEB-INF/ should be ignored by SVN.

Checking out from SVN

To checkout this project fresh locally or on another machine you will need to checkout as usual but then run:

grails upgrade
The grails upgrade command re-instates files we are not committing to the repository. This applies mainly to the files under WEB-INF/ dir: including applicationContext.xml, tld/* , sitemesh.xml , web.template.xml etc.

Adding a new project to Git

To check a project into git for the first time, use the following commands:

cd MyProject
git init
echo target >> .gitignore
echo web-app/WEB-INF >> .gitignore 
git add .
git commit -m "Initial commit"