引入
在平时的java web项目开发中为了便于后期的维护,我们一般都会进行分层开发,最常见的就是分为domain(域模型层)、dao(数据库访问层)、service(业务逻辑层)、web(表现层),这样分层之后,各个层之间的职责会比较明确,后期维护起来也相对比较容易。
现在使用maven来构建以上的各个层,项目结构如下:
1.创建system-parent项目
创建system-parent,用来给各个子模块继承
进入命令行,输入以下命令
mvn archetype:generate -DgroupId=me.gacl -DartifactId=system-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 执行完之后,可以看到在当前目录下生成了一个system-parent目录,里面有一个src目录和一个pom.xml文件
将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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.gacl</groupId>
<artifactId>system-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>system-parent</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.创建system-domain模块
进入创建好的system-parent目录,执行如下命令
mvn archetype:generate -DgroupId=me.gacl -DartifactId=system-domain -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 命令执行完之后,可以看到在system-parent目录中生成了system-domain。
同时,在system-parent目录中的pom.xml文件自动添加了如下内容:
<modules>
<module>system-domain</module>
</modules> 修改system-domain目录中的pom文件,把<groupId>me.gacl</groupId><version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>,因为groupId和version会继承system-parent中的,修改后的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>me.gacl</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>
</project>
2.创建system-dao模块
方法同system-domain,只是还要添加对system-domain模块的依赖,修改后的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>me.gacl</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>me.gacl</groupId>
<artifactId>system-domain</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
3.创建system-service模块
方法同system-domain,只是还要添加对system-dao模块的依赖(system-service依赖system-domain和system-dao,因为system-dao已经依赖了system-domain,所以只用依赖system-dao即可),修改后的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>me.gacl</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>me.gacl</groupId>
<artifactId>system-dao</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
4.创建system-web模块
在system-parent目录下,执行如下命令创建web项目
mvn archetype:generate -DgroupId=me.gacl -DartifactId=system-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false 这时,system-parent中的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>me.gacl</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>
<modules>
<module>system-domain</module>
<module>system-dao</module>
<module>system-service</module>
<module>system-web</module>
</modules>
</project> 在system-web的pom中去掉groupId和version,并且添加对system-service的依赖,修改后的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>me.gacl</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>me.gacl</groupId>
<artifactId>system-service</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>system-web</finalName>
</build>
</project>
5.编译运行
经过以上步骤,相关的模块全部创建完成。
由于实际运行的是system-web模块,所以我们对该模块添加jetty支持(当然也可以先打成war放到容器下)
(1)修改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>me.gacl</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>me.gacl</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>
(2) 进入system-parent目录,执行如下命令
mvn clean install
如图,全部模块install成功
(3)现在进入system-web目录,启动应用
使用如下命令:
mvn jetty:run 启动成功之后,使用如下url访问
http://localhost:8080/system-web/
6.导入eclipse
因为是maven项目,所以以maven项目导入。导入总项目system-parent即可
本文介绍如何使用Maven构建JavaWeb项目的分层结构,包括domain、dao、service和web四个层级,并通过具体步骤展示如何搭建这些模块。
2554

被折叠的 条评论
为什么被折叠?



