Task X: Storing Your Maven Project in Version Control

Since Maven's standard project structure is so simple, it makes putting your project under version control very easy. A common practice for source code management is to not store the binaries generated by your code in version control. This can also extend to not storing any generated code either. Maven helps support these practices by putting all compiled and generated items under a common target directory at the root of your project. When you add your project to version control you configure it to ignore the target directory and you're done!

Version control systems generally provided a file or property format to specific what files and directories you want to ignore. With Git you create a file called .gitignore where you list the files and directories you want to ignore. For Subversion you set a property named svn:ignore. Both Git and Subversion support using wildcards but they shouldn't be necessary for Maven projects.

Creating a Git Repository for a Maven Project

To create a Git repository of a Maven project, do this:

prompt> git initt
Initialized empty Git repository in /Users/brian/projects/hello/.git/
prompt> mate .gitignore
  target
git add .gitignore pom.xml src
prompt> git commit -m "Initial Import"
[master (root-commit) a778c7a] Initial Import
 4 files changed, 94 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 pom.xml
 create mode 100644 src/main/java/com/example/App.java
 create mode 100644 src/test/java/com/example/AppTest.java

Creating a Subversion Repository for a Maven Project

To import a Maven project into Subversion, do this:

mvn clean
svn import . http://example.com/svn/projects/hello/trunk
svn propset svn:ignore target .
svn add .
svn commit -m "Ignoring target directory"