zl程序教程

您现在的位置是:首页 >  后端

当前栏目

maven搭建多模块项目和管理

Maven项目模块 管理 搭建
2023-09-14 09:04:36 时间
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"   
2.1 将项目B中的src文件删除(可有可无,主要看个人需要)
2.2 选中项目B,点击右键,选择NEW -- project-- maven-- maven Module,点击下一步,在出现的界面中输入子模块的名称C,点击下一步,出现Select an Archetype界面。这时选择maven-Archetype-site-quickStart或者maven-Archetype-webapp(构建web层时使用),然后选择完成,即生成子项目C。

这时B的pom文件就变成了这样(和上面的比只是多了个modules标签):


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"   
其实在构建多模块的项目时,在选择父项目的类型时是可以选择任意的类型。选择项目类型的界面如下:

对于这些非常规类型的项目,如果想要成为父项目,需要做如下一些改动:

1.将pom.xml文件中的 packaging jar /packaging 改为 packaging pom /packaging ,若不换为pom则在打包的时候父项目会产生一个target文件;如果没有 packaging pom /packaging 的可以在 version 0.0.1-SNAPSHOT /version 后面添加上这句话即可。

2.删除除pom.xml外的所有文件,其中JRE System Library是不能直接删除的,需要选择中,并单击右键Bulid Path-- Remove from bilud path即可移除。

这时的父项目就显得很干净整洁了。


其实在普通的项目上是不允许构建子模块的,能否在一个项目下创建子模块,主要取决于是否有 packaging pom /packaging 这个配置。如果有这个配置则任何项目都可以创建子模块,硬要这样创建的话整个项目会变的不伦不类。看来maven在判断一个项目是否可以创建子模块其实是取决于这个配置的。





参考二:

首先,前面几次学习已经学会了安装maven,如何创建maven项目等,最近的学习,终于有点进展了,搭建一下企业级多模块项目。

好了,废话不多说,具体如下:

首先新建一个maven项目,pom.xml的文件如下:

搭建多模块项目,必须要有一个packaging为pom的根目录。创建好这个maven项目后,我们对着项目右键-- new

输入你的项目名称

这里就不重复说创建项目了,创建好的目录结构在eclipse中如下:

说明一下这些项目具体都是干嘛的:

easyframework-model:数据模型,与数据库表字段对应的实体类

easyframework-core:核心业务项目。主要是Service处理业务逻辑

easyframework-persist:数据持久层,操作低层数据库。

easyframework-utils:工具类,所有工具类都提取出来写在这个项目中。

easyframework-web :这个就是整个项目的web层了,页面的显示以及控制层

备注:创建这些项目的时候,只有easyframework-web是web项目即maven的:maven-archetype-webapp,其他的都是java项目:maven-archetype-quicktart

打开easyframework-root的pom.xml文件,你会看到模块化是这样的:

接下来是配置各个模块的依赖关系,我个人认为的项目是这样依赖的,不知道对不对,呵呵....

举个例子easyframework-web这个项目依赖easyframework-core(业务核心)和easyframework-model(实体类),easyframework-utils(公共的工具类)这个三个模块。

那么在怎么在easyframework-web的pom.xml中体现呢,具体如下:

 

打开项目的maven依赖你会发现,已经依赖了这三个项目

但是你应该会感觉到奇怪,为什么会有那么jar包,明明只引用了这三个项目,哪来的那么多jar包。

你会发现,我再pom.xml文件中,有个parent节点,继承了根节点的pom,这就是maven的项目继承依赖,会从父POM中继承一些值。这对构建一个大型的系统来说很有必要

这样的话你就不需要一遍又一遍的重复添加同样的依赖元素,当然,如果你在子项目中也有同样的依赖,则会覆盖父POM中的值。

父POM的的依赖如下:

