Thursday, January 23, 2014

Complex application directory structure with MAVEN

Example of creating application directory structure for maven.
On that article I want to describe "mockup" of application: directory structure which I usually use for large and complex applications. It is main older with parent(root) pom.xml file and sub-directories with sub-projects(small application components). Sometimes it very good then application is decomposed and we can just take one of it components and use it in another project. So, let's start!

1. creating parent(main project)
For creating "root" project(it called mockup-root) folder we can use maven archetype generation by next command:

mvn archetype:generate -DgroupId=com.demien.mockup -DartifactId=mockup-root -DarchtypeArtifactId=maven-archtype-quickstart -DinteractiveMode=false

It will create folder mockup-root with maven directory structure and pom.xml file inside.
Now we have to make some improvements for generated files by
1.1 editing root pom.xml(in mockup-root directory) file
   1.1.1. Let's create properties tag(or just add "application.name" element if it exists):

   <properties>
     <application.name>Demien :: mockup </application.name>
   </properties>
   Now all child projects will be able to use that name. Later we will define here some useful information like "spring version", "junit version" etc.
   Using just created parameter we can re-write project name in such tag(<name>): <name>${application.name}</name>
 
   1.1.2. change project packaging.
   Root project will be empty because it's just "container" for child projects. So current packaging tag should be replaced to:

<packaging>pom</packaging>

2. creating "cmd"(in mockup-root directory) files for building application and generating eclipse project file. 
Now we can create very simple script for building our application. Unfortunately eclipse works very bad with maven, so if you use eclipse in development it's better to create also script for generating eclipse project files. Of course in scripts you have to use YOUR path to maven. Or, better, put maven directory into system "classpath" variable and exclude first line at all.
 2.1 file _quick_build.cmd:

set path=c:\java\apache-maven-3.1.1\bin
set MAVEN_OPTS=-Xmx512m -Xms256m -XX:MaxPermSize=128m
mvn clean package -e -Dmaven.test.skip=true

 2.2 file _quick_eclipse.cmd

set path=c:\java\apache-maven-3.1.1\bin
set MAVEN_OPTS=-Xmx512m -Xms256m -XX:MaxPermSize=128m
mvn eclipse:clean eclipse:eclipse -Dmaven.test.skip=true

   Now you can try to build your project by running _quick_build. In my case I have output with my changed application name:
   [INFO] Building Demien :: mockup 1.0-SNAPSHOT
 
 
3. creating childs(subprojects inside mockup-root directory): 
  Most interesting part: creating our sub-projects. Commands for creating child projects:

mvn archetype:generate -DgroupId=com.demien.mockup -DartifactId=mockup-core -DarchtypeArtifactId=maven-archtype-quickstart -DinteractiveMode=false
mvn archetype:generate -DgroupId=com.demien.mockup -DartifactId=mockup-domain -DarchtypeArtifactId=maven-archtype-quickstart -DinteractiveMode=false
mvn archetype:generate -DgroupId=com.demien.mockup -DartifactId=mockup-database -DarchtypeArtifactId=maven-archtype-quickstart -DinteractiveMode=false
mvn archetype:generate -DgroupId=com.demien.mockup -DartifactId=mockup-rest -DarchtypeArtifactId=maven-archtype-quickstart -DinteractiveMode=false
mvn archetype:generate -DgroupId=com.demien.mockup -DartifactId=mockup-webapp -DarchtypeArtifactId=maven-archtype-webapp -DinteractiveMode=false

 Of course, real count (and names) of child projets depends of your application.

 After executing described commands we will have next directory structure:
<DIR>          mockup-core
<DIR>          mockup-database
<DIR>          mockup-domain
<DIR>          mockup-rest
<DIR>          mockup-webapp
           996 pom.xml
<DIR>          src
           139 _quick_build.cmd
           152 _quick_eclipse.cmd

 src directory we can delete, because we not going to keep any files in root project.

 4. finalization
Now let's make some steps for maintain our application:
4.1. Child cross-dependencies.
 In child projects we have to add cross-subproject dependencies.
  For instance, if in webapp project use domain project, we have to add such dependency:

 <dependency>
   <groupId>com.demien.mockup</groupId>
      <artifactId>mockup-domain</artifactId>
      <version>1.0-SNAPSHOT</version>
 </dependency>

 4.2 Child names
 To make child names more "pretty" we can rename them this way:
 <name>${application.name} :: mockup-webapp</name>

 5. final build
 After all, when I tried to build my application, I got next listing :
[INFO] Reactor Summary:
[INFO]
[INFO] Demien :: mockup .................................. SUCCESS [0.236s]
[INFO] Demien :: mockup :: mockup-core ................... SUCCESS [2.024s]
[INFO] Demien :: mockup :: mockup-domain ................. SUCCESS [0.160s]
[INFO] Demien :: mockup :: mockup-database ............... SUCCESS [0.157s]
[INFO] Demien :: mockup :: mockup-rest ................... SUCCESS [0.165s]
[INFO] Demien :: mockup :: mockup-webapp ................. SUCCESS [0.184s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

You can download this example from here.

No comments:

Post a Comment