Maven大全

来源:互联网 发布:可爱表情贴图软件 编辑:程序博客网 时间:2024/05/01 10:07

Maven介绍

Maven资源

最好最权威的网站就是官方网站

Maven的基本功能

  1. 构建:比如生成class、jar、war或者ear文件
  2. 生成文档:比如生成javadoc、网站文档
  3. 生成报告:比如junit测试报告
  4. 生成依赖类库:生成文档,说明项目多其他软件的依赖
  5. 有关SCM(Software Configuration Management)软件配置管理,比如版本控制,比如bug管理等等
  6. 发布:生成供发布的分发包,比如生成Struts2的分发包,供提交给用户使用部署:比如,web应用程序,自动部署到指定的服务器上

Maven使用

Maven的安装和配置

  1. 从官方网站下载最新的Maven分发包http://maven.apache.org/download.html;
  2. 解压缩到本地;
  3. 配置maven, 将maven/bin目录设置到windows环境变量Path中.
  4. 检查maven是否安装成功, 在命令行中执行查看版本命令mvn -version.

常用命令:

//创建Maven项目mvn archetype:create//编译源代码mvn compile//编译测试代码:mvn test -compile//运行测试代码mvn test//打包mvn package//安装jar包到本地库mvn install//清除产生的项目:mvn clean//先清除再把包打到在本地库mvn clean install//跳过测试代码,打包mvn clean install -Dmaven.test.skip=true

重要概念

聚合和继承

传送门:

  1. 2者之间的区分和作用

  2. pom中的依赖详解, 聚合和继承的实际case

build的生命周期

Maven有一套构造的生命周期,构造jar war包时,会按照一套顺序走下来的。
Maven内置三种生命周期:default, clean 和 site。一个生命周期分为多个构造阶段。

下面是default生命周期,包含以下构造阶段:

  1. validate: 验证这个项目是正确的,并且所有需要的消息都是可用的。
  2. initialize: 初始化构建状态,设置属性,或创建目录。

  3. generate-sources: 生成源码文件,用来编译。

  4. process-sources: 处理源码,例如过滤任何值。

  5. generate-resources: 生成资源文件,放到包中。

  6. process-resources: 处理资源并拷贝到目标目录,为打包做准备。

  7. compile: 编译项目中的源码

  8. process-classes:后续处理编译生成的文件,例如在class文件上进行字节码扩展

  9. generate-test-sources:生成用来编译的测试源码。

  10. process-test-sources:处理测试源码,例如过滤值。

  11. generate-test-resources:创建可以用来测试的资源。

  12. process-test-resources:处理资源并且拷贝到测试的目标目录下。

  13. test-compile:把测试源代码编译进目标目录下。

  14. process-test-classes:后续处理测试编译生成的文件,例如对class类进行字节码扩展。
  15. test:运行单元测试框架进行测试,这些测试不需要代码被打包或被部署(复制到远程仓库上)。
  16. 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)
  17. package:把编译好的代码打包,如JAR .
  18. pre-integration-test:perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
  19. integration-test:process and deploy the package if necessary into an environment where integration tests can be run.
  20. post-integration-test:perform actions required after integration tests have been executed. This may including cleaning up the environment.
  21. verify:运行所有的检查,验证包是可用的并且满足质量标准。
  22. install:把包装到本地库中,可以在其他项目中,作为本地依赖来使用.
  23. deploy:把包存复制到远程仓库上去,供其他开发者或项目使用.

这些构造过程是按照顺序执行的,如果执行后面的构造过程,前面的构造过程也会被执行

例如:

mvn deploy

前面的install、verify一直到validate这些build phase都会执行。

每一个build phase是由goal组成的,一个goal其实就是一个任务,一个goal可以关联到一个build phase也可以不关联到任何build phase 。 不关联到任何phase的goal是可以独立执行的,例如:

mvn clean dependency:copy-dependencies package

上面的命令会导致先执行clean这个phase,然后拷贝依赖项,最后打包。注意,这里clean这个goal是clean这个lifecycle里面的一个goal,所以可以看到不同lifecycle的build phase和goal是可以混合在一起执行的。 如果一个goal被绑定到多个phase上,那么goal就会被执行多次。

phase的顺序是已经固定的,如果一个phase没有绑定到任何goal,那么phase就不会被执行。
一个goal可以通过两种方式绑定到一个phase,一个是指定packaging,另一个就是plugin。

