Maven管理工具的基本使用

1.Maven  Ant Gradle
2.maven介绍
maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建、报告和文档的软件项目管理工具。
http://maven.apache.org/
3.Maven的目录结构
|-bin       包含mvn的运行脚本
|
|-boot      包含一个类加载器的框架
|
|-conf      配置文件目录
|
|-lib       Maven运行时所依赖的类库,包括一些第三方的类库

4.Maven的环境变量配置
M2_HOME     D:\maven\apache-maven-3.3.9
Path        ;%M2_HOME%\bin

验证是否配置成功: mvn -v
5.Maven创建的目录
src
-main
---java
---package
-test
---java
---package
-resources

6.
<?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">
    
	<modelVersion>4.0.0</modelVersion>
	
	<groupId>cn.maven.model</groupId>
	        <!--项目的包名(组织名,+公司网址反写,+项目名)-->
	<artifactId>maven01-model</artifactId>
	<!--模块名(项目名-模块名)-->
	<version>0.0.1SNAPSHOT</version>		
<!--项目的版本(0.0.1SNAPSHOT)-->
	<dependencies>
		
		<dependency>
			
			<groupId>junit</groupId>
			
			<artifactId>junit</artifactId>
			
			<version>4.10</version>
		
		</dependency>
	
	</dependencies>
</project>

7.项目的编译
mvn compile
运行测试用例
mvn test
mvn package




8.Maven的常用命令
mvn -v          查看maven的版本
mvn compile     编译项目
mvn test        项目测试
mvn package     打包项目
mvn clean       删除生成的target文件夹(字节码文件和测试报告)
mvn install     安装jar包到本地仓库中

9.自动创建目录骨架
使用archetype插件可以用于创建符合Maven规定的目录骨架。
(1)mvn archetype:generate
(2)mvn archetype:genarate -DgroupId=cn.maven.demo -DartifactId=maven04-demo -Dversion=0.0.1SNAPSHOT -Dpackage=cn.maven.demo

