1.安装Maven
1)官网下载Maven : http://maven.apache.org/download.cgi,解压下载文件
2)配置环境变量
3)验证是否已经安装成功:打开cmd,输入mvn -version
2.Maven的setting.xml文件配置
修改Maven安装路径的config目录的settings.xml文件影响所有用户的Maven操作,在c盘用户目录${user.home}/.m2目录下新建settings.xml文件影响当前用户的操作
1)修改下载仓库。 从中央仓库下载出现异常,可以修改仓库,下面的例子是使用新的仓库,且需要用户名密码登陆的例子
<servers> <server> <id>nexus</id> <username>userName</username> <password>Password</password> </server> </servers> <!-- mirrors | This is a list of mirrors to be used in downloading artifacts from remote repositories. | | It works like this: a POM may declare a repository to use in resolving certain artifacts. | However, this repository may have problems with heavy traffic at times, so people have mirrored | it to several places. | | That repository definition will have a unique id, so we can create a mirror reference for that | repository, to be used as an alternate download site. The mirror site will be the preferred | server for that repository. |--> <mirrors> <mirror> <id>nexus</id> <name>Nexus</name> <url>mirrorURL</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> <!-- profiles | This is a list of profiles which can be activated in a variety of ways, and which can modify | the build process. Profiles provided in the settings.xml are intended to provide local machine- | specific paths and repository locations which allow the build to work in the local environment. | | For example, if you have an integration testing plugin - like cactus - that needs to know where | your Tomcat instance is installed, you can provide a variable here such that the variable is | dereferenced during the build process to configure the cactus plugin. | | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles | section of this document (settings.xml) - will be discussed later. Another way essentially | relies on the detection of a system property, either matching a particular value for the property, | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'. | Finally, the list of active profiles can be specified directly from the command line. | | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact | repositories, plugin repositories, and free-form properties to be used as configuration | variables for plugins in the POM. | |--> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>nexus</id> <name>Nexus</name> <url>mirrorURL/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <name>Nexus</name> <url>mirrorURL/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles>
2)修改本地仓库,默认本地仓库为${user.home}/.m2/repository/
<settings> ... <localRepository>/path/to/local/repo/</localRepository> ... </settings>
官方配置教程:https://maven.apache.org/guides/mini/guide-configuring-maven.html
3.Maven常用命令
mvn compile:
下载项目依赖的jar包
编译项目主代码${basedir}/scr/main/java目录下的代码,并且将生成的calss文件复制到target/classes目录下
将${basedir}/scr/main/resources目录下的文件拷贝至target/classes目录
mvn test:
执行以上步骤,下载项目运行需要的dependency与plugins(如果已存在就不下载),编译且测试执行test-classes目录下的class文件
mvn package:执行以上步骤,并且将项目打包生成jar包文件放在${basedir}/target目录下
mvn install:执行以上几个环节,并且将jar文件拷贝至本地仓库,默认为${user.home}/.m2/repository
mvn clean:清理输出目录${basedir}/target ( ${basedir}为pom.xml文件所在目录,项目根目录)
4.Maven pom.xml文件
1) dependency的命名规则:groupId:artifactId:packaging:classifier:version
2) maven-compiler-plugin的compile目标用来编译位于src/main/java/
目录下的主源码,testCompile目标用来编译位于src/test/java/
目录下的测试源码。