maven工程:只需要在pom.xml中配置包名,就会自动导入配置好所需的包,不需要手动找包再一个一个导入,也可以把各个模块分开管理,降低耦合度
搭建环境:
1.下载maven的压缩包,并解压到除了D盘里的maven文件夹中
2.在解压后的maven同级目录下新建一个mvnrepo文件,用于存放各种jar包
3.进入解压后的文件夹的–>conf–>settings.xml文件,用记事本工具打开,(1)设置默认保存jar包的仓库的位置:修改如下代码,为mvnrepo文件的根路径
mvnrepo的路径为
如有需要也可以修改其他的
例如:
(1)配置内网镜像服务器 nexus服务器 、配置外网服务器(国内镜像)、配置外网服务器(国外镜像)
(2)、配置jdk
如需修改为jdk 1.8 则在引用代码中、或者在settings.xml文件 修改为如下代码
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
4.配置环境变量
(1)变量名为:M2_HOME
变量值为(为路径:解压后apache-maven的根目录):D:\maven\apache-maven-3.2.5
(2)在path中添加maven的bin路径:
变量名:Path
变量值(在path中添加maven的bin路径):%M2_HOME%\bin;
5.在cmd命令行模式下测试是否搭建环境成功: mvn -version
如下图所示:安装成功
6.打开eclipse:把maven管理进来(与eclipse 中把Tomcat管理进来类似)
(1)windows–>preferences–>maven:勾上第四个框,表示下载源文件,如下图
(2)点进maven下的instalations,完成配置点击apply应用,不使用内嵌的,使用刚刚下载的apache-maven-3.2.5,add—>找到自己下载的apache-maven-3.2.5路径
然后打钩选上
(3)点进maven下的user settings,完成配置点击apply应用,不使用默认的路径,把settings.xml 管理到我们自己的mvnrepo 仓库中
7.建立maven项目
点击file–>new–>others
项目如下:
更新项目如下图所示:
报错原因:因为web 工程至少有一个WEB-INF 文件夹下,和其下的目录web.xml 才是Web工程,所以在webapp在新建 WEB-IN 文件 和它的子目录web.xml 文件
8.从该网址中找到jar包的相应代码复制到该xml文件中,保存,就能在仓库和目录中看到下载好的jar包了。阿里云maven仓库:
https://maven.aliyun.com
下图示例两个jar包:
pom.xml 配置关系如下
在阿里云maven仓库中到到对应的jar 依赖代码复制过来:如下图
如果不知道使用那些jar 名称可以百度查查,使用什么需要导入那些jar ,再去阿里云maven 仓库搜索
对应的配置的坐标的所有jar包就会自动出来了,如下图
9.配置环境 pom.xml
解决报错:项目名称右键----> maven ---->Update MaVen Project 就会自动更新修改过的pom.xml 文件
10.例子
pom.xml代码如下
<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.mmm</groupId>
<artifactId>ssm_maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!--配置依赖 -->
<dependencies>
<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.10.0</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.12.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.12.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.12.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.12.Release</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.12.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.12.Release</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 配置数据库环境 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<!-- 配置build环境 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</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>ssm_maven</path>
</configuration>
</plugin>
</plugins>
</build>
</project>