分模块创建maven项目(一)

时间:2022-02-26 14:26:26

maven是一个项目构建和管理的工具。

我们可以通过maven仓库可以实现管理构建(主要是JAR还包括:WAR,ZIP,POM等等)。

我们可以通过maven插件可以实现编译源代、产生Javadoc文、运行unit测试、生成站点等等功能。

maven通过分模块开发提升效率。

工具/原料

  • maven
  • eclipse

创建parent项目

右击创建maven项目创建parent项目模块,(通过parent项目中的pom.xml文件实现聚合和继承的功能);如下图:

{

聚合:不再对其它子模块进行一一操作,在parent直接操作即可,方便管理

继承:类似java的继承——对子模块*同属性的抽象(parent中的pom.xml中有的属性子模块中不需要在添加,避免重复)

}

分模块创建maven项目(一)

分模块创建maven项目(一)

分模块创建maven项目(一)

分模块创建maven项目(一)

修改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>
<name>user-parent</name>
<url>http://maven.apache.org</url>
<groupId>com.company.user</groupId>
<artifactId>user-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging> </project>

注:在修改packaging为pom,在project添加内容

分模块创建maven项目(一)

添加聚合功能

例如项目中的模块分类为core、dao、log、service在pom.xml中的project标签中添加:

<!-- 实现聚合 -->
<modules>
<module>../user-core</module>
<module>../user-dao</module>
<module>../user-log</module>
<module>../user-service</module>
</modules>

分模块创建maven项目(一)

添加继承的功能

继承功能的实现:

不需要在parent中,在其他项目模块的pom.xml中引入parent项目中的pom.xml文件即可

如下:

<parent>
<groupId>org.konghao.user</groupId>
<artifactId>user-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../user-parent/pom.xml</relativePath>
</parent>

分模块创建maven项目(一)

继承功能之自定义变量和全局变量(maven内置隐式变量)--引用方式为${}:

1、全局变量(maven内置隐式变量——参考我的maven内置隐式变量的使用):${project.groupId}、${project.artifactId}

2、自定义变量:

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.10</junit.version>
<mysql.driver>com.mysql.jdbc.Driver</mysql.driver>
<mysql.url>jdbc:mysql://localhost:3306/mysql</mysql.url>
<mysql.username>root</mysql.username>
<mysql.password>123456</mysql.password>
</properties>

备注:

使用范围:

parent项目中和继承parent的项目中

使用方式:

${junit.version}

例如:

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

分模块创建maven项目(一)

继承功能之发布仓库

<distributionManagement>
<!--snapshots仓库-->
<snapshotRepository>
<id>user-snapshots</id>
<name>User Project SNAPSHOTS</name>
<url>http://192.168.0.199:8081/nexus/content/repositories/MyUserReposSnapshots/</url>
</snapshotRepository>
<!--releases仓库-->
<repository>
<id>user-releases</id>
<name>User Project Release</name>
<url>http://192.168.0.199:8081/nexus/content/repositories/MyUserReposRelease/</url>
</repository> </distributionManagement>

分模块创建maven项目(一)

继承功能之全局依赖和自定义依赖:

全局依赖:在所有继承parent的子模块中都会引入jar包

<!-- 全局依赖 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

自定义依赖:在继承parent的子模块中通过指定来引入依赖

<!-- 自定义依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>

指定方式:

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>

分模块创建maven项目(一)

分模块创建maven项目(一)

本文转自:http://jingyan.baidu.com/article/b87fe19e93b8905218356880.html