(转)在Eclipse中创建Maven多模块工程
背景:以前只总结了怎么在命令行下创建maven的多模块项目,在eclipse下怎么创建不是很清楚。最近需要在git的资源库中上传多模块项目,方便后期的维护,所以将网上的资料进行整理。
原文链接:http://blog.csdn.net/leipeng321123/article/details/50995736
模块结构
在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层)、dao(数据库访问层)、service(业务逻辑层)、web(表现层),这样分层之后,各个层之间的职责会比较明确,后期维护起来也相对比较容易,今天我们就是使用Maven来构建分模块项目。
在这个分模块项目中主要分为两个模块,Service模块和web模块,Service模块中包含dao,下面是项目结构
Complaints-parent
|----pom.xml(pom)
|----Complaints-web
|----pom.xml (war)
|----Complaints-service
|----pom.xml(jar)
Complaints-parent是父工程,同时承担聚合模块和父模块的作用,没有实际代码和资源文件;
Complaints-web是web工程,最终形成最终的war包;
Complaints-service是业务模块的逻辑部分,包含了数据库访问层和业务逻辑层,但是不包括web相关的部分;
上述简单示意图中,有一个父项目(Complaints-parent)聚合很多子项目(Complaints-web,Complaints-service)。每个项目,不管是父子,都含有一个pom.xml文件。而且要注意的是,小括号中标出了每个项目的打包类型。父项目是pom,也只能是pom。子项目有jar,或者war。
依赖关系是:Complaints-web需要依赖Complaints-service
接下来就是在Eclipse中搭建项目了:
1.父项目(Complaints-parent)
在Eclipse里面New
-> Maven Project;
在弹出界面中选择“Create
a simple project”
父项目建好之后,目录下面只有一个src和pom.xml文件
将src文件夹删除,然后修改pom.xml文件,将<packaging>jar</packaging>修改为<packaging>pom</packaging>,
pom表示它是一个被继承的模块。(如果再创建项目时已经选为了pom就不需要修改)
以下是父项目的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hrtel</groupId>
<artifactId>Complaints-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Complaints-parent</name>
<modules>
<module>Complaints-service</module>
<module>Complaints-web</module>
</modules> <dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.创建Complaints-service项目
选中刚建的父项目,在弹出菜单中点击 New -> Maven Module;
使用默认的Archetype(默认:GroupId:org.apache.maven.archetypes,Artifact Id:maven-archetype-quickstart)
这样一个子项目就创建完成了,在文件系统中,子项目会建在父项目的目录中。
细心一点会发现,此时父项目的pom.xml文件中就多了一句
<modules>
<module>Complaints-service</module>
</modules>
3.创建web工程Complaints-web
选中刚建的父项目,在弹出菜单中点击 New -> Maven Module;
创建完之后就会发现父项目的pom.xml文件中又自动多了一句
<modules>
<module>Complaints-service</module>
<module>Complaints-web</module>
</modules>
4.优化配置
Complaints-web是依赖service项目的
需要在web的pom.xml中添加依赖配置
<dependency>
<groupId>com.hrtel</groupId>
<artifactId>Complaints-service</artifactId>
<version>${project.version}</version>
</dependency>
按上面步骤创建的子项目,在pom.xml中有个parent节点,所以,他可以继承父项目的相关信息。没错,父子项目中存在继承关系。
在子项目的pom.xml中,子项目的groupId和version一般和父项目相同,那么可以把子项目的这两个参数删除,这样会自动继承父项目的取值。
同样,如果其他的一些属性,所有子项目都是一样的,那么可以上移到父项目中设置,子项目中无需重复设置。比如:<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>可以仅在父项目中设置一次。
Manen提供dependencyManagement和pluginManagement两个标签。使用这两个标签,可以在父项目中统一管理依赖和插件的配置参数,比如版本号啥的。而在子项目中,仅需列出需要使用的依赖和插件的groupId和artifactId就可以了,其他信息会自动从父项目管理的信息里面获取。
在父项目中:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies></dependencyManagement>
在子项目中
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
由于web项目依赖service项目,子项目中只需service中添加配置,web项目会自动加载过去的
最后是打包配置:
打包时需要把service项目中的xml文件和别的property文件也打进去,就得在pom文件中说明
<!-- 打包时把需要的xml文件一块打进去 -->
<build>
<finalName>Complaints-service</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
以下是三个项目的pom.xml中的全部内容
Complaints-parent
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hrtel</groupId>
<artifactId>Complaints-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Complaints-parent</name>
<modules>
<module>Complaints-service</module>
<module>Complaints-web</module>
</modules> <dependencyManagement>
<dependencies>
<span style="white-space:pre"> </span><dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Complaints-service:
<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hrtel</groupId>
<artifactId>Complaints-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <artifactId>Complaints-service</artifactId>
<name>Complaints-service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 打包时把需要的xml文件一块打进去 -->
<build>
<finalName>Complaints-service</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
<!-- 依赖配置 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- spring beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>
</project>
Complaints-web
<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hrtel</groupId>
<artifactId>Complaints-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <artifactId>Complaints-web</artifactId>
<packaging>war</packaging>
<name>Complaints-web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.hrtel</groupId>
<artifactId>Complaints-service</artifactId>
<version>${project.version}</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency> </dependencies>
<build>
<finalName>Complaints-web</finalName>
</build>
</project>
(转)在Eclipse中创建Maven多模块工程的更多相关文章
- 在Eclipse中创建Maven多模块工程的例子
.配置eclipse的maven .在Eclipse里面New -> Maven Project -> 选择“Create a simple project” Group Id: com. ...
- Eclipse中创建Maven多模块工程
1.先创建父项目 在Eclipse里面New -> Maven Project: 在弹出界面中选择“Create a simple project” 这样,我们就按常规模版创建了一个Maven工 ...
- 在Eclipse中创建Maven多模块项目
在Eclipse中创建Maven多模块项目1,创建多模块项目选择File>New>Project,打开New Project窗口,选择Maven>Maven Project,选择下一 ...
- (3)Maven快速入门_3在Eclipse中创建Maven项目打包成jar
Eclipse中创建Maven项目 new ---> maven project ----> next 如下 普通java项目 选择 如下 quickstart 创建项目 : 输入 G ...
- Java归去来第4集:java实战之Eclipse中创建Maven类型的SSM项目
一.前言 如果还不了解剧情,请返回第3集的剧情 Java归去来第3集:Eclipse中给动态模块升级 二.在Eclipse中创建Maven类型的SSM项目 2.1:SSM简介 SSM ...
- 在eclipse中创建maven webapp项目时弹出错误-解决办法
在eclipse中创建maven webapp项目时报错: Could not resolve archetype org.apache.maven.archetypes:maven-archetyp ...
- Eclipse中创建Maven项目失败
Eclipse中创建Maven项目报错:Unable to create project from archetype org.apache.maven.archetypes:maven-archet ...
- 在IntelliJ IDEA中创建Maven多模块项目
在IntelliJ IDEA中创建Maven多模块项目 1,创建多模块项目选择File>New>Project 出现New Project窗口左侧导航选择Maven,勾选右侧的Create ...
- 在eclipse中使用Maven分模块搭建SSM框架,创建jar、war、pom工程模块教学,项目的热部署,需要的可以留下邮箱,给大家发整理过的Maven笔记
第一章:Maven概述 (1)Maven是一个项目构建工具,由apache提供,用Java开发的 (2)构建工具:Ant(蚂蚁),Maven(专家) (3)构建工具作用:帮助程序员进行项目的创建,目录 ...
随机推荐
- Linux umask
新建一个文件或目录,它的默认权限是什么?如果要修改一个用户创建的文件和目录的默认权限该如何做?本文将介绍相关的内容.说明:本文的演示环境为 ubuntu 16.04. 文件的默认权限 为了查看用户创建 ...
- 从零开始搭建属于你的React/redux/webpack脚手架
大家好,我是苏南,今天要给大家分享的是<<我的react入门到放弃之路>>,当然,也不是真的放弃啦--哈哈,这篇博客原本是从17年初写的,一直没有在csdn发布,希望今天不会太 ...
- 【亲测有效】Github无法访问或者访问速度的解决方案
我相信,很多朋友都遇到了 Github 访问速度过慢的问题,我也是在此记下笔记,方便以后拿来使用. 第一步.修改Hosts 通过问题的搜索了解到 github 访问很慢一般通过修改 hosts 文件解 ...
- ireportdesigner下载页面
iReport主页:http://community.jaspersoft.com/project/ireport-designer iReport下载地址:http://sourceforge.ne ...
- 1013 C. Photo of The Sky
传送门 [http://codeforces.com/contest/1013/problem/C] 题意 输入一个n代表n颗星星,输入2n个数,其中任意两个数代表一颗行星的坐标,问你把n个星星围起来 ...
- 20135327郭皓--Linux内核分析第七周 可执行程序的装载
第七周 可执行程序的装载 郭皓 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-1000029000 ...
- Oracle的安装与配置
好久不来博客园了,有种熟悉而又陌生的感觉. 今天我装一下Oracle数据库,从头开始,因为昨天在虚拟机装了,不能用,卸掉了,系统也卸掉了,今天重新装,包括系统. 系统装好了,Oracle准备好了. 这 ...
- 推广App篇
推广App篇 团队github地址:https://github.com/ouqifeng/EasyGoOperation.git 在完成该项目工程后,我们开始寻找合适的方法推广我们的软件. 经过一番 ...
- Linux MYSQL:dead but pid file exists
MYSQL dead but pid file exists问题 - CSDN博客https://blog.csdn.net/shilian_h/article/details/38020567 Er ...
- 【转】Linux tail 命令详解
Linux tail 命令详解 http://www.2cto.com/os/201111/110143.html