Maven的聚合和依赖

版权声明:欢迎转载,转载请说明出处. 大数据Github项目地址https://github.com/SeanYanxml/bigdata。 https://blog.csdn.net/u010416101/article/details/88428101

前言

前几章我们介绍了Maven的基本安装和使用.本章我们将介绍下Maven的聚合和依赖.

在一个复杂的项目内,我们经常会遇到聚合和依赖问题.(PS: 我们上章所说的Jar包的引用其实就是依赖的一种.)

PS: 本文所用的Maven例子可以在我的Github仓库内找到
https://github.com/SeanYanxml/maven-train


聚合

一般的大型项目都会分为多个子项目,例如如下的项目模式.

目录结构如下所示:

maven-train-parent/
├── maven-train-parent-son
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── yanxml
│   │   │   │           └── parent
│   │   │   │               └── son
│   │   │   │                   └── HelloMain.java
│   │   │   └── resources
│   │   └── test
│   │       ├── java
│   │       └── resources
│   └── target
│       ├── classes
│       │   ├── META-INF
│       │   │   ├── MANIFEST.MF
│       │   │   └── maven
│       │   │       └── com.yanxml
│       │   │           └── maven-train-parent-son
│       │   │               ├── pom.properties
│       │   │               └── pom.xml
│       │   └── com
│       │       └── yanxml
│       │           └── parent
│       │               └── son
│       │                   └── HelloMain.class
│       └── test-classes
├── maven-train-parent-util
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── yanxml
│   │   │   │           └── parent
│   │   │   │               └── util
│   │   │   │                   └── StringUtil.java
│   │   │   └── resources
│   │   └── test
│   │       ├── java
│   │       └── resources
│   └── target
│       ├── classes
│       │   ├── META-INF
│       │   │   ├── MANIFEST.MF
│       │   │   └── maven
│       │   │       └── com.yanxml
│       │   │           └── maven-train-parent-util
│       │   │               ├── pom.properties
│       │   │               └── pom.xml
│       │   └── com
│       │       └── yanxml
│       │           └── parent
│       │               └── util
│       │                   └── StringUtil.class
│       └── test-classes
└── pom.xml

由上节,Maven的基本使用我们可以知道.Maven项目的管理主要是通过pom.xml文件进行管理的,此项目内的pom.xml文件如图所示:

  • pom.xml(maven-train-parent )
<?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>com.yanxml</groupId>
    <artifactId>maven-train</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <groupId>com.yanxml</groupId>
  <artifactId>maven-train-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>maven-train-parent</name>
  <url>http://maven.apache.org</url>
  <packaging>pom</packaging>
  
  <modules>
  	<module>maven-train-parent-util</module>
  	<module>maven-train-parent-son</module>
  </modules>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencyManagement>
  	<dependencies>
  	<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
	<dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>fastjson</artifactId>
    	<version>1.2.44</version>
	</dependency>
  </dependencies>
  	
  </dependencyManagement>
  
  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
	<dependency>
    	<groupId>org.apache.commons</groupId>
    	<artifactId>commons-lang3</artifactId>
    	<version>3.7</version>
	</dependency>
  </dependencies>

</project>

其中的<modules>主要列出项目内的多个子项目.由上可知,此项目是一个Maven父项目,项目下有2个子项目.

  • pom.xml(maven-train-parent-util)
<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.yanxml</groupId>
    <artifactId>maven-train-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>maven-train-parent-util</artifactId>
</project>

由上可知,此项目是一个Maven子项目,父项目为maven-train-parent.

  • pom.xml(maven-train-parent-son)
<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.yanxml</groupId>
    <artifactId>maven-train-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>maven-train-parent-son</artifactId>
  
  <dependencies>
  	<!-- dependency -->
  	<!--依赖项目内的工具类-->
  	<dependency>
  		<groupId>com.yanxml</groupId>
  		<artifactId>maven-train-parent-util</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	
  </dependencies>
</project>

由上可知,此项目是一个Maven子项目,父项目为maven-train-parent.且依赖子项目maven-train-parent-util.

依赖

本节.我们的目标为在maven-train-parent-util内编写一个工具类,让maven-train-parent-son对其进行使用.让其像我们引用的Jar包一样进行引用.

  • 工具类(maven-train-parent-util)
package com.yanxml.parent.util;

import org.apache.commons.lang3.StringUtils;

public class StringUtil {
	
	public static boolean isEmpty(String blank){
		boolean flag = false;
		if(StringUtils.isEmpty(blank)){
			flag = true;
		}
		return false;
	}

}

  • 工具类(使用 maven-train-parent-son`)
package com.yanxml.parent.son;

import com.yanxml.parent.util.StringUtil;

public class HelloMain {
	public static void main(String[] args) {
		String string = "hello";
		
		// StringUtil 自己写的工具类(maven-train-parent-util)
		if(StringUtil.isEmpty(string)){
			System.out.println("String is null");
		}else{
			System.out.println("String is not null.");
		}
	}

}
  • 项目结构
    我们可以明显的在maven-train-parent-son发现其引用了maven-train-parent-util项目.

当然,我们也可以将工具包打包,随后放入Maven仓库直接使用.具体操作如下:

localhost:maven-train-parent-util Sean$ mvn package install
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-train-parent-util 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-train-parent-util ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-train-parent-util ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/Sean/Documents/Gitrep/maven-train/maven-train-parent/maven-train-parent-util/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-train-parent-util ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-train-parent-util ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-train-parent-util ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-train-parent-util ---
[INFO] Building jar: /Users/Sean/Documents/Gitrep/maven-train/maven-train-parent/maven-train-parent-util/target/maven-train-parent-util-0.0.1-SNAPSHOT.jar
[INFO] META-INF/maven/com.yanxml/maven-train-parent-util/pom.xml already added, skipping
[INFO] META-INF/maven/com.yanxml/maven-train-parent-util/pom.properties already added, skipping
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-train-parent-util ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-train-parent-util ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-train-parent-util ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-train-parent-util ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-train-parent-util ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-train-parent-util ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-train-parent-util ---
[INFO] Installing /Users/Sean/Documents/Gitrep/maven-train/maven-train-parent/maven-train-parent-util/target/maven-train-parent-util-0.0.1-SNAPSHOT.jar to /Users/Sean/.m2/repository/com/yanxml/maven-train-parent-util/0.0.1-SNAPSHOT/maven-train-parent-util-0.0.1-SNAPSHOT.jar
[INFO] Installing /Users/Sean/Documents/Gitrep/maven-train/maven-train-parent/maven-train-parent-util/pom.xml to /Users/Sean/.m2/repository/com/yanxml/maven-train-parent-util/0.0.1-SNAPSHOT/maven-train-parent-util-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.427 s
[INFO] Finished at: 2019-03-12T20:08:46+08:00
[INFO] Final Memory: 17M/170M
[INFO] ------------------------------------------------------------------------

在这里插入图片描述


Reference

[1] maven-train

没有更多推荐了,返回首页