maven module创建多模块项目

     通常业务模块多,规模庞大的系统都是有多个团队协力开发的,每个开发团队开发划分好的的业务模块。但是各个模块之间还有可能有互相调用依赖的关系,maven的多module特性可以很好解决这个问题,更合理的组织各个业务模块的代码。下面就介绍下maven module项目的创建。

 

创建一个maven工程module_test,4个module,结构参考下面。

maven_module

├─dao

│  

├─service

│  

├─web

└─domain

1、New一个mavenproject,选择Create a simple project。



 

 

然后删除src和target目录,只保留pom.xml文件。注意下parentProject,packing 选择 POM

然后新建module(domain,dao,service,web),File→new→other→Maven→maven Module



 

module可以选择jar,war,ear等,根据项目需要和模块的特征自己选择。domain,dao,service 选择jar,web选择war。

注意module和parentproject的pom文件的变化。

parent  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>
  <groupId>com.test</groupId>
  <artifactId>maven_module</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
 <modules>
  	<module>domain</module>
  	<module>dao</module>
  	<module>service</module>
  	<module>web</module>
  </modules>
</project>

 domain 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>com.test</groupId>
    <artifactId>maven_module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>domain</artifactId>
</project>

dao 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>com.test</groupId>
    <artifactId>maven_module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>dao</artifactId>
  <dependencies>
  	<dependency>
  		<groupId>com.test</groupId>
  		<artifactId>domain</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  </dependencies>
</project>

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>com.test</groupId>
    <artifactId>maven_module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>service</artifactId>
  <dependencies>
  	<dependency>
  		<groupId>com.test</groupId>
  		<artifactId>dao</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  </dependencies>
</project>

web pom.xml

<?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.test</groupId>
    <artifactId>maven_module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>web</artifactId>
  <packaging>war</packaging>
  <name>web Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
    	<groupId>com.test</groupId>
    	<artifactId>service</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>web</finalName>
  </build>
</project>

每添加一个模块,父项目就会在modules 标签下自己插入一条,子模块自动引用父项目的定义parent标签。

子模块通过parent标签,可以继承父项目所有的配置依赖信息,比如依赖的jar,父项目定义后,子模块不需要再配置了,直接用就可以了。 模块之间也可以实现层级调用依赖。比如,dao层,依赖domain的代码,可以通过<dependency>配置引入这个模块,service需要依赖dao,domain,直接引入dao模块,web需要依赖dao,domain,service,直接引入service模块。由于层级依赖关系,dao层引入了domain,在service层就不需要再引入domain层,以此类推,web层只引入service层就行了,domain,dao,service自动会被引入。

这样架构的好处是,共通的配置放到parent的pom里面配置即可,子模块配置自己个性化的需求就可以了,各个模块功能独立,需要的话可以灵活互相调用,父工程可以灵活组织调度各个模块实现复杂的业务需求。

 

猜你喜欢

转载自renft.iteye.com/blog/2251424