Quantcast
Channel: Java Team at Kentor » Frameworks
Viewing all articles
Browse latest Browse all 26

Maven Multi Module Design

$
0
0

Go back to Index

This is a follow up from architecture-and-design

Before we start, i should let you know that i have made some changes to the packages. This is to move away from a package structure that looks like a tutorial. Soon we will have a complete product. And i noticed that i made a couple of minor errors that i have named some of my classes as DAOs where they should have been named DTOs.

Lets go to our file system, we are going to start blank, so create a new folder, i called my “tutorial-multi”
in that folder i created six(6) subfolder

base
common
domain
integration-test
service
webapp

in common, domain, integration-test and service i created two (2) subfolders $name-api and $name-impl and in webapp i only created one (1) subfolder $name-impl
Replace $name with the corresponding parent folder name we now have something like this…

domain
domain\domain-api
domain\domain-impl

Now we need to create the pom files for all these modules.
1. Go to base, create a new text file called pom.xml add the following.

<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;

<modelVersion>4.0.0</modelVersion>

<groupId>com.bekkestad</groupId>
<artifactId>tutorial-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>tutorial-base</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

2. Go to common and then common-api and create a new folder called api and a pom.xml file

in the pom.xml file add the following
<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.bekkestad</groupId>
<artifactId>tutorial-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../base/pom.xml</relativePath>
</parent>

<groupId>com.bekkestad.common</groupId>
<artifactId>common-api-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>${product}-${project.artifactId}</name>
<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<modules>
<module>api</module>
</modules>

</project>

3. Go to the api folder and create a pom.xml file and add the following.

<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.bekkestad.common</groupId>
<artifactId>common-api-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>com.bekkestad.common</groupId>
<artifactId>common-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>3.0.2.Final</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>

4. Go two level up and go into common-impl and create a new folder called common and a pom.xml file, in the pom.xml file add the following

<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.bekkestad</groupId>
<artifactId>tutorial-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../base/pom.xml</relativePath>
</parent>

<groupId>com.bekkestad.common</groupId>
<artifactId>common-impl-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>${product}-${project.artifactId}</name>
<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<modules>
<module>common</module>
</modules>

</project>

5. Go to the common folder and create a new pom.xml file and add the following.

<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.bekkestad.common</groupId>
<artifactId>common-impl-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>com.bekkestad.common</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<repositories>
<repository>
<id>jboss-public-repository</id>
<name>JBoss Repository</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url&gt;
<!– These optional flags are designed to speed up your builds
by reducing remote server calls –>
<releases>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common-api</artifactId>
</dependency>
</dependencies>

</project>

We need to do this for all our folder. This is how we are going to define our inherited dependencies.
The idea here is that we have a common-api that should be seen by everything. Then you want to only expose your api’s. You should not be able to directly access an impl module. It should only go through the interfaces in the api modules.

Get the complete code from GitHub and inspect all the pom files. This would give you a better understanding on how this should look.

Go back to Index



Viewing all articles
Browse latest Browse all 26

Trending Articles