一、修改settting.xml文件

1、设置本地仓库

<localRepository>D:/repository</localRepository>

2、修改jdk版本

<!--修改jdk版本-->
<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>

3、 修改镜像地址,添加阿里云镜像地址,把访问速度快的配置在前

<!-- 阿里云仓库 -->
<mirror>
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>   
<!-- 中央仓库2 -->
<mirror>
    <id>repo2</id>
    <mirrorOf>central</mirrorOf>
    <name>Human Readable Name for this Mirror.</name>
    <url>http://repo2.maven.org/maven2/</url>
</mirror>
<!-- 中央仓库1 -->
<mirror>
	<id>repo1</id>
	<mirrorOf>central</mirrorOf>
	<name>Human Readable Name for this Mirror.</name>
	<url>http://repo1.maven.org/maven2/</url>
</mirror>

二、创建好maven项目后,配置pom.xml

1、配置全局jar包版本

<properties>
	<servlet-version>3.0.1</servlet-version>
	<jsp-version>2.2</jsp-version>
	<jstl-version>1.2</jstl-version>
	<spring-version>4.1.6.RELEASE</spring-version>
	<aspectjweaver-version>1.8.6</aspectjweaver-version>
	<mybatis-version>3.2.7</mybatis-version>     
	<mybatis-spring-version>1.2.3</mybatis-spring-version>
	<log4j-version>1.2.17</log4j-version>
	<mysql-connector-java-version>5.1.38</mysql-connector-java-version>
	<jackson-version>2.4.1</jackson-version>
	<commons-fileupload-version>1.3.1</commons-fileupload-version>
</properties>

2、配置常用jar包