10.maven中的坐标和仓库
maven中构件的坐标都是由 groupId,artifactId,version来表示。
|-本地仓库 
仓库   -|
|-远程仓库( https://repo.maven.apache.org/maven2-> http://search.maven.org
镜像仓库->需修改settings.xml文件中的<mirror>
<mirror>
      
		<id>maven.net.cn</id>
      
		<mirrorOf>central</mirrorOf>
      
		<name>central mirror in china</name>
      
		<url>http://maven.net.cn/content/groups/public</url>
    
</mirror>

修改本地仓库的位置->需修改settings.xml文件中的<localRepository>
<localRepository>D:/maven/repo</localRepository>

11.在Eclipse中安装maven插件以及创建maven项目
高版本的eclipse(4.0以上)中已经安装了maven插件,无需再安装。修改配置如下:
Window->preferences->maven->installations(添加本机安装的maven<D:\maven\apache-maven-3.3.9>)->User Settings(D:\maven\repo\settings.xml)
Window->preferences->java->installed JREs->Default VM arguments:(-Dmaven.multiModuleProjectDirectory=$M2_HOME)

12.maven的生命周期和插件
完整的项目构件过程包括:
清理、编译、测试、打包、集成测试、验证、部署
maven生命周期:
clean     清理项目    (clean)
|-  pre-clean   执行清理前的工作
       -|-  clean       清理上一次构建生成的所有文件
|-  post-clean  执行清理后的文件
default   构建项目    (compile,test,package,install)
site      生成项目站点 ()
|-  pre-site    在生成项目站点之前要完成的工作
|-  site        生成项目的站点文档
|-  post-site   在生成项目站点后要完成的工作
|-  site-deploy 发布生成的站点到富强武器上

maven插件: http://maven.apache.org/plugins/index.html
插件应用实例:
<build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-source-plugin</artifactId>
  			<version>3.0.1</version>
  			<executions>
  				<execution>
  					<phase>package</phase>
  					<goals>
  						<goal>jar-no-fork</goal>
  					</goals>
  				</execution>
  			</executions>
  		</plugin>
  	</plugins>
</build>

13.pom.xml的常用元素
pom.xml是maven的核心管理文件,用于项目描述,组织管理,依赖管理和构建信息的管理。
<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>

	<!--pom的坐标信息-->
	<groupId>反写的公司网址+项目名</groupId>     <!--maven中项目的标识-->

	<artifactId>项目名+模块名</artifactId>
	     <!--maven项目中一个模块的标识-->
	<!--
		snapshot     快照
		alpha        内测
		beta         公测
		Release      稳定
		GA           正式发布
	-->
	<version>0.0.1SNAPSHOT</version>             <!--当前项目的版本
号(第一个0表示大版本号,第二个0表示分支版本号,第三个0表示小版本号)-->  	
	<!--默认jar,还可以是war,zip,pom-->
	<packaging></packaging>                      <!--打包方式-->
	<name></name>                                <!--项目的描述名,产生项目文档的时候使用-->
	<url></url>                                  <!--项目的地址-->
	<description></description>                  <!--项目描述-->
	<developers></developers>                    <!--开发人员列表-->
	<licenses></licenses>                        <!--许可证信息-->
	<organization></organization>                <!--组织信息-->
	<dependencies>
                               <!--依赖列表,可以包含多个依赖项-->
		<dependency>                         <!--依赖项-->
   			<groupId>junit</groupId>

			<artifactId>junit</artifactId>

			<version>4.10</version>

			<type></type>
			<scope></scope>              <!--依赖的范围-->
			<!--设置依赖是否可选-->      <!--默认false(子项目是继承的),如果为true则该子项目必须显式的引入该依赖-->
			<optional></optional>
			<!--排除依赖传递列表-->
			<exclusions>
				<exclusion>
				</exclusion>
			</exclusions>		
		</dependency>

	</dependencies>

	<!--依赖的管理-->
	<dependencyManagement>
		<depandencies><dependency></dependency></depandencies>
	</depandencyManagement>
	<!--对构建的行为进行支持-->
	<build>
		<!--插件的列表-->
		<plugins>
			<plugin>
				<groupId></groupId>

				<artifactId></artifactId>

				<version></version>
			</plugin>
		</plugins>
	</build>
	<parent></parent>                             <!--在子模块中对父模块的继承--> 
	<modules></modules>                           <!--多个模块的集合编译,聚合运行多个maven项目-->
</project>

14.maven中依赖范围
三种classpath:
1.编译
2.测试
3.运行

maven提供了6种scope的属性值:
compile:默认的范围,对编译测试运行都有效
provided:在编译测试有效
runtime:在测试运行时有效
test:只会在测试范围有效
system:在编译和测试有效,与本机系统相关联,可移植性差
import:导入的范围,只使用在dependencyManagement中,表示从其他的pom中导入dependency的配置

15.maven中的依赖传递
修改创建的maven项目中的默认jdk版本
<profile>

      	<id>jdk-1.7</id>


      	<activation>
		<activeByDefault>true</activeByDefault>

        	<jdk>1.7</jdk>

      	</activation>

	<properties>

		<maven.compiler.source>1.7</maven.compiler.source>

		<maven.compiler.target>1.7</maven.compiler.target>

		<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>

	</properties>

</profile>

16.maven中的依赖冲突
1.短路优先            优先解析路径短的版本
2.先声明先优先        如果路径长度相同,则谁先声明,先解析谁
17.聚合和继承
聚合:
<modules>
  			<module>../hongxin-beg</module>
  			<module>../hongxin-nang</module>
  			<module>../hongxin-shanji</module>
  		</modules>

继承:
<properties>
    			<junit.version>3.8.1</junit.version>
  		</properties>
		<dependencyManagement>
			<dependencies>
		    		<dependency>
		      			<groupId>junit</groupId>
		      			<artifactId>junit</artifactId>
		      			<version>${junit.version}</version>
		      			<scope>test</scope>
		    		</dependency>
		 	</dependencies>
		</dependencyManagement>
		
		<parent>
			<groupId>com.hongxin</groupId>
  			<artifactId>hongxin-parent</artifactId>
  			<version>0.0.1-SNAPSHOT</version>	
		</parent>
		
		<dependency>
      			<groupId>junit</groupId>
      			<artifactId>junit</artifactId>
    		</dependency>

18.使用maven创建web项目
New Maven project->选择maven-archetype-webapp->创建web项目->在pom.xml添加servlet的jar包
<dependency>
    	<groupId>javax.servlet</groupId>
    	<artifactId>servlet-api</artifactId>
    	<version>2.5</version>
    	<scope>provided</scope>
	</dependency>

->创建src/main/java,src/test/java,src/test/resources资源文件夹->右键项目选择Build Path
->选择Source,查看文件夹是否指向正确(webdemo/target/classes,webdemo/target/test-classes)
->右键项目选择Properties->选择Project Facets->打勾Dynamic Web Module
->右键项目选择Properties->选择Deployment Assembly->remove掉test文件夹

<!--使用jetty或者tomcat启动-->
 
<build>
    <finalName>webdemo</finalName>
    <plugins>
    	<plugin>
    		<!-- 
		<groupId>org.mortbay.jetty</groupId>
    		<artifactId>jetty-maven-plugin</artifactId>
    		<version>8.1.16.v20140903</version> 
		-->
    		<groupId>org.apache.tomcat.maven</groupId>
          	<artifactId>tomcat7-maven-plugin</artifactId>
          	<version>2.2</version>
    		<executions>
    			<execution>
    				<phase>package</phase>
    				<goals>
    					<goal>run</goal>
    				</goals>
    			</execution>
    		</executions>
    	</plugin>
    </plugins>
  </build>


猜你喜欢

转载自beginner-dai.iteye.com/blog/2322522