(转)在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)构建工具作用:帮助程序员进行项目的创建,目录 ...
随机推荐
- Unity接入Steamworks
一.将scrpts/Steamworks.net/SteamManager组件添加到游戏物体上 二.修改SteamManager的代码为游戏的id如图所示 三.Unity,打开项目根目录,修改stea ...
- linux书籍
<鸟哥私房菜-基础版> <实战LINUX_SHELL编程与服务器管理> <LINUX命令行与SHELL脚本编程大全第2版].布卢姆.扫描版> <Linux初学 ...
- 软工个人作业-博客作业-WEEK2
1.是否需要代码规范: (1)这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 首先来说,从短期上和个体上来看,一个团队的代码风格必然会在一定程 ...
- MyEclipse项目里面出现红叉的解决方案?
一般出现在从别处import的项目上,只有项目文件夹上有红叉,其他地方都正常,现总结个人的几个解决方案: 有几种可能: 1,编码设置是否一致,也即是你项目原来的编码和现在eclipse用的默认编码 ...
- jQuery(六)
$下常用方法 $().xxx:只能给jq对象用 $.xxx不仅可以给jq用也可以给原生js用,叫做工具方法 $.type() <script> $(function(){ var a= n ...
- 文本文件合并(C++实现)
直接附上代码吧 #include<iostream> #include<fstream> #include<cstdlib> using namespace std ...
- JDK学习AbstractQueuedSynchronizer和AbstractQueuedLongSynchronizer
AbstractQueuedLongSynchronizer类是扩展自AbstractQueuedSynchronizer的,实现了java.io.Serializable接口. 其中提到的wait ...
- Array与Object
typeof([ ])的返回值是object,因为数组叫做数组对象. Array有length属性,而Object没有length属性,所以可以根据length属性来判断数据属于数组还是对象. Arr ...
- 自己站点的nginx 配置信息
user www www; worker_processes auto; error_log /home/wwwlogs/nginx_error.log crit; pid /usr/local/ng ...
- FFT ip core
The FFT core provides four architecture options to offer a trade-off权衡取舍 between core size andtransf ...