本文共 4634 字,大约阅读时间需要 15 分钟。
Maven有一个默认的远程仓库,id为central,是Apache搭建的,默认情况下都会去这个仓库查找获取文件,因为服务器在国外,所以速度很感人,因此通常我们会使用国内的库来代替中央仓库。
Maven提供了一个映射机制来实现远程仓库的切换,使用时要配置该镜像的被映射目标对象,可以是一个或多个,*为全部映射,当请求被映射的仓库,会从之前的仓库切到该镜像中的仓库地址进行查找获取文件,从而达到切换远程仓库的目的。
可以同时配置多个mirror,但镜像匹配顺序是按照mirror的配置顺序依次匹配的,前面的mirror匹配成功,则后面的mirror不会生效,因此,不能用mirror来实现配置多个仓库
,mirror的作用主要是用于仓库加速。
配置格式:在maven的settings.xml文件中进行如下配置:
nexus-aliyun central Nexus aliyun http://maven.aliyun.com/nexus/content/groups/public
对于各配置项,官方解释:
- id, name: The unique identifier and user-friendly name of this mirror. The
id
is used to differentiate betweenmirror
elements and to pick the corresponding credentials from the section when connecting to the mirror.- url: The base URL of this mirror. The build system will use this URL to connect to a repository rather than the original repository URL.
- mirrorOf: The
id
of the repository that this is a mirror of. For example, to point to a mirror of the Mavencentral
repository (https://repo.maven.apache.org/maven2/
), set this element tocentral
. More advanced mappings likerepo1,repo2
or*,!inhouse
are also possible. This must not match the mirrorid
.
说明:
central
;*
;repo1,repo2
;*,!inhouse
;通常公司内部会搭建自己的Maven仓库,里面会发布一些内部软件包,所以我们可能需要配置多个仓库。
多个仓库的配置如下:
snapshots true nexus-aliyun http://maven.aliyun.com/nexus/content/groups/public nexus-myself http://localhost:8080/nexus/content/groups/public true always
说明:
updatePolicy说明:
配置多个仓库时,使用的优先级为配置顺序从上到下,第一个查找不到,则从接下来的仓库中查找,直至中央仓库,上面配置的仓库使用顺序为nexus-myself -> nexus-aliyun -> central
Maven通常将src/main/java和src/main/resources作为默认的源目录,并且只会编译src/main/java中的java文件,如果有xml等文件就会被忽略,然而有时我们又需要在src/main/java中放置一些其他类型文件,例如放置mybatis的mapper映射文件。
我们可以通过设置resources标签来控制编译的源目录
src/main/java **/*Mapper.xml src/main/resources
src--main----java------com.xxx.xxx------WebContent--------META-INF----------MANIFEST.MF--------WEB-INF----------lib----------web.xml----resources--test----java------com.xxx.xxxtargetpom.xml
其中src/main/java、src/main/resources、src/test/java为工程源目录。
遇到此问题需要确认各个部位使用的Java版本是否一致:
org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
这种情况是因为虚拟机运行参数没有设置maven的环境变量,解决方案如下:
一、在环境变量的系统变量中设置M2_HOME变量,值就是Maven的安装路径
二、如果是Eclipse可如下设置:
install jre -> edit -> default vm arguments添加**-Dmaven.multiModuleProjectDirectory=$M2_HOME**
问题原因:代码中使用了sun公司的第三方私有库,导致编译不通过
在pom文件中加入如下代码:
org.apache.maven.plugins maven-compiler-plugin 2.3.2
注意:中路径的分隔符,在Windows下是“;”,在Linux下是“:”。
原文链接:https://www.cnblogs.com/xiluhua/p/5621279.html
在pom.xml的properties标签下加入如下元素即可解决
-Dfile.encoding=UTF-8
mvn archetype:generate -DinteractiveMode=false -DarchetypeCatalog=internal -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.dongrui.study_poi -DartifactId=study-poi -Dpackage=com.dongrui.study_poi -Dversion=1.0