maven学习和使用

maven整理

maven整理从如下内容整理:apache

  1. maven中的聚合
  2. 继承
  3. 依赖范围

1.maven中的聚合

一次构建多个项目。每一个项目又包含多个模块。好比:咱们的项目分为:支付项目、商城项目和系统项目。而每个项目又分多个模块 聚合从哪里提及呢?从如下几点提及:eclipse

  • 1.1 如何建立聚合模块
  • 1.2 如何使用聚合模块
  • 1.3在eclipse中建立聚合模块

1.1 如何建立聚合模块

<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.huaying</groupId>
	<artifactId>huaying-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<name>huaying-parent</name>
	<url>http://maven.apache.org</url>

	......
</project>

必定要注意 packaging 为 pommaven

1.2 如何使用聚合模块

使用有多重形式:其实就是为了方便构建项目和多个模块。 以下代码:ide

<!-- 配置  模块 组件  START -->
	<modules>
		<!-- <module>../huaying-common</module>
		<module>../mall-common</module> -->
		<!-- <module>../pay-common</module> -->

		<!-- <module>../pay-yafubao</module>
    	<module>../sys-parent</module> -->
    	<!-- <module>../mall-parent</module> -->
  	</modules>
  	<!-- END 配置  模块 组件  END -->

聚合模块能够分为两类目录结构:工具

  1. 父子目录结构 父子目录结构的代码配置为:
<modules>
		<!-- <module>huaying-common</module>
		<module>mall-common</module> -->
		<!-- <module>pay-common</module> -->
		<!-- <module>pay-yafubao</module>
    	<module>sys-parent</module> -->
    	<!-- <module>mall-parent</module> -->
  	</modules>
  1. 平行目录结构 平行目录结构的代码配置为:
<modules>
		<!-- <module>../huaying-common</module>
		<module>../mall-common</module> -->
		<!-- <module>../pay-common</module> -->

		<!-- <module>../pay-yafubao</module>
    	<module>../sys-parent</module> -->
    	<!-- <module>../mall-parent</module> -->
  	</modules>

1.3在IDEA中建立聚合模块

省略ui

2.继承

使用继承去消除pom中的重复引用。url

<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>
  <parent>
    <groupId>com.huaying</groupId>
    <artifactId>huaying-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../huaying-parent/pom.xml</relativePath>
  </parent>
  <groupId>com.huaying</groupId>
  <artifactId>mall-parent</artifactId>
  <name>mall-parent</name>
  <url>http://maven.apache.org</url>
  <packaging>pom</packaging>
 	......
</project>

2.1 使用pom中必定要注意 relativePath

relativePath 配置项必定要排查清楚否则会报错的呀code

2.2 关注可继承的pom配置元素

  • groupId
  • version
  • url
  • description
  • inceptionYear
  • developers
  • contributors
  • issueManagement
  • ciManagement
  • scm
  • mailingLists
  • properties
  • dependencyManagement
  • distributionManagement
  • build
  • repositories
  • pluginRepositories

3.依赖范围

共有以下5种依赖范围:xml

  1. compile
  2. test
  3. provider
  4. runtime
  5. system 掌握好依赖关联管理基本maven就能够上路了。

4. 一些小技巧和工具

使用以下命令能够分析依赖关系、仅仅是分析。运行时的依赖管理很难排查的只有在项目中才能得到。继承

  1. mvn dependency:list
  2. mvn dependency:tree

5. 参考书籍

maven实战 许晓斌。很是推荐maven小白阅读电子书或者纸质书籍。

相关文章
相关标签/搜索