Maven简介
https://maven.apache.org/
软件项目管理和构建工具,可以帮助我们创建和管理项目[编译/测试/打包/发布]。
Maven基于项目对象模型(Project object model, POM)的概念,帮助开发者构建一个项目的完整生命周期[创建、编译、测试、测试报告、发布部署]。POM是Maven对一个项目的描述,一个POM就是一个XML文件。
为什么要用Maven
(1)项目管理
项目规模很大时,一定要将项目进行拆分,将一个项目拆分成多个模块,每个模块对应一个工程。
多个工程之间存在依赖关系,此时可以使用Maven的依赖管理机制,帮助在项目与项目之间建立依赖关系。
(2)jar包管理
a. 以前jar包都自己管理,换机器环境需要重新配置。通过Maven可以使用仓库管理jar包。多个项目可能会用到相同的jar包,此时可以通过Maven将jar包放到仓库中,在项目中引用仓库里的jar包。
b. jar包之间存在依赖关系,并且依赖层次很复杂。使用Maven,自动帮助我们解决jar包的依赖关系。
c. 项目中使用的jar很多,自己下载并手动添加jar包。使用Maven,帮助我们自动下载jar包。
使用Maven必须有网,至少与Maven中央仓库或者公司搭建的Maven私有仓库联网。
(3)自动化的构建工具
构建过程——>编译主代码——>编译测试代码——>执行单元测试(jUnit)——>生成测试报告——>打包(java文件生成jar文件)——>部署
Maven Repository
(1)中央仓库:apache官方提供的网络仓库,存储jar包和Maven插件。
地址repo.maven.apache.org/maven2/或者repo1.maven.org/maven2/
(2)本地仓库:从中央仓库下载下来的jar包等存储在本地机器的位置。
(3)镜像仓库:一些公司实时对中央仓库拷贝,几分钟同步一次,例如阿里云提供的镜像仓库。
地址http://maven.aliyun.com/nexus/content/groups/public
(4)私服:公司局域网内部搭建的Maven服务器。
安装Maven
(1)官网下载安装包
apache-maven-3.6.3-bin.tar.gz
Maven版本对JDK版本有要求,官网有介绍
(2)解压缩
无中文、无空格路径
conf/settings.xml是最核心的配置文件
(3)配置环境变量
Maven依赖于JAVA_HOME,cmd ——> echo %JAVA_HOME%
M2_HOME=配置Maven的安装目录,D:\maven\apache-maven-3.6.3
Path加Maven的bin目录,D:\maven\apache-maven-3.6.3\bin
测试,cmd ——> mvn -version
linux配置环境变量
centos ——> vim /etc/profile
export M2_HOME=pwd查看Maven的绝对路径
export PATH=$PATH:$M2_HOME/bin
修改生效centos ——> source /etc/profile
测试mvn -version
使用Maven创建项目
(0)配置Maven本地仓库
默认位置:C:\Users\用户名.m2\repository,即后续使用Maven下载jar包时,都默认存到以上目录。
可以编辑Maven的核心配置文件D:\maven\apache-maven-3.6.3\conf\settings.xml:
#修改本地仓库存放位置
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
···
<localRepository>D:\maven\maven-repository</localRepository>
···
#修改Maven下载jar包的仓库地址:中央仓库改为镜像仓库
<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>
-->
···
<mirror>
<id>aliyun</id>
<mirrorOf>*</mirrorOf> <!-- 所以访问都使用该镜像仓库 -->
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
···
</mirrors>
(1)创建项目
d:
cd \maven\maven-projects #切换到存放项目文件夹
dir
mvn archetype:generate #创建mvn项目,默认从中央仓库下载骨架结构所需jar包
一直到Choose archetype出现选择创建项目模板类型,默认7是简单的java项目,直接Enter键
又出现Define value for property 'groupId'指定“组织的名称”,公司域名反向及项目名称(com.公司名.项目名称),e.g. com.itany.ums
又出现Define value for property 'artifactId'指定项目里“模块的名称”,e.g. test
又出现Define value for property 'version' 1.0-SNAPSHOT 指定“模块当前的版本”,默认Enter键,也可以修改
又出现Define value for property 'package' (com.公司名.项目名称),输入“模块里的包名”,默认和groupId一样
最后会确认以上设置,Enter键,BUILD SUCCESS!
#project业务组织名 - module模块名 - package包名 - class/interface
(2)修改JDK版本配置
使用Maven创建的项目,默认使用JDK1.5。可以编辑Maven的核心配置文件D:\maven\apache-maven-3.6.3\conf\settings.xml:
<profiles>
···
<profile>
<id>jdk-1.8</id>
<activation>
<acticeByDefault>true</acticeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
···
<!-- 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>