上一篇记录了maven项目的创建,由于现在项目都是分层搭建,这次记录一篇maven分层项目的创建。参考的http://www.cnblogs.com/xdp-gacl/p/4242221.html。感谢这位大神的分享。
项目结构:system-parent, system-domain, system-dao, system-service, system-web. web依赖service, service 依赖dao, dao依赖domain。parent把这几个模块总括起来。
1.创建system-parent:
mvn archetype:generate -DgroupId=moin -DartifactId=system-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
注:原文archetype:create,在新版本的maven中会报错,generate没有问题。
执行后会生成一个src目录和pom.xml文件。parent只保留pom.xml将src删除。
system-parent的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>moin</groupId>
<artifactId>system-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<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>
2.创建system-domain:
将目录切换到system-parent下,执行命令:
mvn archetype:generate -DgroupId=moin -DartifactId=system-domain -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
system-domain的pom文件如下:
<?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>moin</groupId>
<artifactId>system-domain</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>
3,创建system-dao:
将目录切换到system-parent下,执行命令:
mvn archetype:generate -DgroupId=moin -DartifactId=system-dao -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
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>moin</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>
<dependency>
<groupId>moin</groupId>
<artifactId>system-domain</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
4,创建system-service
方法与system-dao相同,注意pom.xml中依赖于system-dao模块。
5,创建system-web.
切换到system-parent目录下,执行命令:
mvn archetype:generate -DgroupId=moin -DartifactId=system-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
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>moin</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>
<dependency>
<groupId>moin</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>
注意:packaging为war.src的webapp下会默认生成一个index.jsp。
此时system-parent的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>moin</groupId>
<artifactId>system-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<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>
parent会将domain,dao,service,web5个模块包含进来。然后将项目引入到eclipse中,就会看见分模块的maven项目。