Modular maven project and maven

Source: Internet
Author: User

Modular maven project and maven
Preface

The modularization of the project facilitates the division of tasks, post-maintenance, and easy expansion. modules can also be independently deployed as services;

Create a parent project with the packaging type POM

The maven plug-in I used is m2e. I believe most people installed m2e plug-ins in eclipse. If you don't talk nonsense, start directly:

Select create maven project from the menu. Make sure to create a simple project, as shown in red circle. Because we want to create a maven project whose packaging type is pom, there seems to be no corresponding type in the built-in archetype, but I cannot find it anyway.

Click next, select pom in the packaging option, and enter necessary information, such:

At this time, our pom. xml file is like this:

<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>cn.edu.hdu.mm</groupId>  <artifactId>multi-module</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>pom</packaging>  <name>multi-module</name>  <description>multi module projects.</description></project>
Create sub-modules

Here we plan to create a web project as an example, which is divided by layer. In fact, projects with complex points can be divided into multiple subsystem modules and multiple service modules, communication between modules can be implemented through jms, webservice, and other methods.

So, let's start to simply create common, dao, service, and web modules. Here, besides the packaging type of the web module, the other modules are jar, that is, when creating these modules, the archetype type selected by the web module is webapp, And the quickstart option can be selected for other modules;

Right-click the parent project and choose create maven module;

After creating each module, the POM configuration file of the parent module and sub-module is as follows:

Parent module POM Configuration:

<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>cn.edu.hdu.mm</groupId>  <artifactId>multi-module</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>pom</packaging>  <name>multi-module</name>  <description>multi module projects.</description>  <modules>      <module>multi-module-common</module>      <module>multi-module-dao</module>      <module>multi-module-service</module>      <module>multi-module-web</module>  </modules></project>

One of the sub-modules is configured with POM:

<?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>cn.edu.hdu.mm</groupId>    <artifactId>multi-module</artifactId>    <version>0.0.1-SNAPSHOT</version>  </parent>  <artifactId>multi-module-common</artifactId>  <name>multi-module-common</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>
Configure dependencies between modules

In this demo, the dependencies between modules are as follows:

The web depends on service, the service depends on dao, And the dao depends on the common module;

It is very easy to configure the dependency. For example, we need to configure the dependency common module in the dao module. We only need to add the following code snippets in the pom, which is the same as adding the dependency at ordinary times:

    <dependency>        <groupId>${project.groupId}</groupId>        <artifactId>multi-module-common</artifactId>        <version>${project.version}</version>    </dependency>

Note that $ {project. groupId} and $ {project. version} refers to the groupId and version of this module, because the common parent module of this module and the multi-module-common module is multi-module, both groupId and version are inherited from multi-module. Therefore, you can directly write the groupId and version of this module.

Start Program Development

After completing the above configuration, you can start to develop our program, write some common things to the common module, access the data source from the dao module, and business requirements from the service module. You can write some jsp codes on the web, action, controller, etc;

Here, I simply wrote a demo of spring + spring mvc + mybatis, and added a user addition, deletion, modification, and query module. The front-end page is not written ~ For the code, see the final link.

The project directory is roughly as follows:

Run war package, debug, etc.

After the code is written, add the database (the script is in the project), and then run the script in the pom of the Parent Project. add tomcat7-maven-plugin plug-in configuration in xml file, right-click to execute tomcat: run Command, run war package, you can also right-click debug project, debug...

Run the command to view the following logs. You can view the specific execution process in the log:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] multi-module
[INFO] multi-module-common
[INFO] multi-module-dao
[INFO] multi-module-service
[INFO] multi-module-web Maven Webapp
[INFO]
[INFO] Using the builder org. apache. maven. lifecycle. internal. builder. singlethreaded. SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building multi-module 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> tomcat-maven-plugin: 1.1: run (default-cli) @ multi-module >>>
[INFO]
[INFO] <tomcat-maven-plugin: 1.1: run (default-cli) @ multi-module <
[INFO]
[INFO] --- tomcat-maven-plugin: 1.1: run (default-cli) @ multi-module ---
[INFO] Skipping non-war project
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building multi-module-common 0.0.1-SNAPSHOT ............................ ..............

After the command is executed, enter http: // localhost: 8080/multi-module-web/user/getUserList. do? In the browser address? PageNo = 0 & pageSize = 5, access controller, access successful:

Demo source code

The following is the code, which is simply written for reference only.

Https://github.com/peterchenhdu/Demos/tree/master/multi-module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.