项目管理——maven

maven是优秀的项目管理和构建工具,能让我们更为方便的来管理和构建项目,本文从最基础的环境配置到maven核心知识点的应用进行讲解。当然其他优秀的项目管理工具还有Ant、gradle等。

一、maven介绍及环境搭建

maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建、报告和文档的软件项目管理工具。简单地说maven可以帮助我们更有效的管理项目,它提供了一个仓库的概念,统一地为我们管理项目所依赖的第三方的jar包,最大可能的避免由于环境配置的不同所产生的在你的电脑上运行而在我的电脑上却不能运行的问题,很多开源的著名项目是用maven进行管理。如struts2

maven官网:http://maven.apache.org/点击download如下进行下载

将其解压到当前目录如下:

bin目录:包含mvn的运行脚本,在windows的命令行中输入mvn就会调用这些脚本

boot目录:包含一个类加载器的框架

conf目录:配置文件目录

lib目录:maven运行时所用到的所有类库,除了maven自身的还有其他maven运行时所依赖的第三方类库。

环境变量配置:

(1)环境变量-->系统变量-->新建系统变量 变量名:m2_HOME 变量值:maven的解压路径


(2)修改path:添加%m2_HOME%\bin


验证配置成功:


maven的目录结构


注意:

我们需要一个pom文件来管理我们的项目,我们可以从Struts的源代码将其找出来

如果是第一运行mvn compile这个命令它会在命令运行之前下载很多第三方的插件和maven所依赖的jar

二、maven核心知识

常用的构建命令介绍

mvn-v 查看maven版本

    compile 编译

    test 测试

    package 打包

    clean 删除target

    install 安装jar包到本地仓库中

自动创建目录骨架的两种方式:

1.  archetype:generate 按照提示进行选择

2.  archetype:generate –DgroupId=组织名,公司网址的反写+项目名

  -DartifactId=项目名-模块名

-Dversion=版本号

-Dpackage=代码所在的包名

maven中的坐标和仓库

坐标:构件通过坐标作为其唯一标识

构件:任何一个依赖、插件、项目构件的输出都可以被称为构件

仓库:本地仓库和远程仓库

镜像仓库

更改仓库位置

maven的生命周期和插件

完整的项目构件过程包括:

清理、编译、测试、打包、集成测试、验证、部署

在MyEclipse中配置自己下载的maven插件

这里我并没有使用MyEclipse自带的(高版本的myeclipse自带maven),具体可以参考:http://jingyan.baidu.com/article/1e5468f97c8972484861b768.html

maven生命周期

clean 清理项目

    pre-clean 执行清理前的工作

    clean清理上一次构件生成的所有文件

    post-clean执行清理后的文件

default 构件项目(最核心)

    compiletest package install

site 生成项目站点

pre-site 在生成项目站点前要完成的工作

site 生成项目的站点文档

post-site 在生成项目站点后要完成的工作

site-deploy发布生成的站点到服务器上

maven插件的相关使用

maven中Pom.xml解析

Pom.xml常用元素介绍

<?xml version="1.0" encoding="UTF-8"?>
<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">
	<!--指定了当前pom的版本-->
    <modelVersion>4.0.0</modelVersion>

	<groupId>反写的公司网址+项目名</groupId>
	<artifactId>项目名+模块名</artifactId>
	<!--第一个0表示大版本号
		第二个0表示分支版本号
		第三个0表示小版本号
		0.0.1
		snapshot 快照
		alpha 内部测试
		beta 公测
		Release 稳定
		GA 正式发布
	-->
	<version></version>
	<!--默认是jar
	 war zip pom
	-->
	<packaging></packaging>
	<!--项目描述名-->
	<name></name>
	<!--项目地址-->
	<url></url>
	<!--项目描述-->
	<description></description>
	<developers></developers>
	<licenses></licenses>
	<organization></organization>
	<dependencies>
		<dependency>
			<groupId></groupId>
			<artifactId></artifactId>
			<version></version>
			<type></type>
			<scope>test</scope>
			<!--设置依赖是否可选-->
			<optional></optional>
			<!--排除依赖传递列表-->
			<exclusions>
				<exclusion>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
	<!--依赖的管理-->
	<dependencyManagement>
		<dependencies>
			<dependency>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<build>
		<plugins>
			<plugin>
				<groupId></groupId>
				<artifactId></artifactId>
				<version></version>
			</plugin>
		</plugins>
	</build>
	<!--在子模块中对父模块的一个继承-->
	<parent></parent>
	<!--用来聚合多个的maven项目-->
	<modules>
		<module>
		</module>
	</modules>
</project>

依赖范围:


依赖传递:B依赖于A C依赖于B 则C依赖于A

依赖冲突:

1.  短路优先

A-->B-->C-->X(jar)

A-->D-->X(jar)

2.  先声明先优先

如果路径长度相同,则谁先声明,先解析谁

maven聚合和继承

三、使用maven构件web项目

我这里演示使用MyEclipse的maven插件构件web项目

Fiel-->new-->other-->MyEclipse -->Maven4MyEclipse-->Maven Project-->next

next


选择web app -->next

点击finish

默认生成的工程目录


按照maven的约定我们手动创建其他目录,右击项目—>new—>Source Folder


检查class文件的输出路径,项目右键—>Build Path—>ConfigureBuild Path—>Source


确保输出在target/classes目录中

将项目转化为web项目

项目右键-->属性


修改部署时的默认配置,删除测试时的文件(因为在最终发布产品时并不需要这些测试的文件)

这样我们的一个web项目创建成功了。

测试1:使用Tomcat作为web容器

修改tomcat下的conf下的tomcat-users.xml为

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<tomcat-users>
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
-->
<!--
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="admin" password="admin" roles="manager-gui,manager-script"/>
</tomcat-users>
webdemo Maven Webapp的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.davebobo.webdemo</groupId>
  <artifactId>webdemo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>webdemo Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>webdemo</finalName>
    <plugins>
        <plugin>
	        <groupId>org.apache.tomcat.maven</groupId>
	        <artifactId>tomcat7-maven-plugin</artifactId>
	        	 <configuration>
	        		<url>http://localhost:8080/manager/text</url>
					<username>admin</username>  
					<password>admin</password>
	        	</configuration>
        </plugin>
      </plugins>
  </build>
</project>
先启动tomcat服务

右击pom.xml--> run as -->maven build

打开浏览器进行访问

遇到问题可以参考:http://www.cnblogs.com/zhunian/p/4684802.html

项目管理——maven tomcat测试版本源码下载

测试2:使用jetty作为web容器


总结:

maven下载和环境搭建

maven目录骨架说明

pom.xml用于管理项目依赖和构建过程

常用的maven命令

仓库和坐标

在MyEclipse安装和创建maven项目

生命周期,pom.xml以及依赖、聚合、继承

Reference:

http://maven.apache.org/

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

http://mvnrepository.com/

http://mvnrepository.com/artifact/org.eclipse.jetty/jetty-maven-plugin

http://tomcat.apache.org/maven-plugin.html

http://blog.csdn.net/zzq900503/article/details/41249241

http://blog.csdn.net/testcs_dn/article/details/45225413

http://www.cnblogs.com/zhunian/p/4684802.html

http://www.cnblogs.com/liuconglin/p/5632027.html


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DaveBobo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值