Maven 的主要目的是为开发者提供
● 一个可复用、可维护、更易理解的工程综合模型
● 与这个模型交互的插件或者工具
Maven 能够帮助开发者完成以下工作:
● 构建
● 文档生成
● 报告
● 依赖
● SCMs
● 发布
● 分发
● 邮件列表
Maven 工程结构和内容被定义在一个 xml 文件中 - pom.xml,是 Project Object Model (POM) 的简称,此文件是整个 Maven 系统的基础组件
http://wiki.jikexueyuan.com/project/maven/environment-setup.html
POM 代表工程对象模型。它是使用 Maven 工作时的基本组建,是一个 xml 文件。它被放在工程根目录下,文件命名为 pom.xml。POM 包含了关于工程和各种配置细节的信息,Maven 使用这些信息构建工程。
在创建 POM 之前,我们首先确定工程组(groupId),及其名称(artifactId)和版本,在仓库中这些属性是工程的唯一标识。这些可以在仓库repository中查看到相关信息
下面以java EE的常见 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.0modelVersion>
<groupId>crazyfzwgroupId>
<artifactId>springmvc-demoartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>springmvc-demoname>
<description>springmvc-demodescription>
<properties>
<junit.version>4.10junit.version>
<spring.version>4.3.5.RELEASEspring.version>
<slf4j.version>1.6.4slf4j.version>
<log4j.version>1.2.17log4j.version>
<jackson.version>2.8.5jackson.version>
<mapper.version>2.3.4mapper.version>
<commons-lang3.version>3.3.2commons-lang3.version>
<commons-io.version>1.3.2commons-io.version>
<redis.version>2.9.0redis.version>
<spring-data-redis.version>1.7.5.RELEASEspring-data-redis.version>
<jxls-core.version>1.0.6jxls-core.version>
<poi.version>3.15poi.version>
<mchange-commons-java.version>0.2.12mchange-commons-java.version>
<hibernate.version>4.3.9.Finalhibernate.version>
<quartz.version>2.2.3quartz.version>
<fastjson.version>1.2.22fastjson.version>
<jstl.version>1.2jstl.version>
<servlet-api.version>2.5servlet-api.version>
<jsp-api.version>2.0jsp-api.version>
<p6spy.version>2.1.4p6spy.version>
properties>
<dependencies>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>${fastjson.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>${jackson.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>${jackson.version}version>
dependency>
<dependency>
<groupId>net.sf.json-libgroupId>
<artifactId>json-libartifactId>
<version>2.4version>
<classifier>jdk15classifier>
dependency>
<dependency>
<groupId>com.github.abel533groupId>
<artifactId>mapperartifactId>
<version>${mapper.version}version>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocityartifactId>
<version>1.7version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>mchange-commons-javaartifactId>
<version>${mchange-commons-java.version}version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>${commons-lang3.version}version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-ioartifactId>
<version>${commons-io.version}version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>commons-beanutilsgroupId>
<artifactId>commons-beanutilsartifactId>
<version>1.8.3version>
dependency>
<dependency>
<groupId>commons-collectionsgroupId>
<artifactId>commons-collectionsartifactId>
<version>3.2.1version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>${poi.version}version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>${poi.version}version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-scratchpadartifactId>
<version>${poi.version}version>
dependency>
<dependency>
<groupId>net.sf.jxlsgroupId>
<artifactId>jxls-coreartifactId>
<version>${jxls-core.version}version>
dependency>
<dependency>
<groupId>net.sourceforge.jexcelapigroupId>
<artifactId>jxlartifactId>
<version>2.6.12version>
dependency>
<dependency>
<groupId>org.quartz-schedulergroupId>
<artifactId>quartzartifactId>
<version>${quartz.version}version>
dependency>
<dependency>
<groupId>opensymphonygroupId>
<artifactId>quartz-allartifactId>
<version>1.6.1version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>${hibernate.version}version>
dependency>
<dependency>
<groupId>p6spygroupId>
<artifactId>p6spyartifactId>
<version>${p6spy.version}version>
dependency>
<dependency>
<groupId>net.sf.ehcachegroupId>
<artifactId>ehcache-coreartifactId>
<version>2.5.1version>
dependency>
<dependency>
<groupId>com.oraclegroupId>
<artifactId>ojdbc14artifactId>
<version>14.0.2.0.4.0version>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>${redis.version}version>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>${spring-data-redis.version}version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>io.nettygroupId>
<artifactId>nettyartifactId>
<version>3.7.0.Finalversion>
dependency>
<dependency>
<groupId>org.javassistgroupId>
<artifactId>javassistartifactId>
<version>3.18.2-GAversion>
dependency>
<dependency>
<groupId>jlinegroupId>
<artifactId>jlineartifactId>
<version>0.9.94version>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.4version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.8.4version>
dependency>
<dependency>
<groupId>joda-timegroupId>
<artifactId>joda-timeartifactId>
<version>2.3version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>${jstl.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>${servlet-api.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jsp-apiartifactId>
<version>${jsp-api.version}version>
<scope>providedscope>
dependency>
dependencies>
<build>
<sourceDirectory>srcsourceDirectory>
<resources>
<resource>
<directory>srcdirectory>
<excludes>
<exclude>**/*.javaexclude>
excludes>
resource>
<resource>
<directory>resourcedirectory>
<excludes>
<exclude>**/*.javaexclude>
excludes>
resource>
resources>
<plugins>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>1.7source>
<target>1.7target>
configuration>
plugin>
<plugin>
<artifactId>maven-war-pluginartifactId>
<version>2.4version>
<configuration>
<warSourceDirectory>WebRootwarSourceDirectory>
<failOnMissingWebXml>falsefailOnMissingWebXml>
configuration>
plugin>
plugins>
build>
project>
1) 本地仓库
Maven 本地仓库是机器上的一个文件夹。它在你第一次运行任何 maven 命令的时候创建。
Maven 本地仓库保存你的工程的所有依赖(library jar、plugin jar 等)。当你运行一次 Maven 构建,Maven 会自动下载所有依赖的 jar 文件到本地仓库中。它避免了每次构建时都引用存放在远程机器上的依赖文件。
可以在maven安装地址的conf/settings.xml 指定本地仓库的路径
"http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
E:/myLocalRepository
</settings>
2)中央仓库
http://mvnrepository.com/
https://search.maven.org/
3)远程仓库
可以在私有服务器上搭建自己的maven仓库,外网的内网的都可以。eg:
<repositories>
<repository>
<id>companyname.lib1id>
<url>http://download.companyname.org/maven2/lib1url>
repository>
<repository>
<id>companyname.lib2id>
<url>http://172.16.176.234/maven2/lib2url>
repository>
repositories>
Maven 依赖搜索顺序
步骤1:在本地仓库找
步骤2:若本地仓库没找到则去中央仓库找,找到则下载到本地仓库,留着日后备用
步骤3:若中央仓库没找到,则会检查时候有设置远程仓库,若有,则会在远程仓库中查找并下载到本地仓库备用。若没设置远程仓库或者在远程仓库中也无法找到,则会抛出错误(无法找到依赖的文件)
针对这种情况,可以通过构建外部依赖的方法解决。
把jar包文件并放到工程lib下,然后在pom.xml中添加依赖,eg:
<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.0modelVersion>
<groupId>com.companyname.bankgroupId>
<artifactId>consumerBankingartifactId>
<packaging>jarpackaging>
<version>1.0-SNAPSHOTversion>
<name>consumerBankingname>
<url>http://maven.apache.orgurl>
<dependencies>
<dependency>
<groupId>ldapjdkgroupId>
<artifactId>ldapjdkartifactId>
<scope>systemscope>
<version>1.0version>
<systemPath>${basedir}\src\lib\ldapjdk.jarsystemPath>
dependency>
dependencies>
project>
当一个库 A 依赖于其他库 B. 另一工程 C 想要使用库 A, 那么该工程同样也需要使用到库 B。
使用maven,我们只需要在每个工程的 pom 文件里去定义直接的依赖关系。Maven 则会自动的来接管后续的工作。比如我们在pom.xml中定义了依赖jar包A,那么A所依赖的其他jar包,会被自动下载,不需要我们去定义。是不是很省事?
本文只是初步介绍了下maven 的使用,主要是在maven管理jar包,pom.xml文件的配置方面。还没涉及到maven在项目管理以及自动化构建方面。可参考下面链接拓展学习。
Apache Maven 入门篇
Maven - 构建自动化
转载请注明出处: http://blog.csdn.net/FZW_Faith/article/details/54728943