Maven基础学习笔记,记录了Maven的创建和相关配置,实现Jar包统一管理。
文章目录
Maven简介
什么是Maven?
由Apache公司提出的跨平台的项目管理工具。主要用于Java平台的项目构建、依赖管理和项目信息管理。简单来说,就是管理团队项目中所有的JAR包,可以通过Maven的中央仓库或本地仓库便捷的下载使用和共享JAR包。通常配置好JDK和Maven后,会使用第三方开发工具自动化配置Maven(包括pom.xml、setting.xml等文件),仅需了解Maven实现原理。
- Maven的功能:
- 清理项目→编译项目→测试项目→生成测试报告→打包项目→部署项目
- Maven的依赖管理功能:
- 自动下载项目所需的jar包
- 统一管理jar包之间的依赖关系
Maven的安装
安装流程:
- 安装JDK(1.6+)并配置好对应的环境变量(需在CMD模式下可用Java命令)
- Maven下载地址:http://maven.apache.org/download.cgi
- 解压后,把Maven的bin目录配置到环境变量中。
环境变量配置
1. 用户变量,变量名:M2_HOME。变量值:文件解压目录,例如:D:\maven3.2.1
2. 在path变量中,追加值:“%M2_HOME%\bin”,如果前面内容则需要加分号“;”。
- 在CMD命令下执行:“mvn -v”,返回Maven相关信息既配置成功。
Maven目录下文件夹功能介绍
- bin:含有mvn运行的脚本
- boot:含有plexus-classworlds类加载器框架
- conf:含有settings.xml配置文件
- lib:含有Maven运行时所需要的java类库
- LICENSE.txt、NOTICE.txt、README.txt针对Maven版本,第三方软件等简要介绍
手动项目管理
构建项目
- 创建maven目录结构(通过Maven管理项目,必须建立指定的目录结构)
- Maven项目根目录
- src
- main
- java:存放项目的.java文件
- resources:存放项目资源文件,如spring,hibernate
- test
- java:存放项目的.java文件
- resources:存放项目资源文件,如spring,hibernate
- target:项目输出位置
- pom.xml:用于标识该项目是一个Maven项
自动生成目录命令
mvn archetype:generate -Dgroupld=com.mycompany.app -Dartifactld=myapp -DarchetypeArtifactld=maven-archetype-quickstart -DinteractiveMode=false
pom.xml文件获取方法:
- 进入Maven安装目录下的lib目录
- 找到maven-model-3.5.0.jar,通过压缩软件打开
- 进入/META-INF/maven/org.apache.maven/maven-model/
- 创建java文件(需要自行建立包文件夹)。例如:“D:/maventest/src/main/java/com/qqfh/app/HelloMaven.java”
- 建立对应的pom.xml文件(可用后面的pom.xml默认文件配置)
- 打开CMD模式,进入到项目根目录下,输入“mvn compile”(编译)。
- 命令执行完毕,显示“BUILD SUCCESS”则执行成功,在根目录下可以看到自动生成的target目录。(第一次执行时需要联网,让Maven自动安装插件,会等待较长时间)
Maven相关命令
配置本地仓库路径:
Maven根目录下:conf/setting.xml:
<localRepository>/path/to/local/repo</localRepository>
复制到非注释处并且更改为:
<localRepository>D:\maven\resp</localRepository>
(D:\maven\resp为本地仓库路径,可自定义)
命令 | 说明 |
---|---|
mvn clean | 清理(清除target目录下编译内容) |
mvn test | 编译并运行测试文件 |
mvn install | 编译,运行和发布JAR包到本地仓库。在项目路径下执行后,会在项目目录和本地仓库目录下生成JAR包,其他项目可以通过该JAR包引用内容。 |
mvn package | 编译,运行和在项目目录下生成JAR包 |
mvn compile | 编译 |
pom.xml文件
项目编译和发布需要配置pom.xml文件,否则无法编译成功。
命令 | 说明 |
---|---|
<groupId> | 包的名字,例如com.qqfh.app |
<artifactId> | 工程名 |
<version> | 版本号 |
<packaging> | 以这个方式发布 |
<scope> | 仅使用,不会发布 |
默认配置文件(未配置项目信息,可执行compile和packag,不能执行install):
<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven</groupId>
<artifactId>maven</artifactId>
<version>3.5.4</version>
</parent>
<artifactId>maven-model</artifactId>
<name>Maven Model</name>
<description>Model for Maven POM (Project Object Model)</description>
<properties>
<checkstyle.violation.ignore>FileLength</checkstyle.violation.ignore>
</properties>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
配置中央仓库
可以配置指定的中央仓库,Maven可以从中央仓库(http://mvnrepository.com 查询maven依赖配置)下载对应JAR包。
- 进入D:\maven\lib\
- 解压maven-model-builder-3.6.0.jar包
- 进入org\apache\maven\model
- 修改pxm-4.0.0.xml文件
- 把url部分修改为中央仓库url,保存
- 进入项目目录下,修改pom.xml
- 增加对应的jar包代码就下载对应jar包(中央仓库里提供对应下载代码)
自动管理项目
通过Eclipse使用Maven
- 新建Maven Project项目
如果需要发布包,则选择:maven-archetype-quickstart
如果仅使用仓库的JAR包,则选择:archetypes.maven-archetype-webapp 使用类 - 修改JDK:右键项目-选择properties,修改“Java Build Path”中右侧Libraries下的JDK版本。需要1.6以上的JDK。
- 执行Maven命令:右键项目-RUN AS-选择对应的Maven命令。如果需要执行自定义命令(如compile),则选择4 Maven
错误&解决方法
一、错误提示:Failed to execute goal org.apache.rat:apache-rat-plugin:0.12:check (rat-
[ERROR] Failed to execute goal org.apache.rat:apache-rat-plugin:0.12:check (rat-
check) on project maven-model: Too many files with unapproved license: 1 See RAT
report in: E:\java\maven\app\target\rat.txt -> [Help 1]
解决办法:
在每个新建的类的第一行加入以下内容即可
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
二、错误提示:Eclipse执行mvn install错误
Eclipse执行mvn install错误
解决办法:
尝试更换jdk版本