###一、下载并安装 ###二、配置环境变量 MAVEN_HOME/bin 查看版本信息:`mvn -version` 进入项目根目录,输入 `mvn compile` 进行编译,如果出现build success,则编译成功。 输入 `mvn test` 测试 输入 `mvn package` 进行打包 ###三、常见的构建命令 ``` mvn -v 查看版本 compile 编译 test 测试 package 打包 clean 删除target install 安装jar包到本地仓库中 mvn clean compile 再次编译 ``` ``` #自动建立目录架构 #mvn archetype:generate -DgroupId=组织名,公司网址的反写+项目名 -DartifactId=项目名-模块名 -Dversion=版本号 -Dpackage=包名 #方式一 --> 按照提示进行选择 mvn archetype:generate Define value for property 'groupId': : com.lgd.maven.maventest Define value for property 'artifactId': : maventest Define value for property 'version': 1.0-SNAPSHOT: : 1.0.0SNAPSHOT Define value for property 'package': com.lgd.maven.maventest: : com.lgd.maven.demo Confirm properties configuration: groupId: com.lgd.maven.maventest artifactId: maventest version: 1.0.0SNAPSHOT package: com.lgd.maven.demo Y: : y #方式二 --> 一次性写完 mvn archetype:generate -DgroupId=com.lgd.maven.maventest -DartifactId=maventest -Dversion=1.0.0SNAPSHOT -Dpackage=com.lgd.maven.demo ``` ``` #坐标 构件 #仓库 本地仓库和远程仓库 #镜像仓库-->一旦配置了镜像仓库,所有针对原仓库的访问都会转到镜像仓库,原仓库将不能再直接访问。 ${MAVEN_HOME}/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> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>maven.oschina.net</id> <!--镜像仓库的ID--> <mirrorOf>central</mirrorOf> <!--为那个仓库配置镜像 可配置为[ * ]为每个仓库配置镜像--> <name>central mirror in oschina</name> <url>http://maven.oschina.net/content/groups/public/</url> </mirror> </mirrors> # 更改本地仓库的位置 ${MAVEN_HOME}/conf/settings.xml文件下 <localRepository>D:/path/to/local/repo</localRepository> 将settings.xml文件放在D:/path/to/local/repo路径下,这样我们更新版本的时候,就可以不用再更改settings.xml文件。 # 设置默认的JDK版本 ${MAVEN_HOME}/conf/settings.xml文件下 <profiles> <profile> <id>jdk-1.7</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.7</jdk> </activation> <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion> </properties> </profile> </profiles> ``` ###四、Maven的生命周期与插件 完整的项目构建过程包括:清理,编译,测试,打包,集成测试,验证,部署。 ####Maven生命周期 clean 清理项目 ---> pre-clean 执行清理前的工作 ---> clean 清理上一次构建生成的所有文件 ---> post-clean 执行清理后的工作 default 构建项目(最核心) ---> compile test package install site 生成项目站点 ---> pre-site 在生成项目站点前要完成的工作 ---> site 生成项目的站点文档 ---> post-site 在生成项目站点后要完成的工作 ---> site-deploy 发布生成的站点到服务器上 ###五、pom.xml文件解析 ``` <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--指定当前pom的版本--> <modelVersion>4.0.0</modelVersion> <!--反写的公司网址+项目名--> <groupId>com.lgd.frames</groupId> <!--项目名+模块名--> <artifactId>java-frames</artifactId> <!-- 第一个0表示大版本号 第二个0表示分支版本号 第三个0表示小版本号 0.0.1 snapshot 快照 alpha 内部测试 beta 公测 Release 稳定 GA 正式发布 --> <version>1.0.0-SNAPSHOT</version> <!--- 默认是jar war zip pom --> <packaging>pom</packaging> <!--项目描述名--> <name></name> <!--项目地址--> <url></url> <!--项目描述--> <description></description> <!--开发人员列表--> <developers></developers> <!--许可证信息--> <licenses></licenses> <!--组织--> <organization></organization> <dependencies> <dependency> <!--坐标--> <groupId></groupId> <artifactId></artifactId> <!--范围--> <version></version> <!--依赖范围 eg:test 只在测试中有用--> <scope></scope> <!--设置依赖是否可选,默认为false--> <optional></optional> <!--排除依赖传递列表--> <exclusions> <exclusion> </exclusion> </exclusions> </dependency> </dependencies> <!--依赖的管理--> <dependencyManagement> <dependencies> <dependency> </dependency> </dependencies> </dependencyManagement> <build> <!--插件列表--> <plugins> <plugin> <!--坐标--> <groupId></groupId> <artifactId></artifactId> <!--范围--> <version></version> </plugin> </plugins> </build> <!--通常用于在子模块中,对父模块的一个继承--> <parent></parent> <!--用来聚合运行多个maven项目--> <modules> <module> </module> </modules> </project> ``` 依赖范围 ``` <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> ``` Maven为我们提供了3种classpath范围 编译、测试、运行 上述表名junit只存在于测试的classpath范围中 scope有6种值: compile,provided,runtime,test,system,import compile 默认的范围,编译测试运行都有效。 provided 编译和测试时有效 runtime 在测试和运行时有效 test 测试范围有效 system 编译和测试有效,与本机系统相关联,可移植性差 import 导入的范围,它只使用在dependencyManagement中,表示从其他的pom中导入denpendency的配置 依赖传递 比如:A依赖于B,B依赖于C,所以A间接依赖于C 如果不想让A依赖于C ``` <dependency> <groupId>com.demo</groupId> <artifactId>B</artifactId> <version>1.0.0-SNAPSHOT</version> <exclusions> <exclusion> <groupId>com.demo</groupId> <artifactId>C</artifactId> <version>1.0.0-SNAPSHOT</version> </exclusion> </exclusions> </dependency> ``` 依赖冲突-->可能存在不同版本的冲突 解决冲突的两个原则 1、短路优先 2、先声明先优先 如果路径长度相同,则谁先声明,先解析谁。 聚合和继承 ``` <packaging>pom</packaging> <modules> <module>MybatisProject</module> <module>SpringProject</module> <module>EncryptionAndDecryption</module> <module>MavenProject</module> <module>SSMUnion</module> <module>CollectionProject</module> <module>JavaPage</module> <module>MavenInstallProject</module> </modules> ``` 将多个模块聚合在一起。 ``` <groupId>com.lgd.parent</groupId> <artifactId>java-parent</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <!--依赖的管理--> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> ``` ``` <parent> <groupId>com.lgd.parent</groupId> <artifactId>java-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> ```