目录

1. Maven基础知识

2. Maven实战。开发、测试、打包、部署一个Web项目

一、Maven基础知识

Maven坐标

Maven提供了一个中央仓库,里面包含了大量的开源软件的jar包,只需要在pom.xml中采取如下申明,Maven会自动检查本地仓库是否有次jar包,若没有,则会从中央仓库下载到本地。示例代码如下

    <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

Maven生命周期和插件

在Maven的使用过程中,命令行的输入实际上就对应了不同的生命周期,如mvn package命令则执行默认的生命周期package。Maven生命周期是抽象的,其实际行为都有插件完成,如package阶段任务可能由maven-jar-plugin完成。

生命周期就是指软件项目开发过程的不同阶段,Maven通过对常规项目周期进行抽象和统一,包含项目清理、初始化、编译、测试、打包、集成测试、验证、部署和站点生成等几乎所有步骤。

Maven中的生命周期不是一个整体,而是分为三套相互独立的周期,他们分别是clean、default和site。clean周期的目的是清理项目,default周期的目的是构建项目,site周期的目的是建立项目站点。

每个生命周期包含一些阶段(phase),这些阶段是有序的,并且后面的阶段依赖前面的阶段。以clean周期为例,它包含pre-clean、clean和post-clean阶段。当用户调用clean阶段时,pre-clean和clean阶段都会被调用。

下面是三个周期的详细阶段:

Maven profile

不同的环境中,配置文件可能不同,比如数据库配置信息,在开发阶段可能是直接创建连接的方式获取,生产阶段却通过JNDI的方式获取等。如果每次打不同的包时都手工处理话,效率低下不说,也容易出错,对于此种场景,Maven profile可以轻松搞定。

首先,看一段代码,pom.xml

    <profiles>
<!-- 开发 默认激活 -->
<profile>
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
<deploy.url>http://localhost:8080/manager/text</deploy.url>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 测试服务器 -->
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
<deploy.url>http://localhost:8080/manager/text</deploy.url>
</properties>
</profile>
<!-- 生产服务器 -->
<profile>
<id>pro</id>
<properties>
<profiles.active>pro</profiles.active>
<deploy.url>http://localhost:8080/manager/text</deploy.url>
</properties>
</profile>
</profiles>

二、Maven实战

1. 在Eclipse中创建基于Maven的项目,过程略。

2. 编写pom.xml文件,实现按不同环境打包,测试,部署到tomcat7,pom文件内容如下:

