maven系列2-maven的进阶

Table of Contents

1 灵活的构建

1.1 Maven属性

  • 内置属性:两个主要的内置属性,basedir表示项目根目录,version表示项目的版本
  • POM属性:用户可以使用POM文件中对应元素的值。例如${project.artifactId}
  • 自定义属性
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
  • Settings属性:引用settings.xml文件中XML的值
  • Java系统属性:所有Java系统属性都可以使用Maven属性引用。例如${user.home}
  • 环境变量属性:所有环境变量都可以使用Maven属性引用。 例如${env.JAVA_HOME}

1.2 Maven Profile

<profiles>  
        <profile>  
             <id>profileTest1</id>  
             <properties>  
                    <hello>world</hello>  
             </properties>  
             <activation>  
                    <activeByDefault>true</activeByDefault>  
             </activation>  
        </profile>  

        <profile>  
             <id>profileTest2</id>  
             <properties>  
                    <hello>andy</hello>  
             </properties>  
        </profile>  
 </profiles>  

1.3 激活profile

  • 命令行激活
$mvn clean install -PprofileTest1
  • settings文件显示激活

    <activeProfiles>  
         <activeProfile>profileTest1</activeProfile>  
    </activeProfiles>  
    
  • 系统属性激活
  • 操作系统环境激活
  • 文件存在与否激活
  • 默认激活

     <activation>  
          <activeByDefault>true</activeByDefault>  
    </activation>  
    

1.4 Profile种类

  • pom.xml
  • 用户settings.xml
  • 全局settings.xml

2 编写maven插件

2.1 一般步骤

  1. 创建一个maven-plugin项目:插件本身也是maven项目,特殊的是packaging必须是maven-plugin
  2. 为插件编写目标:编写插件的时候必须提供一个或者多个继承自AbstractMojo类
  3. 为目标提供配置点:插件及其目标都是可配置的
  4. 编写代码实现目标行为:根据实际需要实现Mojo
  5. 错误处理及日志:当Mojo发生异常时,根据情况控制Maven的运行状态。在代码中编写必要的日志以便为用户提供足够的信息。
  6. 测试插件

2.2 Mojo标注,参数

  1. 继承AbstractMojo
  2. 实现excute方法
  3. 提供@goal标注
    • @goal <name>
    • @phase <phase>
    • @excute goal= “<goal>”
    • @excute phase= "<phase>"
/**
 * Location of the file.
 */
@Parameter( defaultValue = "${project.build.directory}", property = "outputDir", required = true )
private File outputDirectory;

2.3 错误处理和日志

throw new MojoExecutionException( "Error creating file " + touch, e );

2.4 测试Maven插件

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.7</version>

3 archetype

使用Maven Archetype快速生成项目骨架

Archetype并不是Maven的核心texing,它也是通过插件来实现的。maven-archtype-plugin

批处理方式使用Archetype

mvn archetype:generate \
        -DarchetypeGroupId=pl.codeleak \
        -DarchetypeArtifactId=spring-mvc-quickstart \
        -DarchetypeVersion=1.0.0 \
        -DgroupId=my.groupid \
        -DartifactId=my-artifactId \
        -Dversion=version \
        -DarchetypeRepository=http://kolorobot.github.io/spring-mvc-quickstart-archetype

3.1 编写Archetype

mvn archetype:generate

自定义archetype关键目录

  • pox.xml Archetype自身的pox.xml
  • src/main/resources/archetype-resources/pom.xml基于该Archetyoe生成的项目的POM原形
  • src/main/resources/META-INF/maven/archetype-metadata.xml Archetype的描述文件
  • src/mian/resources/archetype-resources/** 其他需要包含在Archetype中的内容

其中通过fileSets设置了需要被用于项目中的文件,通过filtered,packaged分别设置指定目录下包含的文件是否需要属性值替换,同时对应的目录是否需要生成包目录

<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
                   xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <fileSets>
        <fileSet filtered="true" packaged="true">
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </fileSet>
        <fileSet filtered="true" packaged="true">
            <directory>src/test/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </fileSet>
        <fileSet filtered="true" packaged="false">
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </fileSet>
    </fileSets>
    <requiredProperties>
        <requiredProperty key="groupId">
            <defaultValue>com.zheng.archetypestudy</defaultValue>
        </requiredProperty>
    </requiredProperties>
</archetype-catalog>

通过mvn clean install将archetype项目打包到本地仓库中

3.2 Archetype Catalog

在使用maven-archetype-plugin插件时,会得到一个列表供选择,这个列表的信息来源于一个名为archetype-catalog.xml的文件

用户可以自定义这个archetype-catalog.xml中的内容,当然也可以通过扫描本地仓库自动生成基于该仓库的archetype-catalog.xml文件

archetype-catalog.xml配置文件来源

  • internal maven内置的archetypeCatalog
  • local 指向用户本地的archetype catalog,其位置为C:\Users\Administrator\.m2\archetype-catalog.xml,但是该文件默认是不存在的
  • remote 指向了maven中央仓库的archetype catalog,具体地址为http://repo1.maven.org/maven2/archetype-catalog.xml
  • file://… 用户可以指定本机任何位置的archetype-catalog.xml文件
  • http://… 用户可以使用http协议指定远程的archetype-catalog.xml文件

上面几种方式可以通过mvn archetype:generate命令的时候,使用archetypeCatalog指定插件使用的catalog,例如:

mvn archetype:generate -DarchetypeCatalog=local

mvn archetype:crawl -Drepository=C:/workspace/repository -Dcatalog=C:/Users/Administrator/.m2/archetype-catalog.xml

3.3 常用的archetype

  • maven-archetype-quickstart默认值 常用于一般javase项目结构
  • maven-archetype-webapp 用于web项目架构
  • appfuse archetype appfuse是一个集成了很多开源工具的项目,在于帮助程序员快速高效的创建项目,它提供了大量archetype,方便用户使用各种类型的项目

4 其它知识

4.1 持续集成

简单来说,持续集成就是 快速高频率自动 构建 项目地 所有源码 ,并且为项目成员提供丰富地 反馈 信息。

4.2 项目站点

maven可以为自己地项目生成Maven站点,更便捷、更快速地为团队提供项目当前的状态信息。

4.3 gpg签名

gpg能够帮助我们为文件生成签名、管理密钥以及验证签名等。