Maven学习小结

    Maven做为一个项目管理工具,通常用来构建项目,和管理项目中lib之间的依赖关系。

 
   1. 约定先于配置(Maven有一组默认约定可用,基本不需要再配置,与Ant相比,这是比较人性化的一个特点)如:Maven项目标准目录布局
src/main/java Application/Library sources
src/main/resources Application/Library resources
src/main/filters Resource filter files
src/main/assembly Assembly descriptors
src/main/config Configuration files
src/main/scripts Application/Library scripts
src/main/webapp Web application sources
src/test/java Test sources
src/test/resources Test resources
src/test/filters Test resource filter files
src/site Site
LICENSE.txt Project's license
NOTICE.txt Notices and attributions required by libraries that the project depends on
README.txt Project's readme
 
    2.Maven核心概念
        1)插件和目标(Plugins & goals)
        Maven是基本插件的,基本全部功能全是靠插件目标完成的,如 $mvn complier:compile是调用了complier插件的compile目标。
        一个插件是一个或多个目标的集合。
 

    2)构建生命周期(buil lifecycle)
        Maven有3个内置的构建生命周期:default, clean, site。每个生命周期都有好几个默认的阶段(phase)。 每个阶段定义了到这一步要完成的任务,当执行这一阶段时,之前的阶段会先依次执行再执行当前阶段。各个阶段上绑定了零个或多个插件目标,执行阶段,其实就是依次执行其上绑定的插件目标。 

    3)Maven坐标(Coordinates)Maven定义了一组标识,来唯一标识一个项目、一个依赖或者一个插件。通过groupId, artifactId,version,packaging四元组来标识一个项目的坐标。Maven仓库(Repository)通过坐标来定位相应的构件。

    4)Maven仓库(Repository)Maven仓库中存放着一些构件,根据坐标可查找到相应的构件。只有2种类型的仓库local和remote。当项目需要使用构件时,先查找local仓库,如果没有会从remote仓库上下载到local仓库。

    5)依赖管理(Dependencies Management)Dependency management is one of the features of Maven that is best known to users and is one of the areas where Maven excels(擅长)。传递依赖(Transitive Dependencies)allows you to avoid needing to discover and specify the libraries that your own dependencies require, and including them automatically。

    3.Maven Archetype 插件

     In short, Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made。 Archetype插件共有4个目标:generate, create-from-project, crawl, create(@deprecated)。

 mvn archetype:generate是根据一个原始模型,生成一个项目。

    

    4.POM     

    A Project Object Model or POM is the fundamental unit of work in Maven.It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects.在POM里可以指定项目的依赖,执行的插件和目标,以及项目的版本、描述、开发者、邮件列表等等。

 

    1)Super POM

    所有的Maven项目的POM都扩展自Super POM,Super POM定义了一组被所有项目共享的默认设置。Maven 3.x的Super POM在${M2_HOME}/lib/maven-model-builder-3.0.4.jar/org.apache.maven.model/pom-4.0.0.xml.部分代码

 

<build>

    <directory>${project.basedir}/target</directory>

    <outputDirectory>${project.build.directory}/classes</outputDirectory>

    <finalName>${project.artifactId}-${project.version}</finalName>

    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>

    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>

    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>

    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>

    <resources>

      <resource>

        <directory>${project.basedir}/src/main/resources</directory>

      </resource>

    </resources>

    <testResources>

      <testResource>

        <directory>${project.basedir}/src/test/resources</directory>

      </testResource>

    </testResources>

    <pluginManagement>

      <!-- NOTE: These plugins will be removed from future versions of the super POM -->

      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->

      <plugins>

        <plugin>

          <artifactId>maven-antrun-plugin</artifactId>

          <version>1.3</version>

        </plugin>

        <plugin>

          <artifactId>maven-assembly-plugin</artifactId>

          <version>2.2-beta-5</version>

        </plugin>

        <plugin>

          <artifactId>maven-dependency-plugin</artifactId>

          <version>2.1</version>

        </plugin>

        <plugin>

          <artifactId>maven-release-plugin</artifactId>

          <version>2.0</version>

        </plugin>

      </plugins>

    </pluginManagement>

  </build>

 

    

posted @ 2012-05-03 22:18  cuillgln  阅读(442)  评论(0编辑  收藏  举报