Maven仓库:
本地仓库:默认配置完maven后,会在用户家目录下生成一个本地仓库,window系统的在 C:\Users\Administrator.m2\repository\放置本地仓库的构件。如果要自定义本地仓库:建议修改自己的settings.xml 文件,而不要把系统的maven settings.xml文件修改了,例如在 Linux 上,一个服务器我们配置了多个用户,如果我们修改了 M2_HOME (maven的安装目录)下的conf文件下的 settings.xml 文件,整台机器上所有用户都会受到该配置的影响,所以我们建立修改我们家目录下的settings.xml文件:
- 复制 M2_HOME\conf\settings.xml 到 ~.m2\下,
- 在~.m2\settings.xml中找到 localRepository 标签
<settings> <localRepository> d:\your local repo dir\</localRepository></settings>
将自己的项目安装到本地仓库命令:mvn clean install
注意这个 install 之前默认执行了那些操作,clean - compile - test - package - install
远程仓库:Maven的中央仓库,第三方的中央仓库和我们自己搭建的私服;
- 中央仓库在 maven-model-builder-3.5.0.jar 解压后的 \org\apache\maven\model下的 pom-4.0.0.xml中 配置了maven默认的中央仓库和插件仓库位置:
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
如何配置更多的中央仓库:在pom.xml中类似上面添加自己的第三方中央仓库,需要注意的是maven 自己默认的中央仓库的id是 central 如过我们自己配的第三方仓库的id 也为 central 就会覆盖了maven的中央仓库,这也在强调仓库 id的唯一性与重要性
<repositories>
<repository>
<id>jboss</id>
<name>Jboss Repository</name>
<url>https://repository.jboss.com/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
如果这个仓库需要认证,也就是提供用户名和密码,我们就需要在 settings.xml 文件而不是pom.xml文件中填写 server认证信息:举例
<servers>
<server>
<!-- id 必须和POM中需要认证的repository 元素id一致-->
<id>jboss</id>
<username>jbossRepo_user</username>
<password>jbossRepo_pwd</password>
</server>
</servers>
配置一个仓库的镜像: 何为镜像,自己和镜子中的自己,如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像。换句话说,任何一个可以从仓库Y中获取的构件,都能从它的镜像中获取。例如maven中央仓库 https://repo.maven.apache.org/maven2
在国内阿里云提供的镜像为:http://maven.aliyun.com/nexus/content/groups/public/
为什么要镜像,就像国内的免税店,一个字 快
如何配置镜像 settings.xml中
<mirrors>
<mirror>
<id>aliyunmaven</id>
<name>aliyun maven mirror for central maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
- 私服:架构在企业局域网内的仓库服务,供局部网内的maven 用户使用,好处一个字 快,
将自己编写的项目发布到远程仓库,前面我们说了 mvn clean install
会将编译好的jar文件建立到本地仓库,对于发布远程仓库:首先在 项目的 pom.xml文件中配置<distributionManagement>
<distributionManagement>
<repository>
<id>project-releases</id>
<name>This is project release repository</name>
<url>http://192.168.1.100/repositories/project-releases</url>
</repository>
<snapshotRepository>
<id>project-snapshots</id>
<name>This is project snapshot repository</name>
<url>http://192.168.1.100/repositories/project-snapshots</url>
</snapshotRepository>
</distributionManagement>
mvn clean deploy
Maven 声明周期与插件 Plugin:
引入 Juven Xu 的一段话,说的很生动很彻底:
- Maven 的生命周期就是对所有的构建过程进行抽象和统一。这个生命周期包含了项目的 清理、初始化、编译、测试、打包、集成测试、验证、部署、站点生成等几乎所有构建步骤。也就是说,几乎所有项目的构建,都能映射到这样一个生命周期上。
- Maven 的生命周期是抽象的,它只定义整个项目构建的过程,而每一步的实际工作是由插件 plugin来完成的。这和java的模板模式很像;
- 模板方法模式在父类中定义算法的整体结构,子类可以通过实现或者重写父类的方法来控制 实际的行为,这样既保证了算法有足够的可扩展性,又能严格控制算法的整体结构。
package com.qian.dsgmodel;
public abstract class TemplateModel {
public void build(){
//清理
clean();
//初始化
initialize();
//编译
compile();
//测试
test();
//打包
packagee();
//集成测试
integrationTest();
//部署
deploy();
}
protected abstract void clean();
protected abstract void initialize();
protected abstract void compile();
protected abstract void test();
protected abstract void packagee();
protected abstract void integrationTest();
protected abstract void deploy();
}
生命周期中的每个构建步骤Maven都为其绑定了默认的插件,例如 compiler过程是由 maven-compiler-plugin插件来完成的,自然,我们也可以为一个构建步骤绑定多个实现插件或者开发自己的插件。
1. Maven 拥有三套相互独立的生命中周期,分别为 clean, default,site.
三个生命周期是项目独立的,对于每一个生命周期包含的一些阶段,这些阶段是有顺序的,并且后面的阶段依赖于前面的阶段,例如,当用户调用 post-clean 时,pre-clean 和 clean 会依顺序执行然后在执行 post-clean。
default 生命周期定义了真正构建时所需要执行的所有步骤,它是所有生命周期中最核心的部分,主要包括:官方文档 Maven default
- validate validate the project is correct and all necessary
information is available. (验证项目是否正确,所有的必须的信息是否可用) - initialize initialize build state, e.g. set properties or create
directories. (初始化构建声明,也就是,设置属性或者创建目录) - generate-sources generate any source code for inclusion in
compilation. (生成任何一个在编译中包含的源码) - process-sources process the source code, for example to filter any values.(处理源码,例如过滤一些值)
- generate-resources generate resources for inclusion in the package.(生成包中包含的主资源文件)
- process-resources copy and process the resources into the destination directory, ready for packaging. (复制和处理资源文件到输出目录中,为打包做准备)
- compile compile the source code of the project.(编译项目源码)
- process-classes post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.(处理从编译阶段成长的文件,例如在java classes文件上做一些子界面增强操作)
- generate-test-sources generate any test source code for inclusion in compilation. (这个是针对测试类的和上面主类似)
- process-test-sources process the test source code, for example to filter any values.
- generate-test-resources create resources for testing.
- process-test-resources copy and process the resources into the test destination directory.
- test-compile compile the test source code into the test destination directory
- process-test-classes post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
- test run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.(使用合适的单元测试框架来运行测试类,对于打包和发布这些测试不是必须的代码)
- prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)(在进行实际打包前,执行任何一个打包所需的操作。这经常导致一个未打包,打包的处理版本)
- package take the compiled code and package it in its distributable format, such as a JAR.(进行编译代码并将他们打包成发布所需的格式,例如一个jar文件)
- pre-integration-test perform actions required before integration tests are executed. This may involve things such as setting up the required environment.(执行集成测试前所有的一些行为。这可能会调用一些东西,例如建立一个必要的环境)
- integration-test process and deploy the package if necessary into an environment where integration tests can be run.(如果进行集成测试或者集成运行的环境中需要,就执行和发布这些包)
- post-integration-test perform actions required after integration tests have been executed. This may including cleaning up the environment.(在执行集成测试完成后需要执行的行为,这可能包括清理运行环境等)
- verify run any checks to verify the package is valid and meets quality criteria.(运行一些检查以验证包是否有效并符合质量标准。)
- install install the package into the local repository, for use as a dependency in other projects locally.(把包安装到本地仓库,为本地的其他项目作为依赖使用)
- deploy done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.(做一个集成或者可发布的环境,复制最终的包到远程仓库(私服)供其他的开发者或者其他的项目使用)
(表达能力有限没有真正的翻译出源Englishi的含义,望包容一下)
2. 命令行与生命周期
我们用命令行执行 Maven命令时,要注意,三个生命周期(clean default site)是相互独立的,并且各个生命周期的阶段是有前后依赖关系滴。例如
执行mvn clean install
: 改命令首先调用clean生命周期的 clean 阶段(在这个阶段会把clean 前面的 pre-clean 阶段自行执行了)然后执行default 生命周期中的 install 阶段,自然也会把在 default 生命周期 install 阶段之前的所有阶段都执行了。
3. Plugin 的作用是什么
Maven的核心仅仅定义了抽象的生命周期,具体的任务是交由插件完成的,插件以独立的构建形式存在,因此,Maven的核心发布包只不过4M左右,Maven 会在需要的时候自动去中央仓库下载使用插件,我们在 上面pom-4.0.0.mxl 中介绍默认的中央仓库时,也看到了它也配置了默认的中央仓库。并且每个插件具有多个功能,也就是说每个插件有多个目标,来干不同的事情,例如:我们可以在 http://maven.apache.org/plugins/index.html 查看 apache 提供的maven的插件,以及每个插件各自目标和怎么用都有说明;
4. Maven给我们内置绑定插件
我们需要注意以下几点:
- maven的生命周期是抽象的,构建的每一个步骤都需要相应的插件来完成,如果如果某个阶段没有绑定相应的插件,那么这个阶段就不会有任何实际的行为,例如default 生命周期的 integration-test阶段没有绑定相应的插件,那么我们只执行 install 或者 deploy 阶段时,执行到 integration-test阶段时不会有任何实际行为;
- 项目的打包类型会影响到构建的具体过程,因此default 生命周期的阶段和插件目标的绑定关系由项目的打包类型来决定的,打包类型可以通过 pom.xml packaging元素定义,可以再官网的 Welcome – Documentation – Introductions – the Build Lifecycle 下来各个打包类型所对应的生命周期与插件绑定的关系:
自定义插件绑定:例如创建项目的源码 jar 包:使用插件 maven-source-plugin 的 jar-no-fork 目标来完成,在pom.xml 中:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
周六一天美好回忆,昨天晚上调了个bug,贪玩了会,到1点了,早上不由自主的起晚了 9点了,大周六就原谅自己一把吧,今天没有什么安排,当然要开始最享受的方式了,最近在复习maven, 刚好前天买的《Maven实战》也到了,那还说啥,泡壶铁观音,关上门,开始看书吧,安静、惬意、留恋时间过得太快,早上看了书的前四章、介绍–安装–入门使用–背景案例;不知不觉的中午时间就到了,我也罪恶一把吧,泡了两包火鸡面加了一个蛋(ps: 受昨天晚上看奔驰小哥吃火鸡面的影响看他吃的那么好吃……..哎。。。。),继续续壶茶,迫不及待的看下面的内容,突然想到到了,我应该写个笔记,也好方便自己,如果能给朋友一点帮助,内心还是很高兴的(相逢何必曾相识,只因你给我点了个赞),尤其对我这种菜鸟,不应该是笨鸟,所以开始写了今天这篇博客,下午的内容大部分都在这篇博文中,不得不承认国内社区公认的 Maven 技术专家 Juven Xu写的书的确通俗易懂,又不失丰满;下午看了 第五章 坐标和依赖;第六章 仓库;第七章 生命周期和插件; 第八章 聚合和继承这部分内容还没写到博客呢,又到了吃饭时候,可恨,时间流的真么快。。。。最后真诚的感谢 Juven Xu写的这本书(虽然我也花了60大洋买的,但是写的确实还行)感谢apache maven这个模块的网站,写的很详细和负责,感谢这壶香茗,感谢毕夏的像梦一样自由。。。。。。