复制代码
 1 project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" 

 3 modelVersion 4.0.0 /modelVersion 

 4 groupId com.easyframework /groupId 

 5 artifactId easyframework-root /artifactId 

 6 packaging pom /packaging 

 7 version 1.0 /version 

 8 name easyframework-root /name 

 9 url http://maven.apache.org /url 

 10 modules 

 11 module easyframework-web /module 

 12 module easyframework-persist /module 

 13 module easyframework-core /module 

 14 module easyframework-utils /module 

 15 module easyframework-model /module 

 16 /modules 

 17 properties 

 18 !--指定Maven用什么编码来读取源码及文档 -- 

 19 project.build.sourceEncoding UTF-8 /project.build.sourceEncoding 

 20 !--指定Maven用什么编码来呈现站点的HTML文件 -- 

 21 project.reporting.outputEncoding UTF-8 /project.reporting.outputEncoding 

 22 mysql.version 5.1.25 /mysql.version 

 23 hibernate.version 4.2.2.Final /hibernate.version 

 24 spring.version 3.2.3.RELEASE /spring.version 

 25 aspectj.version 1.7.2 /aspectj.version 

 26 /properties 

 27 repositories 

 28 repository 

 29 id springsource-repo /id 

 30 name SpringSource Repository /name 

 31 url http://repo.springsource.org/release /url 

 32 /repository 

 33 /repositories 

 34 dependencies 

 36 !-- log4j -- 

 37 dependency 

 38 groupId log4j /groupId 

 39 artifactId log4j /artifactId 

 40 version 1.2.17 /version 

 41 /dependency 

 42 !-- junit -- 

 43 dependency 

 44 groupId junit /groupId 

 45 artifactId junit /artifactId 

 46 version 4.11 /version 

 47 scope test /scope 

 48 /dependency 

 49 !-- mysql数据库驱动 -- 

 50 dependency 

 51 groupId mysql /groupId 

 52 artifactId mysql-connector-java /artifactId 

 53 version ${mysql.version} /version 

 54 /dependency 

 55 !-- hibernate4 -- 

 56 dependency 

 57 groupId org.hibernate /groupId 

 58 artifactId hibernate-core /artifactId 

 59 version ${hibernate.version} /version 

 60 /dependency 

 61 !-- aspectjweaver -- 

 62 dependency 

 63 groupId org.aspectj /groupId 

 64 artifactId aspectjweaver /artifactId 

 65 version ${aspectj.version} /version 

 66 /dependency 

 67 !-- spring3 -- 

 68 dependency 

 69 groupId org.springframework /groupId 

 70 artifactId spring-core /artifactId 

 71 version ${spring.version} /version 

 72 /dependency 

 73 dependency 

 74 groupId org.springframework /groupId 

 75 artifactId spring-context /artifactId 

 76 version ${spring.version} /version 

 77 /dependency 

 78 dependency 

 79 groupId org.springframework /groupId 

 80 artifactId spring-jdbc /artifactId 

 81 version ${spring.version} /version 

 82 /dependency 

 83 dependency 

 84 groupId org.springframework /groupId 

 85 artifactId spring-beans /artifactId 

 86 version ${spring.version} /version 

 87 /dependency 

 88 dependency 

 89 groupId org.springframework /groupId 

 90 artifactId spring-web /artifactId 

 91 version ${spring.version} /version 

 92 /dependency 

 93 dependency 

 94 groupId org.springframework /groupId 

 95 artifactId spring-expression /artifactId 

 96 version ${spring.version} /version 

 97 /dependency 

 98 dependency 

 99 groupId org.springframework /groupId 

100 artifactId spring-orm /artifactId 

101 version ${spring.version} /version 

102 /dependency 

103 /dependencies 

104 build 

105 finalName easyframework-root /finalName 

106 plugins 

107 plugin 

108 artifactId maven-compiler-plugin /artifactId 

109 configuration 

110 source 1.6 /source 

111 target 1.6 /target 

112 /configuration 

113 /plugin 

114 /plugins 

115 /build 

116 /project 
复制代码 当然这个父POM只是一个例子,你可以根据自己的配置添加相关的依赖,这里给一个我认为是最好用的仓库:

http://mvnrepository.com/ 相信地球人都知道这个!哈哈.....

到此就搭建好了企业级多模块的项目环境了。