Maven通过项目中的pom.xml文件,对项目引用的jar、测试、环境、构建生成等进行配置。
一、一个pom.xml文件通常包括三个部分:头部、依赖部、构建生成部。为了对pom.xml更好的操作,建议使用Eclipse 4.3以上版本。
1、头部
设置maven识别的项目信息,如groupId、artifactId、packaging、version、name、url、developers等,一些描述性的
2、依赖部
SpringMVC写REST接口,引用的包比较少,就以下几个部分:
A、测试:junit
B、spring框架:spring-webmvc、spring-test。spring框架的其它包,在spring-webmvc都包含了,不用再引用。
C、mysql驱动 :mysql-connector-java、mybatis-spring
D、连接池:c3p0
E、json解析器:org.codehaus.jackson的jackson-core-asl、jackson-core-lgpl、jackson-mapper-asl、jackson-mapper-lgpl
F、文件上传:commons-io、commons-fileupload
G、HTTP客户端:httpclient
H、日志:log4j
I、环境:jstl、standard、javaee-api
3、构建生成部
设置项目的生成目标、资源以及Maven插件.
二、完整的SpringMVC写REST的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jiahe.rest</groupId> 1、 修改为项目的groupId
<artifactId>demo2</artifactId> 2、修改为项目的artifactId
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version> 3、修改为项目的版本
<name>demo2 Maven Webapp</name> 4、修改为项目的name
<url>http://maven.apache.org</url>
<developers>
<developer>
<name>lxh</name>
<email>[email protected]</email>
</developer>
</developers>
<dependencies>
<!-- 测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- spring框架 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- json解析器 -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.9.11</version>
</dependency>
<!-- 文件上传 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.2</version>
</dependency>
<!-- log日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
<!--servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>demo2</finalName> 5、修改为项目生成的war包的名称
<outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<webResource>
<!-- 元配置文件的目录,相对于pom.xml文件的路径 -->
<directory>src/main/webapp/WEB-INF</directory>
<targetPath>WEB-INF</targetPath>
<includes>
<include>**/*.xml</include>
</includes>
</webResource>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<webappDirectory>src/main/webapp</webappDirectory>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
综上,只需要修改5个地方,SpringMVC写REST的pom.xml就配置完整了。这里需要注意mybatis-spring的版本,它决定了spring框架的版本。