如今我们构建一个项目需要用到很多第三方的类库,如写一个使用spring的Web项目就需要引入大量的jar包。一个项目Jar包的数量之多往往让我们瞠目结舌,并且Jar包之间的关系错综复杂,一个Jar包往往又会引用其他Jar包,缺少任何一个Jar包都会导致项目编译失败。
以往开发项目时,程序员往往需要花较多的精力在引用Jar包搭建项目环境上,而这一项工作尤为艰难,少一个Jar包、多一个Jar包往往会报一些让人摸不着头脑的异常。
而Maven就是一款帮助程序员构建项目的工具,我们只需要告诉Maven需要哪些Jar 包,它会帮助我们下载所有的Jar,极大提升开发效率。
http://blog.csdn.net/qjyong/article/details/9098213
若要使用Maven,那么项目的目录结构必须符合Maven的规范
-v:查询Maven版本
本命令用于检查maven是否安装成功。
Maven安装完成之后,在命令行输入mvn -v,若出现maven信息,则说明安装成功。
compile:编译
将Java源文件编译成class文件
test:测试项目
执行test目录下的测试用例
package:打包
将项目打成jar包
clean:删除target文件夹
install:安装
将当前项目放到Maven的本地仓库中。供其他项目使用
Maven仓库用来存放Maven管理的所有Jar包。分为:本地仓库 和 中央仓库。
当项目编译时,Maven首先从本地仓库中寻找项目所需的Jar包,若本地仓库没有,再到Maven的中央仓库下载所需Jar包。
在Maven中,坐标是Jar包的唯一标识,Maven通过坐标在仓库中找到项目所需的Jar包。
如下代码中,groupId和artifactId构成了一个Jar包的坐标。
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.1.1version>
dependency>
<exclusions>
<exclusion>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
exclusion>
exclusions>
若项目中多个Jar同时引用了相同的Jar时,会产生依赖冲突,但Maven采用了两种避免冲突的策略,因此在Maven中是不存在依赖冲突的。
本项目——>A.jar——>B.jar——>X.jar
本项目——>C.jar——>X.jar
若本项目引用了A.jar,A.jar又引用了B.jar,B.jar又引用了X.jar,并且C.jar也引用了X.jar。
在此时,Maven只会引用引用路径最短的Jar。
什么是聚合?
将多个项目同时运行就称为聚合。
如何实现聚合?
只需在pom中作如下配置即可实现聚合:
<modules>
<module>../模块1module>
<module>../模块2module>
<module>../模块3module>
modules>
什么是继承?
在聚合多个项目时,如果这些被聚合的项目中需要引入相同的Jar,那么可以将这些Jar写入父pom中,各个子项目继承该pom即可。
如何实现继承?
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.shirogroupId>
<artifactId>shiro-springartifactId>
<version>1.2.2version>
dependency>
dependencies>
dependencyManagement>
<parent>
<groupId>父pom所在项目的groupIdgroupId>
<artifactId>父pom所在项目的artifactIdartifactId>
<version>父pom所在项目的版本号version>
parent>
New Maven项目:选择WebApp:
若使用JSP,需添加Servlet依赖:
注:Servlet依赖只在编译和测试时使用!
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.0.1version>
<scope>providedscope>
dependency>
在Bulid Path中设置resource输出目录:
勾选:Dynamic Web Module
删掉测试目录
在pom中加入jetty的插件,并设置JDK版本:
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
<plugin>
<groupId>org.eclipse.jettygroupId>
<artifactId>jetty-maven-pluginartifactId>
<version>9.3.10.v20160621version>
<executions>
<execution>
<phase>packagephase>
execution>
executions>
plugin>
plugins>
运行项目:
输入:jetty:run
访问127.0.0.1:8080
pom.xml是Maven的核心,你的项目需要什么Jar包就在pom.xml里面配置。当编译项目时Maven读取该文件,并从仓库中下载相应的Jar包。
<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.0http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId/>
<groupId/>
<version/>
<relativePath/>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>cn.erhuowanggroupId>
<artifactId>erhuowang-maven2artifactId>
<packaging>warpackaging>
<version>1.0-SNAPSHOTversion>
<name>erhuo-mavenname>
<url>http://erhuowang.cnurl>
<description>A maven project to study maven.description>
<prerequisites>
<maven/>
prerequisites>
<issueManagement>
<system>erhuowangsystem>
<url>http://erhuowang.cnurl>
issueManagement>
<ciManagement>
<system/>
<url/>
<notifiers>
<notifier>
<type/>
<sendOnError/>
<sendOnFailure/>
<sendOnSuccess/>
<sendOnWarning/>
<address/>
<configuration/>
notifier>
notifiers>
ciManagement>
<inceptionYear/>
<mailingLists>
<mailingList>
<name>Demoname>
<post>[email protected]post>
<subscribe>[email protected]subscribe>
<unsubscribe>[email protected]unsubscribe>
<archive>[email protected]archive>
mailingList>
mailingLists>
<developers>
<developer>
<id>HELLO WORLDid>
<name>chaimmname>
<email>[email protected]email>
<url/>
<roles>
<role>Project Managerrole>
<role>Architectrole>
roles>
<organization>demoorganization>
<organizationUrl>http://erhuowang.cnorganizationUrl>
<properties>
<dept>Nodept>
properties>
<timezone>-5timezone>
developer>
developers>
<contributors>
<contributor>
<name/><email/><url/><organization/><organizationUrl/><roles/><timezone/><properties/>
contributor>
contributors>
<licenses>
<license>
<name>Apache 2name>
<url>http://www.baidu.com/erhuwoang/LICENSE-2.0.txturl>
<distribution>repodistribution>
<comments>A business-friendly OSS licensecomments>
license>
licenses>
<scm>
<connection>
scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk)
connection>
<developerConnection>
scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk
developerConnection>
<tag/>
<url>http://svn.baidu.com/banseonurl>
scm>
<organization>
<name>demoname>
<url>http://www.erhuowang.cnurl>
organization>
<build>
<sourceDirectory/>
<scriptSourceDirectory/>
<testSourceDirectory/>
<outputDirectory/>
<testOutputDirectory/>
<extensions>
<extension>
<groupId/>
<artifactId/>
<version/>
extension>
extensions>
<defaultGoal/>
<resources>
<resource>
<targetPath/>
<filtering/>
<directory/>
<includes/>
<excludes/>
resource>
resources>
<testResources>
<testResource>
<targetPath/><filtering/><directory/><includes/><excludes/>
testResource>
testResources>
<directory/>
<finalName/>
<filters/>
<pluginManagement>
<plugins>
<plugin>
<groupId/>
<artifactId/>
<version/>
<extensions/>
<executions>
<execution>
<id/>
<phase/>
<goals/>
<inherited/>
<configuration/>
execution>
executions>
<dependencies>
<dependency>
......
dependency>
dependencies>
<inherited/>
<configuration/>
plugin>
plugins>
pluginManagement>
<plugins>
<plugin>
<groupId/><artifactId/><version/><extensions/>
<executions>
<execution>
<id/><phase/><goals/><inherited/><configuration/>
execution>
executions>
<dependencies>
<dependency>
......
dependency>
dependencies>
<goals/><inherited/><configuration/>
plugin>
plugins>
build>
<profiles>
<profile>
<id/>
<activation>
<activeByDefault/>
<jdk/>
<os>
<name>Windows XPname>
<family>Windowsfamily>
<arch>x86arch>
<version>5.1.2600version>
os>
<property>
<name>mavenVersionname>
<value>2.0.3value>
property>
<file>
<exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/exists>
<missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/missing>
file>
activation>
<build>
<defaultGoal/>
<resources>
<resource>
<targetPath/><filtering/><directory/><includes/><excludes/>
resource>
resources>
<testResources>
<testResource>
<targetPath/><filtering/><directory/><includes/><excludes/>
testResource>
testResources>
<directory/><finalName/><filters/>
<pluginManagement>
<plugins>
<plugin>
<groupId/><artifactId/><version/><extensions/>
<executions>
<execution>
<id/><phase/><goals/><inherited/><configuration/>
execution>
executions>
<dependencies>
<dependency>
......
dependency>
dependencies>
<goals/><inherited/><configuration/>
plugin>
plugins>
pluginManagement>
<plugins>
<plugin>
<groupId/><artifactId/><version/><extensions/>
<executions>
<execution>
<id/><phase/><goals/><inherited/><configuration/>
execution>
executions>
<dependencies>
<dependency>
......
dependency>
dependencies>
<goals/><inherited/><configuration/>
plugin>
plugins>
build>
<modules/>
<repositories>
<repository>
<releases>
<enabled/><updatePolicy/><checksumPolicy/>
releases>
<snapshots>
<enabled/><updatePolicy/><checksumPolicy/>
snapshots>
<id/><name/><url/><layout/>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<enabled/><updatePolicy/><checksumPolicy/>
releases>
<snapshots>
<enabled/><updatePolicy/><checksumPolicy/>
snapshots>
<id/><name/><url/><layout/>
pluginRepository>
pluginRepositories>
<dependencies>
<dependency>
......
dependency>
dependencies>
<reports/>
<reporting>
......
reporting>
<dependencyManagement>
<dependencies>
<dependency>
......
dependency>
dependencies>
dependencyManagement>
<distributionManagement>
......
distributionManagement>
<properties/>
profile>
profiles>
<modules/>
<repositories>
<repository>
<releases>
<enabled/>
<updatePolicy/>
<checksumPolicy/>
releases>
<snapshots>
<enabled/><updatePolicy/><checksumPolicy/>
snapshots>
<id>banseon-repository-proxyid>
<name>banseon-repository-proxyname>
<url>http://192.168.1.169:9999/repository/url>
<layout>defaultlayout>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
......
pluginRepository>
pluginRepositories>
<dependencies>
<dependency>
<groupId>org.apache.mavengroupId>
<artifactId>maven-artifactartifactId>
<version>3.8.1version>
<type>jartype>
<classifier>classifier>
<scope>testscope>
<systemPath>systemPath>
<exclusions>
<exclusion>
<artifactId>spring-coreartifactId>
<groupId>org.springframeworkgroupId>
exclusion>
exclusions>
<optional>trueoptional>
dependency>
dependencies>
<reports>reports>
<reporting>
<excludeDefaults/>
<outputDirectory/>
<plugins>
<plugin>
<groupId/>
<artifactId/>
<version/>
<inherited/>
<configuration/>
<reportSets>
<reportSet>
<id/>
<configuration/>
<inherited/>
<reports/>
reportSet>
reportSets>
plugin>
plugins>
reporting>
<dependencyManagement>
<dependencies>
<dependency>
......
dependency>
dependencies>
dependencyManagement>
<distributionManagement>
<repository>
<uniqueVersion/>
<id>banseon-maven2id>
<name>banseon maven2name>
<url>file://${basedir}/target/deployurl>
<layout/>
repository>
<snapshotRepository>
<uniqueVersion/>
<id>banseon-maven2id>
<name>Banseon-maven2 Snapshot Repositoryname>
<url>scp://svn.baidu.com/banseon:/usr/local/maven-snapshoturl>
<layout/>
snapshotRepository>
<site>
<id>banseon-siteid>
<name>business api websitename>
<url>
scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web
url>
site>
<downloadUrl/>
<relocation>
<groupId/>
<artifactId/>
<version/>
<message/>
relocation>
<status/>
distributionManagement>
<properties/>
project>