使用Maven构建多模块项目

本文详细介绍如何使用Maven搭建一个多模块的Web项目,包括创建父模块及子模块如实体层、DAO层、Service层和Web层,并配置各模块间的依赖关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)创建study-parent。可以用命令创建项目,也可以用eclipse创建study-parent模块,只保留pom.xml

study-parent项目的pom.xml

<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.foo</groupId>
  <artifactId>study-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>study-parent</name>

  <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>
(二)创建study-entity。接着study-parent 右键创建 maven module study-entity


study-entity项目的pom.xml

<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>
  <parent>
    <groupId>com.foo</groupId>
    <artifactId>study-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>study-entity</artifactId>
  <name>study-entity</name>
  <url></url>
</project>
(三)创建study-dao。接着study-parent 右键创建 maven module study-dao

study-dao项目的pom.xml 

<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>
  <parent>
    <groupId>com.foo</groupId>
    <artifactId>study-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <packaging>jar</packaging>
  <artifactId>study-dao</artifactId>
  <dependencies>
  	<dependency>
  		<artifactId>study-entity</artifactId>
  		<groupId>com.foo</groupId>
  		<version>${project.version}</version>
  	</dependency>
  </dependencies>
</project>

加上<packaging>jar</packaging>,因为groupId和version会继承system-parent中的groupId和version,

packaging设置打包方式为jar,同时添加对study-entity模块的依赖

(四)创建study-service。接着study-parent 右键创建 maven module study-service

study-service项目的pom.xml 

<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>
  <parent>
    <groupId>com.foo</groupId>
    <artifactId>study-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>study-service</artifactId>
  <packaging>jar</packaging>
  
   <name>study-service</name>
   <url>http://maven.apache.org</url>
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
     <!--
     system-service依赖system-dao和system-domain,
     但是我们只需添加system-dao的依赖即可,因为system-dao已经依赖了system-domain
     -->
      <dependency>
       <groupId>com.foo</groupId>
       <artifactId>study-dao</artifactId>
       <version>${project.version}</version>
     </dependency>
   </dependencies>
</project>
(五)创建study-parent。study-parent 右键创建 maven module study-web

maven 添加web 支持


同时 study-web项目的pom.xml

<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>
  <parent>
    <groupId>com.foo</groupId>
    <artifactId>study-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>study-web</artifactId>
  <packaging>war</packaging>
  
   <name>study-web</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
       <groupId>com.foo</groupId>
       <artifactId>study-service</artifactId>
       <version>${project.version}</version>
     </dependency>
     <!-- https://mvnrepository.com/artifact/jstl/jstl -->
	<dependency>
	    <groupId>jstl</groupId>
	    <artifactId>jstl</artifactId>
	    <version>1.2</version>
	</dependency>
    <dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>2.5</version>
		<scope>provided</scope>
	</dependency>
   </dependencies>
   <build>
     <finalName>study-web</finalName>
     <plugins>
            <!-- jetty插件 -->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.5</version>
                <configuration>
                    <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory>
                    <scanIntervalSeconds>3</scanIntervalSeconds>
                    <contextPath>/study-web</contextPath><!-- 英文状态 -->
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8088</port>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
    </plugins>
   </build>
</project>
注意这里打的是war包,还有添加了jetty 服务器

启动和访问

study-parent 项目上 maven run clean 然后install  最后study-web 项目上右击选择 maven build  输入 jetty:run 

访问的路径 127.0.0.1::8088/study-web   注意“-”是英文状态下

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html;charset=UTF-8"  isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML>
<html>
  <head>
  	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  	<meta http-equiv="Cache-Control" content="max-age=28800"/>
 	<title>学习系统</title>
 </head>
  <body>
	欢迎登陆学习系统!!!
  </body>
</html>

key:maven 多模块web项目

参考文章:http://www.cnblogs.com/xdp-gacl/p/4242221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值