注:学习资料来源于行业前辈大牛:Davieyang.D.Y 所授,仅供个人学习,侵删。
Maven 的实际作用
Maven 是一个项目管理工具,它包含 了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Dependency Management System),和用来运行定义在生命周期阶段(phase)中插件(plugin)目标(goal)的逻辑。当你使用Maven 的时候,你用一个明确定 义的项目对象模型来描述你的项目,然后 Maven 可以应用横切的逻辑,这些逻辑来自一组共享的(或者自定义的)插件。
- 项目非常大时,可借助 Maven 将一个项目拆分成多个工程,最好是一个模块对应一 个工程,利于分工协作。而且模块之间还是可以发送消息的
- 借助 Maven,可将 jar 包仅仅保存在“仓库”中,有需要该文件时,就引用该文件接口,
不需要复制文件过来占用空间 - Maven 会自动将你要加入到项目中的 jar 包导入,不仅导入,而且还会将该 jar 包所 依赖的 jar 包都自动导入进来。
对于初学者,在此阶段Maven的作用我们可以粗浅的理解为帮我们搞定资源库的一个工具,类似于Python中 pip install xxx 模块到本地。
下载与配置
官方下载地址:maven
下载后解压,然后配置 MVN 系统环境变量环境变量,如下图所示:
变量名可用 M2_HOME 或者 MAVEN_HOME,只要自己能分清啥都行,变量值就是安装目录。
然后在系统变量path中添加变量值:
PATH:%M2_HOME%\bin;
检查 Maven 环境
配置仓库
配置本地仓库
配置 MVN 本地仓库物理地址,也就是 maven 会从中央仓库下载需要的 jar 包到本地,根据自己的安装,找到如下路径:
再此路径下打开 setting.xml,配置项如下,注意 XML 节点,配置成自己合适存放所下载 jar包的路径即可:
配置中心仓库
配置 MVN 中央仓库:将如下配置内容添加到镜像配置中:
此处单配置了一个镜像地址,是阿里云的地址,官方地址下载太慢可以通过这个位置的配置代替。
生成.m2 地址
将该文件复制如下路径下,如果没有.m2 路径,启动命令行执行命令 mvn help:system 来生成,然后将原来的配置文件复制到生成的.m2 路径下,集成开发工具,如 IDEA 等会自动读取.m2 路径下的配置。
Maven 常用命令
基本的常用命令如下:
- mvn archetype:create 创建 Maven 项目
- mvn compile 编译源代码
- mvn deploy 发布项目
- mvn test-compile 编译测试源代码
- mvn test 运行应用程序中的单元测试
- mvn site 生成项目相关信息的网站
- mvn clean 清除项目目录中的生成结果
- mvn package 根据项目生成的 jar
- mvn install 在本地 Repository 中安装 jar
- mvn eclipse:eclipse 生成 eclipse 项目文件
- mvnjetty:run 启动 jetty 服务
- mvntomcat:run 启动 tomcat 服务
- mvn clean package -Dmaven.test.skip=true:清除以前的包后重新打包,跳过测试类
配置 IDEA
IDEA 官方下载地址:https://www.jetbrains.com/idea/download/,默认安装即可,打开 IDEA,新建一个 maven 项目,检查 Maven 的配置如图所示,箭头所指两个配置已经更新到了前边步骤中的位置。
DEA 会在右下角(注意右下角)提示你是否需要自动安装 pom 配置的 jar 包,一旦出现,就要 enable 它。
配置 pom
找到 pom.xml 文件,打开它,配置项目所需的依赖包,如下图配置:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.uncommons/reportng -->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.inject/guice -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/velocity/velocity-dep -->
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity-dep</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
到此 Java 环境完成 Pom 中配置的依赖可以在 MVN 的中央库 http://mvnrepository.com/, 检索到你想要的包,配置进去即可。