(转)在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 df du 命令
df 命令 检查磁盘空间占用情况(并不能查看某个目录占用的磁盘大小). 命令格式:df [option] -h 以容易理解的格式(给人看的格式)输出文件系统分区使用情况,例如 10kB.10MB.10 ...
- High-level structure of a simple compiler高級結構的簡單編譯器
1.lexical analysis,which analyzes the character string presented to it and divides it up into tokens ...
- 跨平台、跨语言应用开发,Elements 介绍
目录 1,Elements 介绍 2,Elements 版本 3,Elements 能干嘛 4,Elements IDES 5,Elements 工具 1,Elements 介绍 RemObject ...
- Android环境准备
Android环境准备: 1.安装Java环境(自行安装) 2.下载Android SDK包 3.配置系统环境变量 新建ANDROID_HOME 变量值:SDK安装路径(如:D:\android-sd ...
- c++ 中关于一些变量不能声明的问题
j0,j1,jn,y0,y1,yn被c++中某些函数占用了,所以是不能被声明的,今天就遇到了这个问题,结果我在自己写的程序中找了半天都没找到重复申明的y1
- 小学四则运算APP 第二个冲刺 第一天
团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第二次冲刺阶段时间:11.29~12.09 本次发布的是已完成的功能二(选择题): ChoiceActivity.java: packag ...
- Caffe2的安装
源码下载 首先下载caffe2的源码:https://github.com/caffe2/caffe2 网上都建议使用git命令下载,因为caffe2依赖了很多第三方模块,git会根据依赖自动下载第三 ...
- sqlalchemy orm 操作 MySQL
一.ORM介绍 orm英文全称object relational mapping,是对象映射关系程序,简单来说类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型的,为了 ...
- ajax 异步请求
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- shell脚本--函数
shell的函数和Javacript和php的函数声明一样,只不过shell在调用函数的时候,只需要写函数名就可以调用函数,注意不要在函数名后面加括号 创建并使用函数 #!/bin/bash #文件名 ...