plugin就是用来向maven提供goal任务目标的。
一个plugin里面可以有多个goal,这就是为什么我们在指明goal时,前面会用一个冒号与plugin的名字。

一个plugin自己可以指定自己的goal绑定到哪个lifecycle的哪一个Phase上,另外也可以配置一个goal绑定到哪个phase上。可以在pom.xml里面配置。 看两个配置:

<plugin>   <groupId>org.codehaus.modello</groupId>   <artifactId>modello-maven-plugin</artifactId>   <version>1.4</version>   <executions>     <execution>       <configuration>         <models>           <model>src/main/mdo/maven.mdo</model>         </models>         <version>4.0.0</version>       </configuration>       <goals>         <goal>java</goal>       </goals>     </execution>   </executions> </plugin>

这个就在当前的lifecycle里面添加了一个名字叫java的goal,这goal会根据自己的配置去绑定到一个phase,在phase执行的时候这个goal会执行。并且在这个配置里面,可以指定多个execution来让这个goal执行多次。

看另一个示例配置:

<plugin>   <groupId>com.mycompany.example</groupId>   <artifactId>display-maven-plugin</artifactId>   <version>1.0</version>   <executions>     <execution>       <phase>process-test-resources</phase>       <goals>         <goal>time</goal>       </goals>     </execution>   </executions> </plugin>

这个名为time的goal把自己绑定到了process-test-resource这个phase上。

在默认情况下,并不是所有的phase都绑定了goal,比如clean这个lifecycle是有三个phase的,但是只有其中的一个名为clean的phase默认绑定了一个clean:clean goal,其它两个phase默认没有绑定任何goal。

之前已经提到过packaging,在pom.xml可以指定packaging,每种packaging都设定了一组phase和goal之间的绑定关系。在default lifecycle下,当packaging为 ejb/ejb3/jar/par/rar/war 其中之一的值的时候,只有以下的phase绑定了goal,具体如下:

process-resources   resources:resourcescompile compiler:compileprocess-test-resources  resources:testResourcestest-compile    compiler:testCompiletest    surefire:testpackage jar:jarinstall install:installdeploy  deploy:deploy

总结

首先搞清楚maven的project的目录结构,然后理解maven的lifecycle,lifecycle是由build phase组成,每一个build phase会绑定到goal。goal是由plugin提供的。 每一种packaging的值都表明了一定的phase和goal之间的绑定关系。

创建Project

maven标准规范目录

my-app/|-- pom.xml`-- src    |-- main    |   |-- java    |   |   `-- com    |   |       `-- mycompany    |   |-- resources    |   `-- webapp    |       |-- WEB-INF    |       |   `-- web.xml    |       `-- index.jsp    `-- test        |-- java        |   `-- com        |       `-- mycompany        `-- resources

目录介绍

  1. src: 源码的存放位置
    1. resources: 资源文件的存放位置,如spring配置文件,properties文件
    2. webapp: 存放项目的启动文件和一些静态文件,如jsp文件,样式表,图片等
  2. test源码的存放位置

webapp文件的位置

Eclipse配置项目的源码路径

构建WebProject

传送门

Setting文件

传送门

pom.xml文件

传送门

内置变量

在maven中有一些默认的内置变量,我们可以直接调用:

  1. ${basedir} 项目根目录,存放 pom.xml和所有的子目录

  2. ${basedir}/src/main/java 项目的 java源代码

  3. ${basedir}/src/main/resources 项目的资源,比如说 property文件

  4. ${basedir}/src/test/java 项目的测试类,比如说 JUnit代码

  5. ${basedir}/src/test/resources 测试使用的资源

${project.build.directory} 构建目录,缺省为target。
${project.build.outputDirectory} 构建过程的输出目录,缺省为target/classes。
${project.build.finalName} 构建产出物的名称,缺省为${project.artifactId}-${project.version}
${project.packaging} 打包类型,缺省为jar。
${project.xxx} 当前pom文件的任意节点的内容。

如下:

 <build>    <outputDirectory>        ${project.build.directory}/${project.build.finalName}/WEB-INF/classes    </outputDirectory>    <plugins>        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-resources-plugin</artifactId>            <version>2.6</version>            <configuration>                <outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>            </configuration>        </plugin>        <plugin>            <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-jar-plugin</artifactId>                <version>2.4</version>            <configuration>                <classesDirectory>${project.build.directory}/${project.build.finalName}</classesDirectory>            </configuration>        </plugin>    </plugins>  </build>

常用插件

传送门

搭建私服

传送门

其他实际应用

0 0
原创粉丝点击