1、 Maven仓库
maven⼀共有三种仓库
-
本地仓库:⾃⼰维护
本地仓库的配置只需要修改 settings.xml ⽂件就可以 -
远程仓库(私服):公司维护
-
中央仓库:maven团队维护
中央仓库地址: https://mvnrepository.com/
2、 演示⼊⻔程序
2.1 Maven项⽬的⽬录结构
2.2 Maven的常⽤命令 – ⽣命周期
- clean 清理编译的⽂件
- compile 编译了主⽬录的⽂件
- test 编译并运⾏了test⽬录的代码
- package 打包
- install 就是把项⽬发布到本地仓库
- tomcat7:run ⼀键启动
3. Maven的⽣命周期
常⽤命令:compile test package install deploy(发布到私服)
三种⽣命周期:
- clean⽣命周期
clean - Default⽣命周期
compile、test、package、install、deploy - Site⽣命周期
site
命令和⽣命周期的阶段的关系:
不同的⽣命周期的命令可以同时执⾏
例如:mvn clean package
4 IDEA设置Maven环境并创建项⽬
- 设置maven安装包以及配置⽂件路径
2. 设置运⾏环境
-
创建maven项⽬【不使⽤模板】
-
编写pom.xml⽂件,设置 JDK 以及 tomcat 插件版本
<build>
<!--maven插件-->
<plugins>
<!--jdk编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<!--tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<!-- tomcat7的插件,不同tomcat版本这个也不⼀样 -->
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 通过maven tomcat7:run运⾏项⽬时,访问项⽬的端⼝号 -->
<port>80</port>
<!-- 项⽬访问路径 本例:localhost:80, 如果配置的aa,则访问路径为
localhost:80/aa-->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
- 编写pom.xml⽂件,添加依赖
<dependencies>
<!--junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
<scope>compile</scope>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
</dependencies>
5 依赖范围
- compile
编译(compile)时需要,测试(test)时需要,运⾏时需要,打包(package)时需要
例如:com.alibaba.druid.jar - provided
编译(compile)时需要,测试(test)时也需要 ,运⾏时不需要,打包(package)时不需要
例如:jsp-api.jar javax.servlet-api.jar - runtime
编译(compile)时不需要,测试(test)时需要,运⾏时需要,打包(package)时需要
例如:mysql-connector-java.jar - test
编译(compile)时需要,测试(test)时需要,运⾏时不需要,打包(package)时也不需要
例如:junit.jar