使用Maven构建多模块项目

  在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层)、dao(数据库访问层)、service(业务逻辑层)、web(表现层),这样分层之后,各个层之间的职责会比较明确,后期维护起来也相对比较容易,今天我们就是使用Maven来构建以上的各个层。

  项目结构如下:

  system-parent
    |—-pom.xml
    |—-system-domain
        |—-pom.xml
    |—-system-dao
        |—-pom.xml
    |—-system-service
        |—-pom.xml
    |—-system-web
        |—-pom.xml

一、创建system-parent项目
  创建system-parent,用来给各个子模块继承。

  进入命令行,输入以下命令:

mvn archetype:generate -DgroupId=com.xinyu -DartifactId=system-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

命令执行完成之后可以看到在当前目录(D:\JAVA\study\studymaven)生成了system-parent目录,里面有一个src目录和一个pom.xml文件,如下图所示
这里写图片描述
将src文件夹删除,然后修改pom.xml文件,将jar修改为pom,pom表示它是一个被继承的模块,修改后的内容如下:

<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.xinyu</groupId>
  <artifactId>system-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>system-parent</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>

二、创建sytem-domain模块
  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:generate -DgroupId=com.xinyu -DartifactId=system-domain -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

修改system-domain目录中的pom.xml文件,把<groupId>com.xinyu</groupId><version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>,因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar

 修改过后的pom.xml文件如下:

<?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.xinyu</groupId>
    <artifactId>system-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>


  <artifactId>system-domain</artifactId>
  <packaging>jar</packaging>

 <name>system-domain</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>


</project>

三、创建sytem-dao模块
  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:generate -DgroupId=com.xinyu -DartifactId=system-dao -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

修改system-dao目录中的pom.xml文件,,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>,因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar,同时添加对system-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.xinyu</groupId>
    <artifactId>system-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>system-dao</artifactId>
 <packaging>jar</packaging>

  <name>system-dao</name>
  <url>http://maven.apache.org</url>

   <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>


  <dependencies>
    <!--system-dao需要使用到system-domain中的类,所以需要添加对system-domain模块的依赖-->
     <dependency>
      <groupId>com.xinyu</groupId>
      <artifactId>system-domain</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>


</project>

四、创建system-service模块
  在命令行进入创建好的system-parent目录,然后执行下列命令:

mvn archetype:generate -DgroupId=com.xinyu -DartifactId=system-service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

修改system-service目录中的pom.xml文件,,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>,因为groupId和version会继承system-parent中的groupId和version,packaging设置打包方式为jar,同时添加对system-dao模块的依赖,system-service依赖system-dao和system-domain,但是我们只需添加system-dao的依赖即可,因为system-dao已经依赖了system-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.xinyu</groupId>
    <artifactId>system-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>


  <artifactId>system-service</artifactId>
  <packaging>jar</packaging>

  <name>system-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.xinyu</groupId>
      <artifactId>system-dao</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

</project>

五、创建system-web模块
  在命令行进入创建好的system-parent目录,然后执行下列命令:
  

mvn archetype:generate  -DgroupId=com.xinyu -DartifactId=system-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

修改system-web目录中的pom.xml文件,,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,因为groupId和version会继承system-parent中的groupId和version,同时添加对system-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.xinyu</groupId>
    <artifactId>system-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>


  <artifactId>system-web</artifactId>
  <packaging>war</packaging>

  <name>system-web Maven Webapp</name>
  <url>http://maven.apache.org</url>

   <dependencies>
    <!--
    system-web依赖system-service
    -->
     <dependency>
      <groupId>com.xinyu</groupId>
      <artifactId>system-service</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>system-web</finalName>
  </build>
</project>

六、编译运行项目
  经过上面的五个步骤,相关的模块全部创建完成,怎么运行起来呢。由于最终运行的是system-web模块,所以我们对该模块添加jetty支持,方便测试运行。修改system-web项目的pom.xml如下:
  

<?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.xinyu</groupId>
    <artifactId>system-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>


  <artifactId>system-web</artifactId>
  <packaging>war</packaging>

  <name>system-web Maven Webapp</name>
  <url>http://maven.apache.org</url>

   <dependencies>
    <!--
    system-web依赖system-service
    -->
     <dependency>
      <groupId>com.xinyu</groupId>
      <artifactId>system-service</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>system-web</finalName>
    <plugins>
        <!--配置Jetty插件-->
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
        </plugin>
    </plugins>
  </build>
</project>

在命令行进入system-parent目录,然后执行下列命令:

mvn clean install

命令执行完后,在system-web目录下多出了target目录,里面有了system-web.war,如下图所示:

这里写图片描述
命令行进入sytem-web目录,执行如下命令,启动jetty

mvn jetty:run

这里写图片描述
启动jetty服务器后,访问http://localhost:8080/system-web/ 运行结果如下图所示:
这里写图片描述

备注:关闭jetty服务器 dos界面可以按CTRL+ C 然后输入Y/y 即可 ,最简单的方法是关闭dos界面、

这里写图片描述

七、导入Eclipse中进行开发
导入之后我们会发现system-web下面的index.jsp报错
The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path
这里写图片描述

处理方法:
1.在window 点击preferences进入找到server配置server runtime environments如图:
这里写图片描述
2. 右键system-web –>build path –>进入java bulid path配置 如图:
这里写图片描述
然后index,jsp不再报错

八、部署到Tomcat中运行
这里写图片描述

备注:此项目参考网上博客 http://www.cnblogs.com/xdp-gacl/p/4242221.html 自己实践记录下来,如有侵权,请告知,务必会在第一时间删除博客。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值