Eclipse下使用maven搭建多模块项目
Posted 不积跬步无以至千里
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Eclipse下使用maven搭建多模块项目相关的知识,希望对你有一定的参考价值。
暂时将项目分为如下几层:
domain(域模型层)、dao(数据库访问层)、service(业务逻辑层)、web(表现层),有需要再另行添加(如common等);
目录结构:
一、app
该层为父层,管理其它模块层。
1.1 创建Maven Project
1.2 选择maven-archetype-quickstart
1.3 删除src文件夹,修改pom.xml文件,将<packaging>jar</packaging>修改为<packaging>pom</packaging>,pom表示它是一个被继承的模块
修改之后的配置文件:
- <?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/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.bdong</groupId>
- <artifactId>app</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>app</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <span style="color:#ff0000;"><strong><packaging>pom</packaging></strong></span>
- <modules>
- <module>app-domain</module>
- <module>app-dao</module>
- <module>app-service</module>
- <module>app-web</module>
- </modules>
- </project>
二、app-domain
2.1 创建Maven Module
2.2 选择maven-archetype-quickstart
2.3 修改app-domain目录中的pom.xml文件,把<groupId>com.bdong</groupId>和<version>0.0.1-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>,groupId和version会继承app中的groupId和version,packaging设置打包方式为jar
修改后的配置文件:
- <?xml version="1.0"?>
- <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.bdong</groupId>
- <artifactId>app</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <artifactId>app-domain</artifactId>
- <strong><span style="color:#ff0000;"><packaging>jar</packaging></span></strong>
- <name>app-domain</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project><strong>
- </strong>
三、app-dao
3.1 创建Maven Module
3.2 选择maven-archetype-quickstart
3.3 修改app-dao目录中的pom.xml文件,,把<groupId>com.bdong</groupId>和<version>0.0.1-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>,因为groupId和version会继承app中的groupId和version,packaging设置打包方式为jar,同时添加对app-domain模块的依赖
修改后的配置文件:
- <?xml version="1.0"?>
- <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.bdong</groupId>
- <artifactId>app</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <artifactId>app-dao</artifactId>
- <packaging>jar</packaging>
- <name>app-dao</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <!--依赖app-domain模块-->
- <dependency>
- <groupId>com.bdong</groupId>
- <artifactId>app-domain</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
四、app-service
4.1 创建Maven Module
4.2 选择maven-archetype-quickstart
4.3 修改app-service目录中的pom.xml文件,,把<groupId>com.bdong</groupId>和<version>0.0.1-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>,packaging设置打包方式为jar,同时添加对app-dao模块的依赖,app-service依赖app-dao和app-domain,但是只需添加app-dao的依赖即可,因为app-dao已经依赖了app-domain。
修改后的配置文件:
- <?xml version="1.0"?>
- <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.bdong</groupId>
- <artifactId>app</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <artifactId>app-service</artifactId>
- <packaging>jar</packaging>
- <name>app-service</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>com.bdong</groupId>
- <artifactId>app-dao</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
五、app-web
5.1 创建Maven Module
5.2 选择maven-archetype-webapp
5.3 修改app-web目录中的pom.xml文件,,把<groupId>com.bdong</groupId>和<version>0.0.1-SNAPSHOT</version>去掉,修改打包方式为<packaging>war</packaging>,添加对app-service模块的依赖
修改后的配置文件:
- <?xml version="1.0"?>
- <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.bdong</groupId>
- <artifactId>app</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <artifactId>app-web</artifactId>
- <strong> <span style="color:#ff0000;"><packaging>war</packaging></span></strong>
- <name>app-web Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>com.bdong</groupId>
- <artifactId>app-service</artifactId>
- <version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <finalName>app-web</finalName>
- </build>
- </project>
六,编译
控制台打印信息:
七、运行
7.1 添加app-web到tomcat中
7.2 运行tomcat,然后在浏览器中访问:localhost:8080/app-web
最终,目录结构为:
使用命令搭建多模块可参考:http://www.cnblogs.com/xdp-gacl/p/4242221.html
以上是关于Eclipse下使用maven搭建多模块项目的主要内容,如果未能解决你的问题,请参考以下文章