maven3笔记

前言,之我的困惑


Ref: maven安装官方教程地址

  1. Unzip the distribution archive, i.e. apache-maven-3.0.5-bin.zip to the directory you wish to install Maven 3.0.5. These instructions assume you chose C:\Program Files\Apache Software Foundation. The subdirectory apache-maven-3.0.5 will be created from the archive.
  2. Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, then adding theM2_HOME variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-3.0.5. Be sure to omit any quotation marks around the path even if it contains spaces. Note: For Maven   2.0.9, also be sure that the M2_HOME doesn't have a '\' as last character.
  3. In the same dialog, add the M2 environment variable in the user variables with the value %M2_HOME%\bin.
  4. Optional: In the same dialog, add the MAVEN_OPTS environment variable in the user variables to specify JVM properties, e.g. the value -Xms256m -Xmx512m. This environment variable can be used to supply extra options to Maven.
  5. In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M2% to add Maven available in the command line.
  6. In the same dialog, make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK, e.g. C:\Program Files\Java\jdk1.5.0_02 and that %JAVA_HOME%\bin is in your Path environment variable.
  7. Open a new command prompt (Winkey + R then type cmd) and run mvn --version to verify that it is correctly installed.
Eclipse maven插件:
Eclipse -> Install -> 添加:http://download.eclipse.org/technology/m2e/releases
有时候可能会报错,多试几次就可以了,不要着急,休息,休息一下。


Maven in 5 Minutes

Maven in 30 Minutes - Maven Getting Started Guide


仓库:

打个比方 —— 首先来个书上的比喻,仓库就是“书库”,你自己家里有个书房,当你需要一本书的时候你先去自己书房里找,找不到了再从网上或者实体商店买一本放到自己的书房,然后你自己可以到书房里去取,当然你也有个默认的习惯,比如默认没有书的时候你首先选择amazon去买书,这是一种很好的管理图书模式,你每天都是过着如此的生活以至于你不回觉得这是一件多么了不起的事情。其实,它是一件了不起的事情,因为我们很多时候就是在类似的事情上没有按照这种方式管理项目。我们每天写很多代码,是不是就要建一个工程,每次都是把需要的jar包放进去,这个工程此时就是一个小的“书房”了,这种开销也不小。这也是为什么maven要帮助我们如何管理项目的一个方面的原因(原因不知这一个)。

maven仓库也是这个概念,当你构建项目时,你有依赖项(我不知道可不可以把它等同于jar包),maven会根据你对pom.xml的操作,知道你需要哪些jar包,然后构建开始后它就帮你从本地仓库(你的书房)开始寻找相应的jar包,找不到了就从远程仓库(amazon)下载jar包到你的本地仓库,然后用于构建项目。

这样的好处是……?每个程序员都遇到这样的情况,他要构建一个项目,每次它都在一个工程里面创建一个这样的文件夹"lib",然后每次都将相应的jar包添加进去,这就相当于你自己没有划出一块专用的区域来统一管理你的jar包,而maven就帮你管理了它了。

理解仓库。我们把问题稍稍深入下去,那么我们需要多少个本地仓库?多少个远程仓库?如何管理它们呢?我们可以按照书房的方式来进行理解,从你自己一个程序员角度来讲,那么你一般仅需要一个本地仓库,设置几个远程仓库,正如你家里有一个书房,而可以供你选择的商店可以有多个,比如amazon,京东,当当。从多个人的团队来讲,你就不是一个人在战斗了,这时候如果每个人都是如果本地仓库没有就去远程仓库去下载,那么将造成很大的浪费,你受的了,带宽也嘚抱怨吧,带宽不抱怨,中央仓库也精神崩溃吧。所以就要建一个私服,我们可以这样描述它:它架设在局域网服务器上,对于局域网内的用户而言他就是远程服务器,而它自己也相当于一个本地仓库(因为没有依赖项时它会去远程仓库去取),这样它就减轻了远程仓库的负担,让你快速得到需要的依赖项,大家都好。

更多。同样因为你要使用maven,那么就会有相关的需求。比如,你知道某一个依赖项的名称,但是你不知道它的maven坐标,你也是不能使用它的,maven坐标可以这样理解,问“你是如何从地图上找到”成都“的地方的”,答“唯一性和完全性”,”完全性“完整的描述了你找到它所需要的信息,”唯一性“确定了你可以通过完整性的描述找到有且仅有一个的成都。maven坐标就是这样的概念。那么就伴随着你可以使用google地图搜索,或者百度地图搜索找到程度了,不同的工具根据maven坐标找到相应的依赖项,常用的、功能强大的公共maven仓库搜索服务有以下主要内容:

生命周期:

key:清理、初始化、编译、测试、打包、集成测试、验证、部署和站点生成。

三套生命周期:clean、default 和 site。

