本章节教学视频位置: https://pan.baidu.com/s/1_TiILfFGKD4JY7FTEQ9kNQ
Maven: 用来管理项目依赖,构件
maven中央库:http://repo1.maven.org/maven2/
maven国内镜像:http://maven.aliyun.com/nexus/content/groups/public
maven本地库:
查询maven依赖(http://mvnrepository.com/)
在maven库中,每个maven构建由Group Id, Artifact Id, Version唯一标识。
Maven环境搭建
1. 下载apache-maven-3.5.0-bin.zip
2. 修改conf目录下的setting.xml
//设置maven本地库
~~~
<localRepository>d:/.m2/repository</localRepository>
<!--设置maven镜像(从哪下载)-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
~~~
3. 在eclipse中配置maven
![](https://box.kancloud.cn/6fb979f1b987c8ebc8f2f14a73504544_558x413.png)
4. 创建maven工程(create a simple project(skip archetype selection))
填写Group Id, Artifact Id, Version
![](https://box.kancloud.cn/00b96d185cd6ebf83c6f3b248c9d24b6_558x426.png)
注意:第一个使用maven创建工程会比较慢,因为需要联网下载创建maven工程所必须的构建。
工程类型为war的工程需要web.xml(工程右键:JAVAEE Tools->Generate Deployement Descriptor Stub)
5. 编写POM文件(depencies & build)
~~~
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.neuedu.test</groupId>
<artifactId>myvote</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!--配置依赖 -->
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!--<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> -->
<!-- 配置spring基础依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!-- 配置spring数据库连接池 -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.0</version>
</dependency>
<!-- 配置springmvc功能 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!-- 配置springmvc文件上传 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 配置spring jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.3</version>
</dependency>
<!-- 配置mybatis -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<!-- 配置mybatis和spring整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.4</version>
</dependency>
<!-- 配置mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!--配置日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<!-- <scope>test</scope> -->
</dependency>
</dependencies>
<!-- 配置build环境 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/myvote</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
~~~
注意:第一次maven build会比较慢,因为需要联网下载所有依赖。
若想使用jdk1.8, tomcat8, 请参考https://blog.csdn.net/wild46cat/article/details/52412330
运行maven工程(2种方式)
1. 原来的方式,设置jdk, 部署tomcat
2. Maven命令方式:
工程右键-》run as maven build -> tomcat7:run