Hello Maven 4 - 生命周期和插件

Maven的生命周期是抽象的,抽象了项目构建的所有步骤,本身不做任何实际工作,实际的工作由插件来完成

Maven有三条独立的生命周期、同一生命周期内的阶段存在依赖关系
    Clean Lifecycle 清理
        pre-clean
        clean
        post-clean
        
    Default Lifecycle 构建
        validate
        initialize
        generate-sources
        process-sources
        generate-resources
        process-resources
        compile - 编译项目的主代码
        process-classes
        generate-test-sources
        process-test-sources
        generate-test-resources
        process-test-resources
        test-compile
        process-test-classes
        test
        prepare-package
        package - 接受编译好的代码,打包成可发布的格式,如jar
        pre-integration-test
        integration-test
        post-integration-test
        verify
        install 将包安装到Maven本地仓库
        deploy 将最终的包复制到远程仓库
        
    Site Lifecycle 发布项目站点
        pre-site
        site
        post-site
        site-deploy - 将生产的项目站点发布到服务器上
    
插件绑定
    生命周期各过程是通过绑定的插件实现的,默认的绑定参见:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
    查看插件的默认绑定阶段
    mvn help:describe -Dplugin = org.apache.maven.plugins:maven-source-plugin:2.1.1-Ddetail
    
自定义绑定
    在pom.xml中

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.1</version>
                <executions>
                    <execution>
                        <id>attact-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

插件配置
    命令行配置 如 mvn install -Dmaven.test.skip = true
    pom中全局配置 如

    <build>
        <plugins>
            <plugin>    
                <groupId>org.apache.maven.plugins</groupId>    
                <artifactId>maven-compiler-plugin</artifactId>   
                <version>3.2</version>
                <configuration>    
                    <source>1.7</source>    
                    <target>1.7</target>    
                    <encoding>UTF-8</encoding>
                </configuration>    
            </plugin>  
      </plugins>
    </build>

 pom中对插件目标单独配置

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <id>ant-validate</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>....</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

 

posted on 2015-03-09 11:08  汤能养身  阅读(236)  评论(0)    收藏  举报

导航