I have help a team recently to move to maven 3, and ask for “what’s the standard way to organise a project ?”.

Using Maven, you are encourage to follow convention to organise your library sources, but working at a project level you have more freedom.

When you use Maven since years, yo know there’s some good / bad practices that will make your project easy to maintain or not. Recently, some plug-in maintainer have advise a structure, and I’m pretty align with it.

So let’s describe it, and see how to benefit from it. For those interested, you can clone the repository on github, to have the files locally on your PC ( https://github.com/LaurentTardif/mavenTraining )

The project is made of :

  • 3 java libraries (2 in project 1 : module1 and module2, and 1 in project2)
  • one build-tools project
  • one pom parent project

We’ll saw later the benefit of this structure.

+—build-tools
|   \—src
|       \—main
|           \—resources
|               \—build
+—PomParent
+—Project1
|   +—module1
|   |   \—src
|   |       +—main
|   |       |   \—java
|   |       |       \—org
|   |       |           \—persistentsas
|   |       |               \—mavenTraining
|   |       \—test
|   |           \—java
|   |               \—org
|   |                   \—persistentsas
|   |                       \—mavenTraining
|   \—module2
|       \—src
|           +—main
|           |   \—java
|           |       \—org
|           |           \—persistentsas
|           |               \—mavenTraining
|           \—test
|               \—java
|                   \—org
|                       \—persistentsas
|                           \—mavenTraining
+—Project2
|   \—module1
|       \—src
|           +—main
|           |   \—java
|           |       \—org
|           |           \—persistentsas
|           |               \—mavenTraining
|           \—test
|               \—java
|                   \—org
|                       \—persistentsas
|                           \—mavenTraining
\—src
\—site
\—markdown
\—training

Now, let add some files inside this structure ;

  • Some java files in sources and tests. The current setup is minimalist, and contains one file per project.
  • A pom files per library, and some others to make life easier
  • Some files in the build-tools repository, these files will be use to share some common configuration or settings among  libraries.
  • A package-info.java to demonstrate package documentation in Java
  • a Site repository with some markdown file (.md), to demonstrate how to provide documentation up to date with your project.

C:.
|   .gitignore
|   LICENSE
|   pom.xml
|   README.md
|
+—build-tools
|   |   pom.xml
|   |
|   \—src
|       \—main
|           \—resources
|               \—build
|                       checkstyle.xml
|                       lib-filter.xml
|                       LICENSE.txt
|
+—PomParent
|       parent.pom
|
+—Project1
|   |   pom.xml
|   |
|   +—module1
|   |   |   pom.xml
|   |   |
|   |   \—src
|   |       +—main
|   |       |   \—java
|   |       |       \—org
|   |       |           \—persistentsas
|   |       |               \—mavenTraining
|   |       |                       HelloWorld.java
|   |       |                       package-info.java
|   |       |
|   |       \—test
|   |           \—java
|   |               \—org
|   |                   \—persistentsas
|   |                       \—mavenTraining
|   |                               HelloWorldTest.java
|   |
|   \—module2
|       |   pom.xml
|       |
|       \—src
|           +—main
|           |   \—java
|           |       \—org
|           |           \—persistentsas
|           |               \—mavenTraining
|           |                       HelloWorld2.java
|           |
|           \—test
|               \—java
|                   \—org
|                       \—persistentsas
|                           \—mavenTraining
|                                   HelloWorld2Test.java
|
+—Project2
|   |   pom.xml
|   |
|   \—module1
|       |   pom.xml
|       |
|       \—src
|           +—main
|           |   \—java
|           |       \—org
|           |           \—persistentsas
|           |               \—mavenTraining
|           |                       HelloWorld3.java
|           |
|           \—test
|               \—java
|                   \—org
|                       \—persistentsas
|                           \—mavenTraining
|                                   HelloWorld3Test.java
|
\—src
\—site
|   site.xml
|
\—markdown
|   index.md
|
\—training
Training.md

Now, let describe what are the benefit of such layout.

  1. You have all items related to a project, inside a project (pom, source code, test code, resources)
  2. You are able to share common build related configuration file using the build-tools repository
  3. you have several pom to make a hierarchical build, let’s call them aggregator pom for the moment
  4. you have “master” pom at the root of the project, that allow you to easily build, tests all your projects, but all to run command on a dedicated module.
  5. you do not duplicated informations inside build (versionning, behavior, …) using pomParent mechanism.

We’ll see in the next posts, how these functionalities works.

Advertisements