官网:Maven – Welcome to Apache Maven
下载地址:Maven – Download Apache Maven
搜索包:Maven Repository: Search/Browse/Explore (mvnrepository.com)
使用方法
1、添加环境变量。
MAVEN_HOME:D:\java\maven\apache-maven-3.6.3
Path:%MAVEN_HOME%\bin
2、验证安装。
打开cmd,输入mvn -v看看是否正常运行。
3、配置Maven。
常用教程
配置文件
环境变量
JVM内存设置:-Xms256m -Xmx512m
settings.xml
在用户目录/.m2目录下。
.mvn/extensions.xml文件
在项目根目录下,
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
<extension>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
</extension>
</extensions>
.mvn/maven.config文件
配置mvn执行的默认参数,例如:-T3 -U ~-~-fail-at-end
可以使用mvn clean package代替mvn -T3 -U –fail-at-end clean package。
.mvn/jvm.config文件
可以添加jvm设置,例如:-Xmx2048m -Xms1024m -XX:MaxPermSize=512m -Djava.awt.headless=true
settings.xml
可以配置本地依赖位置和maven镜像。
参考文档:https://maven.apache.org/settings.html
使用阿里云镜像参考配置:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<pluginGroups ></pluginGroups>
<proxies ></proxies>
<servers ></servers>
<!--本地依赖位置-->
<localRepository>D:\java\maven\.m2\repository</localRepository>
<mirrors>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
<mirror>
<id>ibiblio</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>google-maven-central</id>
<name>Google Maven Central</name>
<url>https://maven-central.storage.googleapis.com
</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- 中央仓库在中国的镜像 -->
<mirror>
<id>maven.net.cn</id>
<name>oneof the central mirrors in china</name>
<url>http://maven.net.cn/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
</settings>
常用命令
参考:https://maven.apache.org/run.html
1、查看帮助:mvn -h
2、内置生命周期命令。
clean - pre-clean, clean, post-clean
default - validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources,
generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test,
verify, install, deploy
site - pre-site, site, post-site, site-deploy
3、使用命令行编译项目。
项目结构:
Project
|--src
| |--main
| | |--java
| | | |--Hello.java
| | | --resources
| |--test
|--pom.xml
使用命令构建项目:
mvn clean
mvn compile
mvn package
mvn install
常见问题
1、缺少包it.sauronsoftware:java:1.0.2。
编辑pom.xml。
<dependency>
<groupId>it.sauronsoftware</groupId>
<artifactId>jave</artifactId>
<version>1.0.2</version>
</dependency>
需要手动安装:
下载地址:http://www.sauronsoftware.it/projects/jave/jave-1.0.2.zip
mvn install:install-file -Dfile="jave-1.0.2.jar" -DgroupId="it.sauronsoftware" -DartifactId="jave" -Dversion="1.0.2" -Dpackaging="jar"
注意:使用powershell安装,值需要用引号引起来。
2、部分阿里云仓库插件无法下载。
在 pom.xml 中指定阿里云仓库即可。
<project>
<repositories>
<repository>
<id>kensite-repos</id>
<name>kensite ali Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
</repositories>
<!-- 设定插件仓库 -->
<pluginRepositories>
<pluginRepository>
<id>aliyun-repos</id>
<name>aliyun Releases</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</project>
3、Maven本地pom文件安装到本地仓库中。
mvn install:install-file -DgroupId=org.springblade.platform -DartifactId=blade-bom -Dversion=2.5.0 -Dpackaging=pom -Dfile=blade-bom:pom:2.5.0
4、Failure to transfer com.bamboocloud:bamboocloud_Codec:pom:0.0.5 from http:~/~/192.168.23.106:8081/repository/maven-releases/ was cached in the local repository, resolution will not be reattempted until the update interval of hylab-release has elapsed or updates are forced.
查看 .m2\repository\com\bamboocloud\bamboocloud_Codec\0.0.5 目录发现只有jar包,但是缺少pom文件。
将jar包拷贝到一个空文件夹,并在该文件夹创建一个pom.xml文件。
<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>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 其他配置(如依赖项) -->
</project>
执行以下代码安装到本地maven仓库中。
mvn install:install-file -Dfile="./bamboocloud_Codec-0.0.5.jar" -DgroupId="com.bamboocloud" -DartifactId="bamboocloud_Codec" -Dversion="0.0.5" -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true