<dependencies>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>javax.servlet-api</artifactId>
		<version>${servlet-version}</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>javax.servlet.jsp</groupId>
		<artifactId>jsp-api</artifactId>
		<version>${jsp-version}</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
		<version>${jstl-version}</version>
	</dependency>
	<!-- spring -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>${spring-version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jdbc</artifactId>
		<version>${spring-version}</version>
	</dependency>
	<dependency>
		<groupId>org.aspectj</groupId>
		<artifactId>aspectjweaver</artifactId>
		<version>${aspectjweaver-version}</version>
	</dependency>
	<!-- mybatis -->
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>${mybatis-version}</version>
	</dependency>
	<!-- mybatis和spring整合 -->
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis-spring</artifactId>
		<version>${mybatis-spring-version}</version>
	</dependency>
	<!-- log4j 日志 -->
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>${log4j-version}</version>
	</dependency>
	<!-- mysql 驱动类 -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>${mysql-connector-java-version}</version>
	</dependency>
	<!-- jackson -->
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-databind</artifactId>
		<version>${jackson-version}</version>
	</dependency>
	<!-- apache 图片上传 -->
	<dependency>
		<groupId>commons-fileupload</groupId>
		<artifactId>commons-fileupload</artifactId>
		<version>${commons-fileupload-version}</version>
	</dependency>
</dependencies>

3、配置编译资源路径

如果某些配置文件(如:pojoMapper.xml文件)没有放到src/main/resources中,而是放在了src/main/java目录下,项目编译的时候只会编译src/main/java目录下的.java文件,而xml等文件会被忽略,如果仍想编译它们,需要在build标签下面配置被编译的资源,把src/main/java也加入到资源目录。(后来又经过几次测试,貌似有的时候不配也能编译成功,看情况吧,如果不行就加上这个配置)

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
        	<include>**/*.xml</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.xml</include>
            <include>**/*.properties</include>
        </includes>
    </resource>
</resources>

4、在build标签下面配置tomcat插件

<plugins>
    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
            <!-- 控制tomcat端口号 -->
            <port>80</port>
            <!-- 项目发布到tomcat后的名称 -->
            <!-- / 相当于把项目发布名称为ROOT -->
            <!-- /abc -->
            <path>/</path>
        </configuration>
    </plugin>
</plugins>

5、启动项目

右键项目->run as->maven build…->在goal中输入tomcat7:run,这时候会将项目部署到maven的tomcat插件中,部署速度相当快,然后打开浏览器就可以访问了。

三、使用maven将项目热部署到对应的服务器

1、先修改tomcat目录下面的conf下面的tomcat-user.xml文件,加上如下内容:

<role rolename="manager-gui"/> 
<role rolename="manager-script"/>
<user password="candywall" username="admin"

roles=“manager-gui,manager-script,manager-jmx,manager-status” />

2、修改pom.xml文件中tomcat插件的配置信息

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <!-- 控制tomcat端口号 -->
        <port>8080</port>
        <!-- 项目发布到tomcat后的名称 -->
        <!-- / 相当于把项目发布名称为ROOT -->
        <!-- /abc -->
        <path>/Sample_Data_Storage</path>
        <username>admin</username>
        <password>candywall</password>
        <url>http://192.168.98.101/manager/text</url>
    </configuration>
</plugin>

3、此时如果在setting.xml也配置了tomcat的登录用户信息,如下所示:

<server> 
     <id>tomcat8</id>
     <username>admin</username>
     <password>admin</password>
</server>

则可以在pom.xml中配置tomcat插件的时候,也可以直接引用server的id号

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <!-- 控制tomcat端口号 -->
        <port>8080</port>
        <!-- 项目发布到tomcat后的名称 -->
        <!-- / 相当于把项目发布名称为ROOT -->
        <!-- /abc -->
        <path>/Sample_Data_Storage</path>
        <server>tomcat8</server>
        <url>http://192.168.98.101/manager/text</url>
    </configuration>
</plugin>
setting.xml中的设置是全局的,都可以在pom.xml中重新配置,起到覆盖的作用。

注意 项目配置tomcat插件时,port配置的端口号,在使用热部署命令时是不起作用的,以它使用的是服务器独立的tomcat(可以是本地的tomcat也可是远程服务器上的tomcat)的server.xml中配置的<Connector port=“8080” 端口为准。
但是在Eclipse中使用maven的tomcat7:run命令启动项目的时候访问该项目的端口号以port设置为准,设置的端口是多少,访问该项目时要用的端口号就是多少。(因为此时使用的maven的tomcat插件,而并不是独立的tomcat容器)

4、最后贴上完整的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>top.jacktgq</groupId>
    <artifactId>Sample_Data_Storage</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>网服中心</name>
    <description>收集故障诊断的数据</description>
    <properties>
		<servlet-version>3.0.1</servlet-version>
		<jsp-version>2.2</jsp-version>
		<jstl-version>1.2</jstl-version>
		<spring-version>4.1.6.RELEASE</spring-version>
		<aspectjweaver-version>1.8.6</aspectjweaver-version>
		<mybatis-version>3.2.7</mybatis-version>     
		<mybatis-spring-version>1.2.3</mybatis-spring-version>
		<log4j-version>1.2.17</log4j-version>
		<mysql-connector-java-version>5.1.38</mysql-connector-java-version>
		<jackson-version>2.4.1</jackson-version>
		<commons-fileupload-version>1.3.1</commons-fileupload-version>
		<dbutils-version>1.6</dbutils-version>
		<druid-dbpool-version>1.1.21</druid-dbpool-version>
		<excel-poi-version>4.0.1</excel-poi-version>
    </properties>
    <dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet-version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp-version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl-version}</version>
		</dependency>
		
		<!-- spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${aspectjweaver-version}</version>
		</dependency>
		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis-version}</version>
		</dependency>
		<!-- mybatis和spring整合 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>${mybatis-spring-version}</version>
		</dependency>
		<!-- log4j 日志 -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j-version}</version>
		</dependency>
		<!-- mysql 驱动类 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector-java-version}</version>
		</dependency>
		<!-- jackson -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson-version}</version>
		</dependency>
		<!-- apache 图片上传 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>${commons-fileupload-version}</version>
		</dependency>
	    <!-- 操作excel -->
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi</artifactId>
		    <version>${excel-poi-version}</version>
		</dependency>
		<!-- dbutils -->
		<dependency>
		    <groupId>commons-dbutils</groupId>
		    <artifactId>commons-dbutils</artifactId>
		    <version>${dbutils-version}</version>
		</dependency>
		<!-- 阿里巴巴连接池:druid -->
		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>druid</artifactId>
		    <version>${druid-dbpool-version}</version>
		</dependency>
    </dependencies>
    <build>
	  	<plugins>
	  		<plugin>
	  			<groupId>org.apache.tomcat.maven</groupId>
	  			<artifactId>tomcat7-maven-plugin</artifactId>
	  			<version>2.2</version>
	  			<configuration>
	 				<!-- 控制tomcat端口号 -->
					<port>8080</port>
					<!-- 项目发布到tomcat后的名称 -->
					<!-- / 相当于把项目发布名称为ROOT -->
					<!-- /abc -->
					<path>/Sample_Data_Storage</path>
					<username>admin</username>
					<password>candywall</password>
					<url>http://192.168.98.101/manager/text</url>
	  			</configuration>
	  		</plugin>
	  	</plugins>
    </build>
</project>