7 create a multi-module project in Eclipse
A multi-module project is, as its name suggests, a project that consists of multiple modules, where a module is a project. You can think of a multi-module project as a logical grouping of sub-projects. The packaging of a multi-module project is “pom”, since it consists of a pom.xml file but no artifact (jar, war, ear, etc).
The typical “maven” way to organize multi-module projects is to store them hierarchically, where your modules exist within your multi-module project. Your modules may even have more modules within them. However, the “Eclipse” way to organize projects is in a flat manner, where each module is a project located at the root level of your workspace.
In general, I prefer to do things the “maven” way, but as for project layouts, I prefer to do things the “Eclipse” way and use a flat structure where all projects are located at the same level and not nested within each other. I prefer the “Eclipse” way because it allows me to easily debug across modules when modules are dependent on other modules.
In my myproject pom.xml file, I set the packaging to “pom”. Next, I listed the modules as “../myapp_presentation”, “../myapp_service” and “../myapp_integration”. The “../” is used since the modules are Eclipse projects that are located at the same level as the “myproject” project. After this, I added commons io dependency to the “myproject” pom.xml file, since the commons io dependency is a common dependency shared by all modules. The “../myapp_presentation”, “../myapp_service” and “../myapp_integration” projects will now get the commons io dependency through inheritance.
Right click on the parent Project ie myproject ,create an maven module named myapp_integration and myappservice by following the below snapshot.
Create another maven module myapp_presentation as shown below
enter the filter javee6 and select select the artifactid webapp-javaee6 .click Next
change the package name.click Finish.
Myapp_presentation/pom.xml
<?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> <parent> <artifactId>myproject</artifactId> <groupId>in.com.pradeepit</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>myapp_presrentation</artifactId> <packaging>war</packaging> <name>myapp_presrentation</name> <properties> <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> <compilerArguments> <endorseddirs>${endorsed.dir}</endorseddirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
Notice each module pom.xml file contains parent tag ,which specifies parent project.
considering presentation module depends on service module ,add service module dependency to presenatation.
Updated pom.xml file .
/myapp_presentation/pom.xml
<?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> <parent> <artifactId>myproject</artifactId> <groupId>in.com.pradeepit</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>myapp_presentation</artifactId> <packaging>war</packaging> <name>myapp_presentation</name> <properties> <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>in.com.pradeepit</groupId> <artifactId>myapp_service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> <compilerArguments> <endorseddirs>${endorsed.dir}</endorseddirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
similarly service module depends on intregration module ,add integration module dependency to service module.
/myapp_service/pom.xml
<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> <parent> <groupId>in.com.pradeepit</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>myapp_service</artifactId> <dependencies> <dependency> <groupId>in.com.pradeepit</groupId> <artifactId>myapp_integration</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
Create a TestServlet class in presentation module and create reference of an service module ,in service module create reference of an integration module .
/myapp_service/src/main/java/in/com/pradeepit/service/TestService.java
package in.com.pradeepit.service; import in.com.pradeepit.dao.TestDao; public class TestService { private TestDao testDao; } |
/myapp_presentation/src/main/java/in/com/pradeepit/servlet/TestServlet.java
package in.com.pradeepit.servlet; import in.com.pradeepit.service.TestService; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestServlet */ @WebServlet("/TestServlet") public class TestServlet extends HttpServlet { private TestService testService; private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public TestServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
Create a common dependency in parent module myproject,this dependency will be available to all the submodules .
/myproject/pom.xml
<?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>in.com.pradeepit</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>myapp_integration</module> <module>myapp_service</module> <module>myapp_presentation</module> </modules> <dependencies> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>20030203.000550</version> </dependency> </dependencies> </project> |
0 responses on "Create a Multi-Module Project in Eclipse"