<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.yyn.maven</groupId>
<artifactId>BookSystem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <properties>
<spring.version>4.1.3.RELEASE</spring.version>
<aspectj.version>1.6.11</aspectj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.2.RELEASE</version>
</dependency> <!--AOP: Aspectjweaver.jar -->
<!--使用AspectJ方式注解需要相应的包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!--使用AspectJ方式注解需要相应的包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<!-- maven compile插件默认只支持jdk1.3,需手工配置支持1.5 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<!-- maven打jar包时,指定jar包中的main class,需要利用shade插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.yyn.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin> <plugin>
<!-- 3个可用插件 -->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<username>admin</username>
<password>admin</password>
<path>/BookSystem</path>
<uriEncoding>UTF-8</uriEncoding>
<update>true</update>
</configuration>
<!-- 运行redeploy命令前,要能正常访问http://localhost:8080/manager -->
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 资源根目录排除各环境的配置,使用单独的资源目录来指定,先排除掉所有,再添加 -->
<excludes>
<exclude>test/*</exclude>
<exclude>pro/*</exclude>
<exclude>dev/*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/${profiles.active}</directory>
</resource>
</resources>
</build> <profiles>
<!-- 开发 默认激活 -->
<profile>
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
<deploy.url>http://localhost:8080/manager/text</deploy.url>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 测试服务器 -->
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
<deploy.url>http://localhost:8080/manager/text</deploy.url>
</properties>
</profile>
<!-- 生产服务器 -->
<profile>
<id>pro</id>
<properties>
<profiles.active>pro</profiles.active>
<deploy.url>http://localhost:8080/manager/text</deploy.url>
</properties>
</profile>
</profiles> </project>

3. 在项目上右键-》Run As --》maven build,在弹出对话框的goals中输入tomcat7:redeploy命令即可完成自动部署。

4. 项目源码下载:http://pan.baidu.com/s/1pLM2xV9

项目管理01--使用Maven构建项目(纯干货)的更多相关文章

  1. 你竟然没用 Maven 构建项目?

    一年前,当我和小伙伴小龙一起做一个外包项目的时候,受到了严重的鄙视.我那时候还不知道 Maven,所以搭建项目用的还是最原始的方式,小龙不得已在导入项目的时候花了很长时间去下载项目依赖的开源类库. 出 ...

  2. Maven构建项目后项目报Error错误Java compiler level does not match the version of the installed Java project fac

    项目->右键->Project Facets->修改facets中Java版本(下拉箭头出)为要用的版本 Maven构建项目需注意 1.项目右键->Preferences-&g ...

  3. Maven学习3-使用Maven构建项目

    转自:http://www.cnblogs.com/xdp-gacl/p/4240930.html maven作为一个高度自动化构建工具,本身提供了构建项目的功能,下面就来体验一下使用maven构建项 ...

  4. Maven学习总结(三)——使用Maven构建项目

    maven作为一个高度自动化构建工具,本身提供了构建项目的功能,下面就来体验一下使用maven构建项目的过程. 一.构建Java项目 1.1.创建Java Project 1.使用mvn archet ...

  5. 转】Maven学习总结(三)——使用Maven构建项目

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4240930.html 感谢! maven作为一个高度自动化构建工具,本身提供了构建项目的功能,下面就来体验一下使 ...

  6. Maven构建项目比较慢的解决办法

    [前言] Intellij IDEA下Maven构建项目时,构建项目速度比较慢.需要等好久才能构建好一个项目,有时候一下午也还在提示进度. [问题描述] 使用Maven构建项目时: 设置好各种参数,然 ...

  7. Maven学习(三)-- 使用Maven构建项目

    摘自:http://www.cnblogs.com/xdp-gacl/p/4240930.html maven作为一个高度自动化构建工具,本身提供了构建项目的功能,下面就来体验一下使用maven构建项 ...

  8. (转)Maven学习总结(三)——使用Maven构建项目

    孤傲苍狼 只为成功找方法,不为失败找借口! Maven学习总结(三)——使用Maven构建项目 maven作为一个高度自动化构建工具,本身提供了构建项目的功能,下面就来体验一下使用maven构建项目的 ...

  9. 一:使用maven构建项目

    一般情况下:使用maven构建项目有两种情况: 1:用maven构建java项目: 2:用maven构建javaweb项目: 还有一种经常需要使用到的就是用maven构建项目模块:如:一个父项目用来作 ...

随机推荐

  1. codevs3410 别墅房间

    题目描述 Description 小浣熊松松到他的朋友家别墅去玩,发现他朋友的家非常大,而且布局很奇怪.具体来说,朋友家的别墅可以被看做一个N*M的矩形,有墙壁的地方被标记为’#’,其他地方被标记为’ ...

  2. 5-45 航空公司VIP客户查询 (25分) HASH

    不少航空公司都会提供优惠的会员服务,当某顾客飞行里程累积达到一定数量后,可以使用里程积分直接兑换奖励机票或奖励升舱等服务.现给定某航空公司全体会员的飞行记录,要求实现根据身份证号码快速查询会员里程积分 ...

  3. BZOJ(2) 1041: [HAOI2008]圆上的整点

    1041: [HAOI2008]圆上的整点 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4966  Solved: 2258[Submit][Sta ...

  4. Ubuntu 16.04安装7zip

    搜索时发现7zip有两个:p7zip,p7zip-full,p7zip-rar.研究发现: p7zip:包含7zr(最小的7zip归档工具),仅仅只能处理原生的7z格式. p7zip-full:包含7 ...

  5. Spring Cloud ZooKeeper集成Feign的坑1,错误:Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.

    错误如下: ERROR 31473 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** A ...

  6. firebug 扩展介绍和下载

    https://getfirebug.com/wiki/index.php/Firebug_Extensions#Firecookie https://addons.mozilla.org/zh-CN ...

  7. Java Map 怎样实现Key 的唯一性?

    大家都知道.在Map和Set不可存在反复元素? 可是对于内部的细节我们并不了解.今天我们就一块来 探讨一下! 1 对于 HashMap  HashSet 他们的底层数据结构的实现是:维护了一张  Ha ...

  8. [dfs] UVALive 3667 Ruler

    题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=1668">https://ic ...

  9. cocos2d-x之android编译环境搭建(第二篇)[版本号:cocos2d-x-3.1.1]

    基于 Android NDK 的学习之旅-----环境搭建 工欲善其事 必先利其器 , 以下介绍下 Eclipse SDK NDK Cygwin CDT 集成开发环境的搭建. 1.Android 开发 ...

  10. error: &#39;Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysql.sock&#39; (2)&#39;

    [root@luozhonghua ~]#   /usr/bin/mysqladmin -u root password 'aaaaaa' /usr/bin/mysqladmin: connect t ...