在Eclipse中创建Maven多模块工程

来源:互联网 发布:局域网络监控软件 编辑:程序博客网 时间:2024/05/21 09:38

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

在这个分模块项目中主要分为两个模块,Service模块和web模块,Service模块中包含dao,下面是项目结构

Complaints-parent
        |----pom.xml(pom)
        |----Complaints-web
                |----pom.xml (war)
        |----Complaints-service
                |----pom.xml(jar)


Complaints-parent是父工程,同时承担聚合模块和父模块的作用,没有实际代码和资源文件;

Complaints-web是web工程,最终形成最终的war包;

Complaints-service是业务模块的逻辑部分,包含了数据库访问层和业务逻辑层,但是不包括web相关的部分;

上述简单示意图中,有一个父项目(Complaints-parent)聚合很多子项目(Complaints-web,Complaints-service)。每个项目,不管是父子,都含有一个pom.xml文件。而且要注意的是,小括号中标出了每个项目的打包类型。父项目是pom,也只能是pom。子项目有jar,或者war。

依赖关系是:Complaints-web需要依赖Complaints-service

接下来就是在Eclipse中搭建项目了:

1.父项目(Complaints-parent

在Eclipse里面New -> Maven Project

在弹出界面中选择“Create a simple project”



父项目建好之后,目录下面只有一个src和pom.xml文件

将src文件夹删除,然后修改pom.xml文件,将<packaging>jar</packaging>修改为<packaging>pom</packaging>

pom表示它是一个被继承的模块。(如果再创建项目时已经选为了pom就不需要修改)

以下是父项目的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>com.hrtel</groupId>  <artifactId>Complaints-parent</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>pom</packaging>  <name>Complaints-parent</name>  <modules>    <module>Complaints-service</module>    <module>Complaints-web</module>  </modules>    <dependencyManagement>  <dependencies>  <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.12</version>      <scope>test</scope>    </dependency>  </dependencies>  </dependencyManagement></project>

2.创建Complaints-service项目

选中刚建的父项目,在弹出菜单中点击 New -> Maven Module;


使用默认的Archetype(默认:GroupId:org.apache.maven.archetypes,Artifact Id:maven-archetype-quickstart)

这样一个子项目就创建完成了,在文件系统中,子项目会建在父项目的目录中。

细心一点会发现,此时父项目的pom.xml文件中就多了一句

<modules>    <module>Complaints-service</module>  </modules>

3.创建web工程Complaints-web

选中刚建的父项目,在弹出菜单中点击 New -> Maven Module;



创建完之后就会发现父项目的pom.xml文件中又自动多了一句

<modules>    <module>Complaints-service</module>    <module>Complaints-web</module>  </modules>


4.优化配置

Complaints-web是依赖service项目的

需要在web的pom.xml中添加依赖配置

<dependency>          <groupId>com.hrtel</groupId>          <artifactId>Complaints-service</artifactId>          <version>${project.version}</version>      </dependency>

按上面步骤创建的子项目,在pom.xml中有个parent节点,所以,他可以继承父项目的相关信息。没错,父子项目中存在继承关系。

在子项目的pom.xml中,子项目的groupIdversion一般和父项目相同,那么可以把子项目的这两个参数删除,这样会自动继承父项目的取值。

同样,如果其他的一些属性,所有子项目都是一样的,那么可以上移到父项目中设置,子项目中无需重复设置。比如:<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>可以仅在父项目中设置一次。

Manen提供dependencyManagementpluginManagement两个标签。使用这两个标签,可以在父项目中统一管理依赖和插件的配置参数,比如版本号啥的。而在子项目中,仅需列出需要使用的依赖和插件的groupIdartifactId就可以了,其他信息会自动从父项目管理的信息里面获取。

在父项目中:

<dependencyManagement>  <dependencies>    <dependency>        <groupId>junit</groupId>        <artifactId>junit</artifactId>        <version>${junit.version}</version>        <scope>test</scope>    </dependency>    <dependency>        <groupId>org.slf4j</groupId>        <artifactId>slf4j-log4j12</artifactId>        <version>1.7.5</version>        <scope>test</scope>    </dependency>    <dependency>        <groupId>org.slf4j</groupId>        <artifactId>slf4j-api</artifactId>        <version>1.7.5</version>    </dependency>     </dependencies></dependencyManagement>
在子项目中

<dependencies>  <dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>  </dependency></dependencies>

由于web项目依赖service项目,子项目中只需service中添加配置,web项目会自动加载过去的

最后是打包配置:

打包时需要把service项目中的xml文件和别的property文件也打进去,就得在pom文件中说明

<!-- 打包时把需要的xml文件一块打进去 -->  <build>          <finalName>Complaints-service</finalName>          <plugins>              <plugin>                  <groupId>org.apache.maven.plugins</groupId>                  <artifactId>maven-jar-plugin</artifactId>              </plugin>          </plugins>          <resources>              <resource>                  <directory>src/main/java</directory>                  <includes>                      <include>**/*.xml</include>                  </includes>              </resource>              <resource>                  <directory>src/main/resources</directory>                  <includes>                      <include>**/*.xml</include>                      <include>**/*.properties</include>                  </includes>              </resource>          </resources>      </build>

一下是三个项目的pom.xml中的全部内容

Complaints-parent

<?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>com.hrtel</groupId>  <artifactId>Complaints-parent</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>pom</packaging>  <name>Complaints-parent</name>  <modules>    <module>Complaints-service</module>    <module>Complaints-web</module>  </modules>    <dependencyManagement>  <dependencies>  <span style="white-space:pre"></span><dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.12</version>      <scope>test</scope>    </dependency>    <!-- spring beans -->    <dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-core</artifactId>    <version>4.2.5.RELEASE</version></dependency>  </dependencies>  </dependencyManagement></project>

Complaints-service:

<?xml version="1.0"?><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>  <parent>    <groupId>com.hrtel</groupId>    <artifactId>Complaints-parent</artifactId>    <version>0.0.1-SNAPSHOT</version>  </parent>    <artifactId>Complaints-service</artifactId>  <name>Complaints-service</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <!-- 打包时把需要的xml文件一块打进去 -->  <build>          <finalName>Complaints-service</finalName>          <plugins>              <plugin>                  <groupId>org.apache.maven.plugins</groupId>                  <artifactId>maven-jar-plugin</artifactId>              </plugin>          </plugins>          <resources>              <resource>                  <directory>src/main/java</directory>                  <includes>                      <include>**/*.xml</include>                  </includes>              </resource>              <resource>                  <directory>src/main/resources</directory>                  <includes>                      <include>**/*.xml</include>                      <include>**/*.properties</include>                  </includes>              </resource>          </resources>      </build>  <!-- 依赖配置 -->  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>    </dependency>    <!-- spring beans -->    <dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></dependency>  </dependencies></project>

Complaints-web

<?xml version="1.0"?><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>  <parent>    <groupId>com.hrtel</groupId>    <artifactId>Complaints-parent</artifactId>    <version>0.0.1-SNAPSHOT</version>  </parent>    <artifactId>Complaints-web</artifactId>  <packaging>war</packaging>  <name>Complaints-web Maven Webapp</name>  <url>http://maven.apache.org</url>  <dependencies>    <dependency>          <groupId>com.hrtel</groupId>          <artifactId>Complaints-service</artifactId>          <version>${project.version}</version>      </dependency>        <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>    </dependency>      </dependencies>  <build>    <finalName>Complaints-web</finalName>  </build></project>







1 0