本文讲解Maven环境的搭建
- 没有Maven 时,项目用到的.jar 文件通常需要拷贝到lib目录下,项目多了,拷贝的文件副本就多了,占用磁盘空间,且难以管理。
- Maven使用 一个称之为仓库的目录,根据构件的坐标统一存储这些构件的唯一副本,在项目中通过依赖声明,可以方便的引用构件。
- 构件的坐标范例如下:
<dependency> <groupId>javax.servletgroupId> <artifactId>javax.servlet-apiartifactId> <version>3.1.0version> <type>jartype> dependency>
Maven 官网:http://maven.apache.org/
Maven 下载地址: http://maven.apache.org/download.cgi
详情参考官网文档:http://maven.apache.org/download.cgi
Maven 运行前提需要安装有Java JRE 环境, 换句话说必须在PATH环境变量中找到JAVA_HOME环境变量
下载解压后目录如下:
新建MAVEN_HOME 环境变量,并追加软件解压的路径,比如这样:
MAVEN_HOME
C:\apps\maven\apache-maven-3.6.1
在Path环境变量下追加
%MAVEN_HOME%\bin\;
详情参考官网:http://maven.apache.org/install.html
查看Maven都有哪些内置的命令,执行如下:
mvn -h
执行后会在在${user.home}当前登录用户目录下生成一个 .m2文件夹
在 ${user.home}/.m2/下创建一个settings.xml文件,内容如下:
<settings
xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>${user.home}/.m2/repositorylocalRepository>
<interactiveMode>trueinteractiveMode>
<offline>falseoffline>
<pluginGroups>
<pluginGroup>org.mortbay.jettypluginGroup>
<pluginGroup>org.sonatype.pluginspluginGroup>
<pluginGroup>org.sonarsource.scanner.mavenpluginGroup>
pluginGroups>
<proxies>
proxies>
<servers>
servers>
<mirrors>
mirrors>
<profiles>
<profile>
<id>myShareProfileid>
<repositories>
<repository>
<id>alimavenid>
<name>aliyun mavenname>
<url>https://maven.aliyun.com/repository/publicurl>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
<repository>
<id>Centeralid>
<name>Central Maven2name>
<url>http://central.maven.org/maven2url>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
<repository>
<id>UKid>
<name>UK Centralname>
<url>http://uk.maven.org/maven2url>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
<repository>
<id>SpringPluginid>
<name>Spring Pluginsname>
<url>http://mvnrepository.com/repos/sonatype-releasesurl>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
<repository>
<id>SpringLibsid>
<name>Spring Libsname>
<url>http://repo.spring.io/libs-milestone/url>
<snapshots>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
<repository>
<id>spring-snapshotsid>
<name>Spring Snapshotsname>
<url>https://repo.spring.io/libs-snapshot-localurl>
<snapshots>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
repository>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/libs-milestone-localurl>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
repository>
<repository>
<id>spring-releasesid>
<name>Spring Releasesname>
<url>https://repo.spring.io/libs-release-localurl>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
repository>
<repository>
<id>Snoatypeid>
<name>Snoatypename>
<url>http://mvnrepository.com/repos/sonatype-releasesurl>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
<repository>
<id>Atlassianid>
<name>Atlassian Repositoryname>
<url>https://maven.atlassian.com/content/repositories/atlassian-public/url>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
<repository>
<id>Hortonworksid>
<name>Hortonworks Repositoryname>
<url>http://repo.hortonworks.com/content/repositories/releases/url>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
<repository>
<id>Jbossid>
<name>JBoss Releases Repositoryname>
<url>https://repository.jboss.org/nexus/content/repositories/releases/url>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
<repository>
<id>Nexeoid>
<name>Nuxeo Releases Repositoryname>
<url>https://maven-eu.nuxeo.org/nexus/content/repositories/public-releases/url>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
<repository>
<id>XWikiid>
<name>XWiki Releases Repositoryname>
<url>http://maven.xwiki.org/releases/url>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
<repository>
<id>Apacheid>
<name>Apache Releases Repositoryname>
<url>https://repository.apache.org/content/repositories/releases/url>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus-aliyunid>
<url>http://maven.aliyun.com/nexus/content/groups/publicurl>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
releases>
<snapshots>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
pluginRepository>
<pluginRepository>
<id>spring-snapshotsid>
<name>Spring Snapshotsname>
<url>https://repo.spring.io/libs-snapshot-localurl>
<snapshots>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
pluginRepository>
<pluginRepository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/libs-milestone-localurl>
<snapshots>
<enabled>falseenabled>
<updatePolicy>alwaysupdatePolicy>
snapshots>
pluginRepository>
pluginRepositories>
profile>
profiles>
<activeProfiles>
<activeProfile>myShareProfileactiveProfile>
activeProfiles>
settings>
有时候添加jar依赖需要手动查找Maven依赖配置信息,可通过以下两个网址查找。
有时候Maven传递依赖的jar会有冲突,如何查找呢?
比如A-1.2.jar 包依赖于 B-0.1.jar , 然而项目中已经引入了一个较高版本B-1.1.jar,
我们想要的结果是只有A-1.2.jar 和B-1.1.jar 首先通过这个Maven照妖镜命令查找Maven依赖树
在pom.xml 同级目录下执行命令:
mvn dependency:tree
可以看到xmlschema-core 是传递依赖的一个jar
然后排除冲突的依赖可以配置如下
<dependency>
<groupId>xml-resolvergroupId>
<artifactId>xml-resolverartifactId>
<version>1.2version>
<exclusions>
<exclusion>
<groupId>org.apache.ws.xmlschemagroupId>
<artifactId>xmlschema-coreartifactId>
exclusion>
exclusions>
dependency>
如果想排除所有的依赖,可以使用通配符
<dependency>
<groupId>xml-resolvergroupId>
<artifactId>xml-resolverartifactId>
<version>1.2version>
<exclusions>
<exclusion>
<groupId>*groupId>
<artifactId>*artifactId>
exclusion>
exclusions>
dependency>
有时候有些私有的包或者网络等原因,中心仓库下载不到或无法上传私服,那么可以通过命令安装一个jar到本地仓库
比如要junit-bom.5.4.2.jar安装到本地仓库
<dependency>
<groupId>org.junitgroupId>
<artifactId>junit-bomartifactId>
<version>5.4.2version>
<type>pomtype>
dependency>
在unit-bom.5.4.2.jar的同一级目录下执行命令如下:
mvn install:install-file -DgroupId=org.junit -DartifactId=junit-bom -Dversion=5.4.2 -Dpackaging=jar -Dfile=junit-bom.5.4.2.jar
注意:groupId 和artifactId 以及version 要和自己项目里面pom.xml中配置的保持一致。
<skipTests>trueskipTests>
命令行配置法
mvn clean package -Dmaven.test.skip
-Dmaven.test.failure.ignore=true
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<configuration>
<testFailureIgnore>truetestFailureIgnore>
configuration>
plugin>
如果有时候Maven出问题,看不出具体原因可开启详细异常日志。
输入命令如下:
mvn clean install -e
-e 详细异常
mvn clean install -U
-U 强制更新
如果想更详细了解学习Maven,可查阅官方文档
或移步我的阿里云云栖社区博文。