本篇博文主要包含:
- maven的安装与配置
- maven项目目录的约定
- pom.xml 文件里部分标签的含义
- maven常用命令
- maven的核心概念
- 配置仓库
- 使用Maven建立(聚合)多模块功能
- Maven打包原理
- Maven依赖冲突解决
1、 什么是maven
Maven是一个跨平台的项目管理工具,主要用于基于java平台的项目构建,依赖管理。
如图为项目构建的过程:
解决的项目的问题:
- 如果有好几个项目,这好几个项目中,需要用到很多相同的jar包,
能不能只建立一个仓库来解决这个问题? - 测试方法能不能全部运行呢?
- 怎么样把一个模块的功能放入到仓库中
2、Maven的安装与配置
2.1 下载安装包
官网:http://maven.apache.org
2.2 配置maven环境变量,设置path路径
2.3 利用命令行检查是否成功
使用cmd 打开,输入mvn -version 命令 出现如下界面表示安装成功
3、maven项目目录的约定
src/main/java 存放项目的java文件
src/main/resources 存放项目的资源文件,如spring,hibernate的配置文件
src/test/java 存放所有的测试的java文件
src/test/resources 存放测试用的资源文件
target 项目输出位置
-target/classes 存放编译后的类
- target/test-classes 存放编译后的测试类
- target/surefire-reports 存放测试报告
-target/maven-archiver 执行package的归档
-Hello-0.0.1-SNAPSHOT.jar 执行完package命令后打成的jar包
4、pom.xml 文件里部分标签的含义
groupId:这是项目组的编号,这在组织或项目中通常是独一无二的。
artifactId:这是项目的ID。这通常是项目的名称。 例如,consumer-banking。 除了groupId之外,artifactId还定义了artifact在存储库中的位置。
version:这是项目的版本。与groupId一起使用,artifact在存储库中用于将版本彼此分离。
project:表示一个工程
modelVersion:为版本号
5、maven常用命令
mvn clean :清除target文件。
mvn test:进行测试,并且清理和编译也会自动执行。
mvn package:打包,成功以后,在target目录下多了一个当前工程jar包。
mvn compile:编译。
mvn install:打包后将其安装在本地仓库。
6、maven的核心概念
项目对象模型:
说明:
maven根据pom.xml文件,把它转化成项目对象模型(POM),这个时候要解析依赖关系,然后去相对应的maven库中查找到依赖的jar包。
在clean,compile,test,package等阶段都有相应的Plug-in来做这些事情。而这些plug-in会产生一些中间产物。
7、配置仓库
7.1 配置本地仓库路径
在apache-maven-3.3.9\conf下的setting中
7.2 配置中央仓库的路径和私服路径
私服:是一种特殊的远程仓库,它是架设在局域网内的仓库
在apache-maven-3.3.9\conf下的setting中
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
</mirrors>
<!-- profiles
| This is a list of profiles which can be activated in a variety of ways, and which can modify
| the build process. Profiles provided in the settings.xml are intended to provide local machine-
| specific paths and repository locations which allow the build to work in the local environment.
|
| For example, if you have an integration testing plugin - like cactus - that needs to know where
| your Tomcat instance is installed, you can provide a variable here such that the variable is
| dereferenced during the build process to configure the cactus plugin.
|
| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
| section of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a system property, either matching a particular value for the property,
| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
| Finally, the list of active profiles can be specified directly from the command line.
|
| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
| repositories, plugin repositories, and free-form properties to be used as configuration
| variables for plugins in the POM.
|
|-->
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>
<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>
<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
</profiles>
8、使用Maven建立(聚合)多模块功能
8.1、创建父工程wmq-parent
创建maven工程,并选择pom
在maven里引入依赖,用于子类的继承
<dependencies>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
8.2 创建wmq-service工程
创建maven工程,并选择jar
8.3 创建wmq-web项目
创建maven工程,并选择war
在maven项目中引入父工程wmq-parent
<parent>
<groupId>fly</groupId>
<artifactId>wmq-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
父类中引入的依赖包,已导入此工程下。
每次更新项目时,使用mvn clean install 命令将最新的内容推送到本地仓库,供其他引入父项目依赖的项目使用。
8.4 启动方式
添加tomat启动即可
9、Maven打包原理
9.1 Maven中央存储库
当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖下载。首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源,如果没有找到,然后把它会从默认的 Maven 中央存储库 http://search.maven.org/ 查找下载。
在Maven中,当你声明的库不存在于本地存储库中,也没有不存在于Maven中心储存库,该过程将停止并将错误消息输出到 Maven 控制台。
9.2 添加远程仓库
默认情况下,Maven从Maven中央仓库下载所有依赖关系。但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到。
现在,Maven的依赖库查询顺序更改为:
在 Maven 本地资源库中搜索,如果没有找到,进入下一步,否则退出。
在 Maven 中央存储库搜索,如果没有找到,进入下一步,否则退出。
在Maven的远程存储库搜索,如果没有找到,提示错误信息,否则退出。
10、Maven依赖冲突解决
Web工程依赖两个不同的,maven项目,依赖同一个artifactId但是版本不同,这时候就会产生mavenjar依赖冲突问题,排除依赖:
<dependencies>
<dependency>
<groupId>com.itmayiedu</groupId>
<artifactId>itmayiedu-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.itmayiedu</groupId>
<artifactId>itmayiedu-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 排除commons-logging依赖 -->
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>