(说实话这段的理解我不是有十分的把我认为我能说服你,而且我也没有十分的把握能证明我是对的,只是我尽我所能能用这些来说服我自己罢了,你要辩证的去看哈)总的基本上就是上面的过程,它是maven抽象出来的所有项目的构建过程。也就是说它是一个并集,所有项目的构建过程都是从其中抽出几个来组成一个子集作为自己特有的项目构建过程。那么何为生命周期,这个我找了下没找到非常概念化的定义,但是可以这样理解:生命周期就是项目当前所处在构建过程中的一个抽象的阶段。maven将构建过程划分为三套生命周期:clean、default 和 site。它们之间是相互独立的,也就是说比如你调用 default 生命周期的某个阶段不会影响对其他生命周期产生任何影响。

首先来说clean。clean包含三个阶段:

  1. pre-clean:清理前要完成的操作
  2. clean:清理上次构建生成的文件
  3. post-clean:执行一些清理后需要完成的工作

其次是default。

// TODO

最后是site

// TODO


Maven中的目录结构:

ref:Maven2目录和pom.xml文件基本节点介绍


插件绑定:

key:maven生命周期与插件相互绑定,用以完成实际的构建任务。

每个生命周期的每一个阶段都需要绑定插件来做”实际的工作“,完成构建任务。构建有默认的插件绑定,需要时可以自定义绑定需要的插件。


常见问题处理:

001. java版本号不对     @see: 《Maven使用入门》p.33

【常见错误输出】

 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 19, column 13

解决:step1:在 pom.xml 中添加 java 版本指定:

 <project> 
 ...
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<configuration>
  				<source>1.6</source>
  				<target>1.6</target>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
...
</project>
step2:工程名右键 -> Properties -> Java Build Path -> Libraries -> Edit... -> Execution environment -> JavaSE-1.6(jre6) -> Finish

002. 添加本地依赖项(jar包)@see:1)Added dependency on plugin.jar  2) Added dependency on plugin.jar

【情景】

比如你有时候要用一些本地的依赖项(如本地的java库自带的jar包,而仓库里面没有或者你找不到的时候,可以这样用,但是以我目前对 maven 的了解,不鼓励这样做,还是要不得已的时候用吧),这里我们因为要使用 applet 调用 javascript 代码,所以要用到 java 官方的一个叫plugin 的 jar 包,而我又没在仓库中找到,而且,注意而且,即使在其他机器上也可以通过这种方式找到对应的jar包。所以我选择这样去添加依赖项。

</project>
...
  </dependencies>
  ...
	<dependency>
		<groupId>com.sun.jdk.applet</groupId>
		<artifactId>plugin</artifactId>
		<version>1.0</version>
		<scope>system</scope>
		<systemPath>${java.home}/lib/plugin.jar</systemPath>
	</dependency>
	...
  </dependencies>
  ...
</project>

003.创建工程错误

CoreException: Could not get the value for parameter compilerId for plugin execution default-compile: PluginResolutionException: Plugin org.apache.maven.plugins:maven-compiler-plugin:2.3.2 or one of its dependencies could not be resolved: Failed to collect dependencies for org.apache.maven.plugins:maven-compiler-plugin:jar:2.3.2 (): ArtifactDescriptorException: Failed to read artifact descriptor for org.apache.maven:maven-plugin-api:jar:2.0.6: UnresolvableModelException: Failure to transfer org.apache:apache:pom:3 from http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache:apache:pom:3 from/to central (http://repo.maven.apache.org/maven2): connection timed out to http://repo.maven.apache.org/maven2/org/apache/apache/3/apache-3.pom

...

【情景】

Maven刚安装好,还没创建过工程,m2e插件刚安装好,这时候需要第一次使用Maven创建工程,很多的jar包依赖没有,所以eclipse创建了工程会在pom.xml中报上面的错误

【解决】

ref:eclipse加载maven工程提示pom.xml无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3解决方案

1、在pom.xml文件中加入maven-resources-plugin配置

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.3</version>
</dependency>

2、在命令行下运行mvn install ,如果执行正确应该会在{user.home}\.m2\repository\org\apache\maven\plugins\maven-resources-plugin\下看到maven-resources-plugin-2.4.3.jar文件
3、刷新工程(右键工程选择刷新项)
4、刷新maven配置,右键工程节点,选择Maven4MyEclipse-Update Project Configuration

004.获得sources和javadoc

ref:Get source JARs from Maven repository

在依赖项中加入classifier项,会自动下载sources和javadoc:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate</artifactId>
  <version>3.2.7.ga</version>
  <classifier>sources</classifier>
</dependency>


MAVEN2安装环境变量:

http://www.mkyong.com/maven/how-to-install-maven-in-windows/


m2e:安装总是不行,可以考虑使用旧版本的m2e
















  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值