Enterprise projects will have multi maven modules.
Step 1: Create simple-app-parent is a package of type “pom”
1 2 3 | mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root \ -DarchetypeVersion=RELEASE -DgroupId=com.mytutorial -DartifactId=simple-app-parent -Dpackage=pom |
press “Y” for all the questions.
Step 2: cd into your newly created root dir “simple-app-parent”
1 | cd simple-app-parent |
Step 3: Create a child module “simple-app-service”, which is a packaging type of “jar”
1 2 3 | mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart \ -DarchetypeVersion=RELEASE -DgroupId=com.mytutorial -DartifactId=simple-app-service -Dpackage=jar |
Step 4: Create a child module “simple-app-endpoint”, which is a packaging type of “war”
1 2 3 | mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=RELEASE \ -DgroupId=com.mytutorial -DartifactId=simple-app-endpoint -Dpackage=war |
Take note of the different archetypes and packaging types used.
Importing into Eclipse IDE
Step 1: File –> Import –> Existing Maven Project. Select the “simple-app-parent”.
Step 2: You should now have a project structure as shown below.
Inspect the pom.xml files created. The simple-app-parent/pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mytutorial</groupId> <artifactId>simple-app-parent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>simple-app-parent</name> <modules> <module>simple-app-service</module> <module>simple-app-endpoint</module> </modules> </project> |
The simple-app-service/pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.mytutorial</groupId> <artifactId>simple-app-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.mytutorial</groupId> <artifactId>simple-app-service</artifactId> <version>1.0-SNAPSHOT</version> <name>simple-app-service</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> |
The simple-app-endpoint/pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.mytutorial</groupId> <artifactId>simple-app-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.mytutorial</groupId> <artifactId>simple-app-endpoint</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>simple-app-endpoint Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>simple-app-endpoint</finalName> </build> </project> |
Now you want the simple-app-endpoint.war to include simple-app-service-1.0-SNAPSHOT.jar to be included in WEB-INF/lib. So, you need to add the following dependency to your “simple-app-endpoint/pom.xml”
1 2 3 4 5 6 | <dependency> <groupId>com.mytutorial</groupId> <artifactId>simple-app-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> |
After adding this, if you run “mvn clean install” you will get: