With the advent of Maven, many companies will use Maven to build projects, just projects

It does not use multiple projects to build, so that in the future, when the project becomes bigger and more business, the project will be difficult to maintain, which will increase the maintenance cost and decrease the team morale

And so on, using Maven to build multiple projects is the trend today

Here is an example of maven project interdependence, high hand tap, everyone learn from each other, please point out any shortcomings

1. Create the Demo-parent project as the parent project

Choose the quickstart

Change jar in POM to POM. This means that the current project is the parent project, and a child project can be created under it

 

2. Create demo-project-Web. As a Web project, this project is the general Web project, and the following two sub-Web projects will be included

Select WebApp as the Web project

 

3. Similarly, create demo-project-Customer-Web as a sub-Web project (WebApp). This project is an independent Web project, including Service and DAO

4. Create demo-project-gods- Web as a sub-Web project (webApp). This project will have a demo-project-gods-core as the background.

5. Create demo-project-gods-core as a sub-project (QuickStart) and rely on Demo-project-gods – Web

6. Create demo-common as a sub-project (QuickStart), which acts as a toolkit and can be relied upon by other projects

3-6 The steps are omitted. The generated project is shown as follows:

The parent project POM will look like this:

1 <modules>
2       <module>demo-project-web</module>
3       <module>demo-project-gods-web</module>
4       <module>demo-project-customer-web</module>
5       <module>demo-project-gods-core</module>
6       <module>demo-common</module>
7   </modules>Copy the code

 

Ok, so now let’s do dependencies between projects

First, demo-project-Web is a top-level Web project, where all subweb projects are packaged and integrated (if the file with the same name is overwritten by the top-level, if the top-level doesn’t have a file with the same name, but the subweb has a file with the same name, it is overwritten by the first dependent file in the order of dependency).

Pom is as follows:

1 <? The XML version = "1.0"? < > 2 project 3 xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 4 XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" > 5 <modelVersion>4.0.0</modelVersion> 6 7 <parent> 8 <groupId>com.demo</groupId> 9 <artifactId>demo-parent</artifactId> 10 <version>0.0.1-SNAPSHOT</version> 11 </parent> 12 13 <groupId>com.demo</groupId> 14 <artifactId>demo-project-web</artifactId> 15 <version>0.0.1-SNAPSHOT</version> 16 <packaging>war</packaging> 17 <name>demo-project-web Maven Webapp</name> 18 <url>http://maven.apache.org</url> 19 20 <dependencies> 21 22 <! -- Add war package dependencies to merge --> 23 <dependency> 24 <groupId>com.demo</groupId> 25 <artifactId>demo-project-gods-web</artifactId> 26 <version>0.0.1-SNAPSHOT</version> 27 <type>war</type> 28 </dependency> 29 <dependency> 30 <groupId>com.demo</groupId> 31 <artifactId>demo-project-customer-web</artifactId> 32 <version>0.0.1-SNAPSHOT</version> 33 <type>war</type> 34 </dependency> 35 36 <dependency> 37 <groupId>junit</groupId> 38 <artifactId>junit</artifactId> 39 <version>3.8.1</version> 40 <scope>test</scope> 41 </dependency> 42 43 </dependencies> 44 45 <build> 46 47 <finalName>demo-project-web</finalName> 48 49 <plugins> 50 51 <plugin> 52 <groupId>org.apache.maven.plugins</groupId> 53 <artifactId> Maven-war-plugin </artifactId> 54 <version>2.6</version> 55 <configuration> 56 <! Add the other two Web subprojects Demo </groupId> 60 <artifactId>demo-project-gods-web</artifactId> 61  </overlay> 62 <overlay> 63 <groupId>com.demo</groupId> 64 <artifactId>demo-project-customer-web</artifactId> 65 </overlay> 66 </overlays> 67 </configuration> 68 </plugin> 69 70 </plugins> 71 </build> 72 73 </project>Copy the code

 

Modified poM for Demo-project-Gods – Web to add its own dependency on Core as service and DAO

1 <? The XML version = "1.0"? < > 2 project 3 xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 4 XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" > 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>com.demo</groupId> 8 <artifactId>demo-parent</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 </parent> 11 <groupId>com.demo</groupId> 12 <artifactId>demo-project-gods-web</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <packaging>war</packaging> 15 <name>demo-project-gods-web Maven Webapp</name> 16 <url>http://maven.apache.org</url> 17 18 <dependencies> 19 20 <! -- Introducing dependencies, -> 21 <dependency> 22 <groupId>com.demo</groupId> 23 <artifactId>demo-project-gods-core</artifactId> 24 <version>0.0.1-SNAPSHOT</version> 25 </dependency> 26 27 <dependency> 28 <groupId>junit</groupId> 29 <artifactId>junit</artifactId> 30 <version>3.8.1</version> 31 <scope>test</scope> 32 </dependency> 33 34 </dependencies> 35 36 <build> 37 <finalName>demo-project-gods-web</finalName> 38 </build> 39 </project>Copy the code

Add a utility class dependency in Customer

1 <? The XML version = "1.0"? < > 2 project 3 xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 4 XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" > 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>com.demo</groupId> 8 <artifactId>demo-parent</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 </parent> 11 <groupId>com.demo</groupId> 12 <artifactId>demo-project-customer-web</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <packaging> WAR </packaging> 15  <name>demo-project-customer-web Maven Webapp</name> 16 <url>http://maven.apache.org</url> 17 <dependencies> 18 19 <! Demo </groupId> 22 <artifactId>demo-common</artifactId> 23 <version>0.0.1-SNAPSHOT</version> 24 </dependency> 25 26 <dependency> 27 <groupId>junit</groupId> 28 <artifactId>junit</artifactId> 29 <version>3.8.1</version> 30 <scope>test</scope> 31 </dependency> 32 33 </dependency>  34 <build> 35 <finalName>demo-project-customer-web</finalName> 36 </build> 37 </project>Copy the code

 

============ Enter the code phase ============

Parent is a parent project that doesn’t need to be coded

Demo – common:

1 package org.demo.common; 2 3 import java.util.Date; 4 5 public class DateUtil { 6 7 public static void showTime(String name) { 8 System.out.println("Run in demo-common - Hello ~ " + name + ", it is " + new Date().toString()); 9} 10 11}Copy the code

Demo – project – the customer – the web:

1 package com.demo.project.customer.web; 2 3 import org.demo.common.DateUtil; 4 5 public class AbcService { 6 7 public static void getDateService(String name) { 8 DateUtil.showTime(name); 9 System.out.println("Run in demo-project-customer-web... "); 10} 11 12}Copy the code

customer.jsp

1 <? XML version="1.0 "encoding="UTF-8"? > 2 <%@page import="com.demo.project.customer.web.AbcService" %> 3 <%@ page language ="java" contentType="text/html; charset=UTF-8 " 4 pageEncoding= "UTF-8"%> 5 <! PUBLIC DOCTYPE HTML "- / / / / W3C DTD XHTML 1.0 Transitional / / EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >  6 <html xmlns ="http://www.w3.org/1999/xhtml" 7 xmlns:f="http://java.sun.com/jsf/core" 8 xmlns:h="http://java.sun.com/jsf/html"> 9 <head> 10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 11 < title>Insert title here</title> 12 </head> 13 <body> 14 <% 15 System.out.print( "hello demo-project-customer-web customer.jsp" ); 16 AbcService.getDateService(" abc"); 17 %> 18 19 </body > 20 </html>Copy the code

 

The demo project – gods – the core:

1 package org.demo.project.gods.core; 2 3 4 public class Gods { 5 6 public static void getDateService(String name) { 7 System.out.println("Run in demo-project-gods-core..." ); 8} 9 10}Copy the code

demo-project-gods-web

index.jsp

1 <? The XML version = "1.0" encoding = "utf-8"? > 2 <%@page import="org.demo.project.gods.core.Gods"%> 3 <%@ page language="java" contentType="text/html; charset=UTF-8" 4 pageEncoding="UTF-8"%> 5 <! PUBLIC DOCTYPE HTML "- / / / / W3C DTD XHTML 1.0 Transitional / / EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > 6 <html xmlns="http://www.w3.org/1999/xhtml" 7 xmlns:f="http://java.sun.com/jsf/core" 8 xmlns:h="http://java.sun.com/jsf/html"> 9 <head> 10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 11 <title>Insert title here</title> 12 </head> 13 <body> 14 15 <% 16 System.out.print("hello demo-project-gods-web gods.jsp"); 17 Gods.getDateService("nathan"); 18 19 %> 20 </body> 21 </html>Copy the code

 

run – mvn install:

 

If the operation succeeds, the corresponding page is displayed