哈特中尉's Blog

不会写代码的司机不是好厨师!

centos7安装maven

下载安装包

1
wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz

安装

1
tar -zxvf apache-maven-3.6.3-bin.tar.gz

配置环境变量

1
2
export MAVEN_HOME=/usr/local/maven
export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin

刷新环境变量

1
source /etc/profile

修改maven配置文件

1
2
cd $MAVEN_HOME/conf
vi settings.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
......
<localRepository>/opt/mvnrepo</localRepository>
......

......
<mirrors>
<mirror>
<id>aliyun-maven</id>
<mirrorOf>*,!dxrj-maven</mirrorOf>
<name>aliyun-maven</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
......

windows配置文件参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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">

<localRepository>D:\\software\\mvnrepo</localRepository>

<pluginGroups>
</pluginGroups>

<proxies>
</proxies>

<servers>
<server>
<id>dxrj-maven-releases</id>
<username>dxrjmaven</username>
<password>dxrj#2020</password>
</server>
<server>
<id>dxrj-maven-snapshots</id>
<username>dxrjmaven</username>
<password>dxrj#2020</password>
</server>
</servers>
<mirrors>
<mirror>
<id>aliyun-maven</id>
<mirrorOf>*,!dxrj-maven</mirrorOf>
<name>aliyun-maven</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
<profiles>
</profiles>
</settings>

maven使用本地jar文件

1
2
3
4
5
6
7
<dependency>
<groupId>org.apache</groupId>
<artifactId>abc</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/abc-2.0.jar</systemPath>
</dependency>

安装jar到本地仓库

1
mvn install:install-file -DgroupId=com.alipay -DartifactId=sdk-java -Dversion=20170818173712 -Dpackaging=jar -Dfile=alipay-sdk-java20170818173712.jar

跳过test

mvn install -Dmaven.test.skip=true
或者

1
2
3
4
5
6
7
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

tomcat插件使用

1
2
3
4
5
6
7
8
9
10
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<uriEncoding>UTF-8</uriEncoding>
<path>/eap</path>
<finalName>eap</finalName>
</configuration>
</plugin>

创建maven项目

  • 缺省创建
    mvn archetype:generate
    所有参数按照提示输入
  • 创建JAVA SE项目
    1
    mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.banmaxia -DartifactId=map -Dpackage=com.banmaxia.map -Dversion=1.0
  • 创建JAVA EE项目
    1
    mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.banmaxia -DartifactId=map -Dpackage=com.banmaxia.map -Dversion=1.0 -DinteractiveMode=false

创建项目速度太慢的解决办法:

  • 1.下载http://repo1.maven.org/maven2/archetype-catalog.xml到本地maven仓库根目录。
  • 2.archetype:generate命令加上 -DarchetypeCatalog=local参数
1
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.banmaxia -DartifactId=map -Dpackage=com.banmaxia.map -Dversion=1.0 -DarchetypeCatalog=local

原因说明:创建时候会校验-DarchetypeArtifactId=maven-archetype-webapp的值,但是此文件很大,每次去网络上校验耗时,所以直接下载到本地,每次本地校验很快。