安装maven,我的系统是unix,按理说安装了Xcode会自带maven,据说在/usr/share/java中,但是我没有找到,所以自己安装。
首先去官网下载maven,地址:https://maven.apache.org/download.cgi。
我的版本为apache-maven-3.3.9-bin.tar.gz, 下载之后解压到自己喜欢的文件夹就好。
然后在终端输入命令:cd ~ 进入根目录
编辑文件: vi .bash_profile 没有就先建一个。
在文件中加入
MAVEN_HOME=/usr/xxx/maven-3.3.9 //解压maven的路径
PATH=$MAVEN_HOME/bin:$PATH
export MAVEN_HOME
export PATH
相当于windows的环境变量配置了,配好之后直接可用 mvn -vesion来查看版本。
maven的使用:
常用命令:compile 编译
package 打包
install 将项目打成jar包放入本地仓库
clean 删除target
项目的构建:
使用maven构建web 项目,首先进入项目的根目录。
使用命令:mvn archetype:generate -DgroupId=组织名 -DartifactId=项目名_模块名 -Dversion=版本号 -Dpackage=代码所存在的包名
例如:mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -Dpackage=com.xxx.java
初次建立项目会需要去中央仓库去下载一些相关的依赖包。会花比较长的时间。下载到本地仓库之后就不需要再次下载了。
maven默认的中央仓库是英国,所以因为国内访问外网不方便的原因可能导致下载失败或者及其的慢,所以我们可以把中央仓库改为国内的。
在/maven_3.3.9/conf/settings.xml中,有配置镜像地址的示例,
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
|
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
</mirrors>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
被注释的部分就是配置镜像地址方式,将注释去掉,配置修改为如下:
<mirror>
<id>maven.net.cn</id>
<mirrorOf>central</mirrorOf>
<name>mirror in china</name>
<url>http://maven.net.cn/content/groups/public/</url>
</mirror>
其中的url就是国内的中央仓库地址,其中包含了大部分开源的第三方库和框架。
安装jar包到本地仓库命令
mvn install:install-file -Dfile=commons-httpclients-3.1.jar -DgroupId=commons-httpclient -DartifactId=commons-httpclient -Dversion=3.1 -Dpackaging=jar