maven multi modules setup

This is a very common use case and it took me some time to figure everything out. Consider we have a Maven project with 2 modules and both modules need to have access to the same database with the same domain models. Since we do not want to write these models multiple times lets create a 3rd module for them.

Setup

We want a project setup like this.

pom.xml
--> /WebProject
		pom.xml
--> /WorkerProject
		pom.xml
--> /ModelProject
		pom.xml

parentModule

Our parent pom.xml keeps most of the dependencies and loads the modules.

<groupId>cc</groupId>
<artifactId>multiModule</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
	<module>ModelProject</module>
	<module>WebProject</module>
	<module>WorkerProject</module>
</modules>

<properties>
	<jdk.version>1.8</jdk.version>
</properties>

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>3.8.1</version>
		<scope>test</scope>
	</dependency>
    ...
</dependencies>	

WebProject

<parent>
	<relativePath>../pom.xml</relativePath>
	<artifactId>multiModule</artifactId>
	<groupId>cc</groupId>
	<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>WebProject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>


<dependencies>
	<dependency>
		<groupId>DSE</groupId>
		<artifactId>model</artifactId>
		<version>1.0-SNAPSHOT</version>
	</dependency>
</dependencies>

WorkerProject

<parent>
	<relativePath>../pom.xml</relativePath>
	<artifactId>warteliste</artifactId>
	<groupId>DSE</groupId>
	<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>GeoResolver</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>
	<dependency>
		<groupId>DSE</groupId>
		<artifactId>model</artifactId>
		<version>1.0-SNAPSHOT</version>
	</dependency>
</dependencies>

ModelProject

<parent>
	<relativePath>../pom.xml</relativePath>
	<artifactId>multiModule</artifactId>
	<groupId>cc</groupId>
	<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>modelProject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
	<plugins>
		<plugin>
			<groupId>com.mysema.maven</groupId>
			<artifactId>apt-maven-plugin</artifactId>
			<version>1.1.1</version>
			<executions>
				<execution>
					<phase>generate-sources</phase>
					<goals>
						<goal>process</goal>
					</goals>
					<configuration>												<outputDirectory>target/generated-sources</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
					</configuration>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

So for the workerProject and webProject all that is necessary is to add the dependency for the modelProject module. Now your IDE should already be able to work with the domain models in all modules.

For the deployment there is one more step todo. The modelProject has to be packaged as jar, so the war modules can use it when getting deployed.