前言
Apache Maven是一个软件项目管理和理解工具。基于项目对象模型(POM)的概念,Maven可以从一个中心信息管理项目的构建,报告和文档。
以上是抄官网的,我也不是很懂。总之,个人感觉,Maven能极大地简化从开发到编译再到部署的过程。但是由于国内网络环境,在使用maven的过程中体验极差。所以我写这一篇博文记录我的修改maven过程中的踩坑经历。本篇开发工具均为vs code。
下载速度慢
在vsc下使用Maven原型生成项目,选择quickstart。你会发现它会卡住一会,2-6分钟不等。去网上查了查原因,发现是因为国内网络环境造成。解决方案大多是将Maven根目录下配置文件加个阿里镜像(阿里牛逼):
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
这段语句的作用是maven下载central(Maven的中央仓库)时,转用阿里云镜像下载。
找不到原型
其实大多数人到这里就可以正常使用了,因为我换了台电脑,而没有使用central的情况下就配置了阿里云。而阿里云的仓库貌似有点问题,具体我也不是很明白,想了解更多参见:使用aliyun作为maven源
通过-X输出Debug可以发现,maven在生成原型时,先在本地cache找到之前下载的,跟服务器上的做对比,再去选择下载服务器还是就用本地的。我本地并没有,所以它就去服务器上下载了,而因为阿里云仓库问题,下载出的pom文件并没有编译等插件,还得自己手动来添加。于是我去查了查maven的服务器下载顺序,写出了另外一种配置:
使用profile,配置阿里云服务,这样它就会在找不到的时候再去中央仓库下载,而不是就认准这一个了。具体代码如下:
<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
如果你要配置多个profile,还需要在下面激活一下:
<activeProfiles>
<activeProfile>aliyun</activeProfile>
<!-- <activeProfile>central</activeProfile>
<activeProfile>jdk-11</activeProfile> -->
<!-- 我有三个配置,所以我需要来激活 -->
</activeProfiles>
它还是慢啊
就如同我说的,它在阿里云里面找不见了,就去central里找,但是如果去了central,就又回到我们的根本问题上了。它下载还是慢QAQ,在这里我选择配置代理,不会科学上网的同学跳过这一步吧,委屈一下自己也没啥。在maven根目录settings.xml文件中加入:
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>127.0.0.1</host>
<port>1080</port>
<!-- <username>username</username> -->
<!-- <password>password</password> -->
<!-- 上面两行根据自己需要填写 -->
</proxy>
这样就可以了。
最后
我就是一小白,上面理论大多都是我自行看文档摸索的,难免会有错误。如果该博文能有幸被大佬所看见,还望大佬能够批评指正一下。
关于配置文件可以参考官网案例