背景:以前只总结了怎么在命令行下创建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中,子项目的groupIdversion一般和父项目相同,那么可以把子项目的这两个参数删除,这样会自动继承父项目的取值。

同样,如果其他的一些属性,所有子项目都是一样的,那么可以上移到父项目中设置,子项目中无需重复设置。比如:<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>可以仅在父项目中设置一次。

Manen提供dependencyManagementpluginManagement两个标签。使用这两个标签,可以在父项目中统一管理依赖和插件的配置参数,比如版本号啥的。而在子项目中,仅需列出需要使用的依赖和插件的groupIdartifactId就可以了,其他信息会自动从父项目管理的信息里面获取。

在父项目中:

<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多模块工程的更多相关文章

  1. 在Eclipse中创建Maven多模块工程的例子

    .配置eclipse的maven .在Eclipse里面New -> Maven Project -> 选择“Create a simple project” Group Id: com. ...

  2. Eclipse中创建Maven多模块工程

    1.先创建父项目 在Eclipse里面New -> Maven Project: 在弹出界面中选择“Create a simple project” 这样,我们就按常规模版创建了一个Maven工 ...

  3. 在Eclipse中创建Maven多模块项目

    在Eclipse中创建Maven多模块项目1,创建多模块项目选择File>New>Project,打开New Project窗口,选择Maven>Maven Project,选择下一 ...

  4. (3)Maven快速入门_3在Eclipse中创建Maven项目打包成jar

    Eclipse中创建Maven项目 new ---> maven project  ----> next 如下 普通java项目 选择  如下 quickstart 创建项目 : 输入 G ...

  5. Java归去来第4集:java实战之Eclipse中创建Maven类型的SSM项目

    一.前言 如果还不了解剧情,请返回第3集的剧情          Java归去来第3集:Eclipse中给动态模块升级 二.在Eclipse中创建Maven类型的SSM项目 2.1:SSM简介 SSM ...

  6. 在eclipse中创建maven webapp项目时弹出错误-解决办法

    在eclipse中创建maven webapp项目时报错: Could not resolve archetype org.apache.maven.archetypes:maven-archetyp ...

  7. Eclipse中创建Maven项目失败

    Eclipse中创建Maven项目报错:Unable to create project from archetype org.apache.maven.archetypes:maven-archet ...

  8. 在IntelliJ IDEA中创建Maven多模块项目

    在IntelliJ IDEA中创建Maven多模块项目 1,创建多模块项目选择File>New>Project 出现New Project窗口左侧导航选择Maven,勾选右侧的Create ...

  9. 在eclipse中使用Maven分模块搭建SSM框架,创建jar、war、pom工程模块教学,项目的热部署,需要的可以留下邮箱,给大家发整理过的Maven笔记

    第一章:Maven概述 (1)Maven是一个项目构建工具,由apache提供,用Java开发的 (2)构建工具:Ant(蚂蚁),Maven(专家) (3)构建工具作用:帮助程序员进行项目的创建,目录 ...

随机推荐

  1. IIS_部署出错

    在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Fil ...

  2. 深入浅出Automation Anywhere

    Automation Anywhere是基于CLIENT-SERVER架构(control room和客户端),客户端主要是Bot Creator 和 BotRunner 主要构成: 1.WEBCR: ...

  3. tmux使用总结

    ctrl+b +%:增加垂直分屏 ctlr+b +左右箭头: 在垂直分屏中移动 ctrl+b+c:新建窗口(不分屏) ctrl+b+数字键: 切换窗口 ctrl+b+d: 断开窗口 tmux  a : ...

  4. 12.13 Daily Scrum

    现在已经可以实现在应用中直接通过WebView浏览餐厅的网页,而不用再调用手机的浏览器. 收藏夹的功能也基本实现,接下来的目标时将收藏夹与每一个用户关联.   Today's Task Tomorro ...

  5. Linux虚拟机下与主机通信

    1.更改虚拟机ip和主机ip同一网段 2.配置虚拟机的网络适配器 3.主机进行ping测试

  6. jmeter 使用csv文件 注意项

    1.首先在jmeter 中导入csv文件时我们程序并不知道csv文件中有多少行 : >1.获取的时候 使用 循环控制器来获取csv文件中的所有数据 : 通过 ${__jexl3("${ ...

  7. CentOS yum 安装获取原始rpm文件的方法

    1. 有时候 yum install 需要从几个repo下载rpm包速度很慢,不如自己能够将rpm包下载下来继续使用,比较好. 发现yum install 有两种方式能够将下载的rpm包保存下来. 方 ...

  8. linux学习之centos(三):mysql数据库的安装和配置

    前言:mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库, ...

  9. \r\n

    转载自http://www.studyofnet.com/news/285.html '\r'是回车,'\n'是换行,前者使光标到行首,后者使光标下移一格,通常敲一个回车键,即是回车,又是换行(\r\ ...

  10. Mysql误删表中数据与误删表的恢复方法

    由于头两天面试时被问了这样一个问题,如果某同事误删了某个表,你该怎么恢复? 当时想了一下,因为博主没有遇到过这个问题,但是也多少了解一些,所以就回答通过mysql的binlog日志进行恢复. 面试官当 ...