Maven:站点生成

一、生成简单的站点 
用户可以让Maven自动生成一个Web站点,以Web的形式发布如项目描述、版本控制系统地址、缺陷跟踪系统地址等,更便捷、更快速地为团队提供项目当前的状态信息; 
1.在pom.xml文件中,配置maven-site-plugin插件(Maven的site生命周期如果默认绑定了site插件就可以不配置): 
 
    ... ...  
     
         
             
                org.apache.maven.plugins 
                maven-site-plugin 
                3.3 
             
         
     
    ... ...  
 
2.配置正确版本之后,在项目之下运行mvn site就能直接生成一个最简单的站点; 
C:\Users\chengxiang.peng.QUNARSERVERS\QunarGitSources\phone_spider_project>mvn site 
[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building check phone 1.0-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-site-plugin:3.3:site (default-site) @ phoneproject --- 
[WARNING] Report plugin org.apache.maven.plugins:maven-project-info-reports-plugin has an empty version. 
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. 
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects. 
[INFO] configuring report plugin org.apache.maven.plugins:maven-project-info-reports-plugin:2.9 
[WARNING] No project URL defined - decoration links will not be relativized! 
[INFO] Rendering site with org.apache.maven.skins:maven-default-skin:jar:1.0 skin. 
[INFO] Generating "Dependencies" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Dependency Convergence" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Dependency Information" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "About" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Plugin Management" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Plugins" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Summary" report    --- maven-project-info-reports-plugin:2.9 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 29.989 s 
[INFO] Finished at: 2016-10-20T12:09:41+08:00 
[INFO] Final Memory: 26M/357M 
[INFO] ------------------------------------------------------------------------ 
3.待Maven运行完成后,可以再项目的target/site/目录下找到Maven生成的站点文件,包括dependencies.html、dependency-convergence.html、index.html和css、images文件夹; 
Project [Phoneproject] config target images dependencies.html dependency-convergence,html dependency-info.html Index.html plugin-management.html plugins.html project-info.html project-summary.htm .gitignore PhoneProject.iml m pom.xml External Libraries
4.点击index.html文件,打开生成的站点; 
C O localhost:63342/PhoneProject/target/site/project-info.htmI check phone Last Published: 2016-10-20 | version: 1.0-SNAPSHOT check phone Project Documentation Project Information Dependencies Dependency Convergence Dependency Information About Plugin Management Plugins Summary maven Project Information This document provides an overview of the various documents and links that are part of this project's general information. All of this content is automatically generated by Maven on behalf of the project. Overview Document Dependencies Dependency Convergence Dependency Information About Plugin Management Plugins Summary Description This document lists the project's dependencies and provides information on each dependency. This document presents the convergence of dependency versions across the entire project, and its sub modules. This document describes how to to include this project as a dependency using various dependency management tools. There is currently no description associated with this project. This document lists the plugins that are defined through pluginManagement. This document lists the build plugins and the report plugins used by this project. This document lists other related information of this project Copyright 2016. All Rights Reserved.
二、丰富项目的信息 
1.默认情况下,Maven生成的站点包含了很多项目的信息链接,这其实是由一个名为maven-project-info--reports-plugin的插件(Maven3中,该插件内置在maven-site-plugin中,Maven2内置在核心源码中)生成的;该插件会基于POM配置生成下列项目信息报告(见上站点截图): 
  关于(about):项目描述; 
  持续集成(Continuous Integeration):项目持续化集成服务器信息; 
  依赖(Dependencies):项目依赖信息,包括传递性依赖、依赖图、依赖许可证以及依赖文件的大小、所包含的类的数目; 
  依赖收敛(Dependency Convergence):针对多个模块项目生成,提供一些依赖健康状况分析; 
  依赖管理(Dependency Management):基于项目的 依赖管理生成的报告; 
  问题追踪(Issue Tracking):项目问题追踪系统信息; 
  邮件列表(Mailing Lists):项目的邮件列表信息; 
  插件管理(Plugin Management):项目所有项目插件的列表; 
  项目许可证(Project License):项目许可证信息; 
  项目概述(Project Summary):项目概述包括坐标、名称、描述等; 
  项目团队(Project Team):项目团队信息; 
  源码仓库(Source Repository):项目的源码仓库信息; 
2.Maven不会凭空生成信息,只有用户在POM中提供了相关配置后,站点才可能包含这些信息的报告。为了让站点包含完整的项目信息,需要配置PMO如下: 
 
    ... ...  
    http://phoneproject.qunar.com 
     
    check phone description. 
     
     
        scm:git:http://gitlab.corp.chengxiang.com/mobile_hotel_res/phone_spider_project 
        scm:git:[email protected]:mobile_hotel_res/phone_spider_project.git 
         
        http://gitlab.corp.chengxiang.com/mobile_hotel_res/phone_spider_project 
     
     
     
        Jenkins 
        http://ci.chengxiang.com/phoneproject 
     
     
     
         
            chengxiang.peng 
            chengxiang.peng 
            [email protected] 
            8 
         
     
     
     
        JIRA 
        http://jira.chengxiang.com/phoneproject 
     
    ... ...  
 
3.执行mvn site重新生成站点,发现对比简单站点,多生成了如 "CI Management" report、"Source Code Management" report等; 
C:\Users\chengxiang.peng.QUNARSERVERS\QunarGitSources\phone_spider_project>mvn site 
[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building check phone 1.0-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-site-plugin:3.3:site (default-site) @ phoneproject --- 
[WARNING] Report plugin org.apache.maven.plugins:maven-project-info-reports-plugin has an empty version. 
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. 
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects. 
[INFO] configuring report plugin org.apache.maven.plugins:maven-project-info-reports-plugin:2.9 
[INFO] Relativizing decoration links with respect to project URL: http://phoneproject.qunar.com 
[INFO] Rendering site with org.apache.maven.skins:maven-default-skin:jar:1.0 skin. 
//新增的报告生成 
[INFO] Generating "CI Management" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Dependencies" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Dependency Convergence" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Dependency Information" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "About" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Issue Management" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Plugin Management" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Plugins" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Team" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Source Code Management" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Summary" report    --- maven-project-info-reports-plugin:2.9 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 19.624 s 
[INFO] Finished at: 2016-10-20T15:21:46+08:00 
[INFO] Final Memory: 26M/354M 
[INFO] ------------------------------------------------------------------------

3.有些时候,我们并不需要生成某些项目信息,如可能不想公开源码仓库信息。可以通过maven-project-info-reports-plugin选择性生成信息项目,pom.xml配置如下: 

 
    ... ...  
     
     
         
             
             
                org.apache.maven.plugins 
                maven-project-info-reports-plugin 
                2.9 
                 
                     
                         
                            dependencies 
                            project-team 
                         
                     
                 
             
         
     
    ... ...
4.mvn site生成站点: 
C:\Users\chengxiang.peng.QUNARSERVERS\QunarGitSources\phone_spider_project>mvn site 
[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building check phone 1.0-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-site-plugin:3.3:site (default-site) @ phoneproject --- 
[INFO] configuring report plugin org.apache.maven.plugins:maven-project-info-reports-plugin:2.9 
[INFO] Relativizing decoration links with respect to project URL: http://phoneproject.qunar.com 
[INFO] Rendering site with org.apache.maven.skins:maven-default-skin:jar:1.0 skin. 
//只生成了上面配置的Dependencies和Team报告 
[INFO] Generating "Dependencies" report    --- maven-project-info-reports-plugin:2.9 
[INFO] Generating "Team" report    --- maven-project-info-reports-plugin:2.9 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 36.054 s 
[INFO] Finished at: 2016-10-20T15:30:33+08:00 
[INFO] Final Memory: 24M/275M 
[INFO] ------------------------------------------------------------------------ 
三、项目报告插件
除了默认的项目信息报告,Maven社区还提供了大量报告插件,只要稍加配置,用户就能让Maven自动生成各种内容丰富的报告。一般的插件在配置,报告插件在配置: 
1.JavaDocs:使用JDK的javadoc工具,基于项目源代码生成JavaDocs文档; 
pom.xml配置如下: 
 
    ... ...  
     
     
         
             
             
                org.apache.maven.plugins 
                maven-javadoc-plugin 
                2.7 
             
         
     
    ... ...  
 

执行mvn site生成站点中,包含javadoc文档Project Reports->JavaDocs如下:
comchengxiangphoneproject comchengxianqphoneprojectconfig comchengxianqphoneprojectdao comchengxianqphoneprojectdaoimpl comchengxianqphoneprojectdomaimarea comchengxianqphoneprojectdomaimareafeponse comchengxianqphoneprojectdomaimphone comchengxianqphoneprojectdomain.proxy comchengxianqphoneprojectservice.impl com.chengxiang.phoneproject.utils Area AreaDaolmpl AreaResponse Areaservicelmpl aaseModel BaseReponse CheckCallable CheckState Config DBPoolScheduleThread GetPhoneThread HttpUtils IAreaDao 'Area Service IPhoneDao IPh on e Service IPModeI JdbcUtils Main check phone I.O.SNAPSHOT API com.chengxiang.phoneproject com.chengxiang.phoneproject.config com.chengxiang.phoneproject.dao com.chengxiang.phoneproject.dao.impl com.chengxiang.phoneproject.domain.area com.chengxiang.phoneproject.domain.area.reponse com.chengxiang.phoneproject.domain.phone com.chengxiang.phoneproject.domain.proxy com.chengxiang.phoneproject.service com.chengxiang.phoneproject.service.impl com.chengxiang.phoneproject.thread com.chengxiang.phoneproject.utils Copyright 02015 All Rights Reservec[

2.Source Xref:能够随时随地打开浏览器访问项目的最新源代码;
pom.xml配置: 

 
    ... ...  
     
     
         
             
             
                org.apache.maven.plugins 
                maven-jxr-plugin 
                2.2 
             
         
     
     
     
         
             
             
                org.apache.maven.plugins 
                maven-jxr-plugin 
                2.2 
             
         
     
    ... ...  

执行mvn site生成站点中,包含源码文档Project Reports->Source Xref如下:
All Classes Packages com chengxiang.phoneproject com chengwang.phoneproect.config com chengxiang.phoneproject.dao com chengxiang.phoneproject.dao.impl com chengwang.phoneproect.domain.area com chengwang.phoneproect.domain.area_repons com chengxiang.phoneproject.domain.ohone com chengxiang.phoneproject.domain.proxy com chengwang.phoneproect.serwce com chengwang. phoneproect. service. impl All Classes AmountRu e AreaDaoImpl AreaReponseData AreaResponse AreaServiceImpl BaseMode BaseReponse CheckCalIable CheckState Config DBPoolScheduIeThread GetPhoneThread HttplJtils IAreaDao V ew Javadoc pa*age com. chengxiang. phoneproject. domai_n. area: * Created by chervxåmg. pere on 2015/9/22. public class { private String mAreaId: private St ring mA_reaUame: private String mHomeProvi_nce private String mOper*eId: private String mSaIeCode mAreaId = mA_reaUame = . mHomeProvi_nce = mOper*eId = mSaIeCode = * & a_ran aAreaId * & a_ran aArea,Vane * arm aHoneProvince public Area (String a_AreaId, String this. mAreaId = aAreaId: this. mAreaName = aAreaName: this. nHomeProvince = aHomeProvince: String aHomeProvi_nce)  

3.CheckStyle:帮助开发人员遵循编码规范的工具,能根据一套规则自动检查Java代码,使得团队能够方便地定义自己的编码规范; 
pom.xml配置如下: 
 
    ... ...  
     
     
         
             
             
                org.apache.maven.plugins 
                maven-checkstyle-plugin 
                2.5 
                 
                 
                    ${project.basedir}/config/checkstyle/checkstyle.xml 
                 
             
         
     
     
     
         
             
             
                org.apache.maven.plugins 
                maven-checkstyle-plugin 
                2.5 
                 
                 
                    ${project.basedir}/config/checkstyle/checkstyle.xml 
                 
             
         
     
    ... ...  
 
Checkstyle.xml配置如下(详细配置信息,查看官方文档:http://checkstyle.sourceforge.net/checks.html): 
 
 
 
     
         
         
             
         
        
         
             
             
         
        … …. 
         
          
             
             
             
         
     
    … ….. 

执行mvn site生成站点中,包含源码文档Project Reports->CheckStyle如下:
check phone Last Published: 2016-10-20 | version: 1.0-SNAPSHOT Project Documentation Project Information Java Docs Source Xref maven heckstyle Results e following document contains the results of Checkstyle Summary Files Files Files I nfos Warnings com/chengxiang/phoneproject/thread/checkcallable.java Rules Rules PackageName format: Violations check phone Errors Seve rity Error  

4.PMD:Java源代码分析工具,能够寻找代码中的问题,包括潜在的bug、无用的代码、可优化的代码、重复代码以及过于复杂的表达式; 
pom.xml配置如下: 
 
    ... ... 
     
     
         
             
             
                org.apache.maven.plugins 
                maven-pmd-plugin 
                3.7 
             
         
     
     
     
         
             
             
                org.apache.maven.plugins 
                maven-pmd-plugin 
                3.7 
             
         
     
    ... ...  
 
执行mvn site生成站点中,包含源码文档Project Reports->PMD如下:
check phone Last Published: 2016-10-20 | version: 1.0-SNAPSHOT Project Documentation Project Information Project Reports JavaDocs Source Xref Checkstyle PMD Built by: maven PMD Results The following document contains the results of PMD 5.5.1. Files com/chengxiang/phoneproject/thread/CheckCallable.java Violation Avoid empty if statements com/chengxiang/phoneproject/thread/DBPoolScheduleThread.java Violation rriding method merely calls super check phone Line 218-220 Line 8-10 Copyright 2016. All Rights Reserved.
关于PMD自定义规则详情查看(http://pmd.sourceforge.net/pmd-4.3.0/howtowritearule.html) 
5.ChangeLog:基于版本控制系统中就近的变更记录生成三份报告(貌似只支持github); 
6.Cobertura:生成测试覆盖率报告; 
pom.xml配置: 
 
    ... ...  
     
     
         
             
             
                org.apache.maven.plugins 
                cobertura-maven-plugin 
                2.7 
             
         
     
     
     
         
             
             
                org.apache.maven.plugins 
                cobertura-maven-plugin 
                2.7 
             
         
     
    ... ...  
 
执行mvn site生成站点中,包含源码文档Project Reports->Coberatura Test如下: 
Packages Coverage Report All Packaqes - Ali Packages # Classes Line Coverage Branch Coverage Complexitv com com com com com com com com com com com com . chen . chen . chen . chen . chen xlan xlan xlan xlan xlan xlan xlan xlan xlan xlan xlan . hone . hone . hone . hone . hone ro ect.dao ro ect.domain. ro ect.domain. ro ect.domain. ro ect.utils area. hon rox on se com com com com com com com com com com com chen . chen . chen xlan xlan xlan xlan xlan xlan xlan xlan xlan xlan xlan hone . hone . hone ro'ect ro ect.dao ro ect.utils Package area.re on se ro 16-10-20 1.55 1.444 1.125 3.056 All Packages Classes Area (0%) AreaDao[m I (0%) AreaRes onse (0%) AreaService[m I (0%) BaseModel (0%) BaseRe onse (0%) Report generated by Cobertura 2.1.1 on
四、自定义站点外观 
Maven生成的站点非常灵活,除了前面提到的标准信息报告和其它创建生成的报告,还能够自定义站点的外观和布局: 
1.创建如下文件目录(src/site): 
Project [Phoneproject] config t site apttest.apt faqtest.fml resources images right.jpg
2.分别创建site.xml、faqtest.fml和apttest.apt文件: 
src/site/site.xml文件(定义了站点描述符,头部内容及外观-1、2、3、4,皮肤-7,导航边栏-5,创建自定义页面-5):
 
 
     
     
        Phone Site 
         
        http://tika.apache.org/asf-logo.gif 
        http://www.baidu.com 
     
     
     
     
        java 
         
        images/right.jpg 
        http://www.hao123.com 
     


     
     


     
         
         
             
             
         


         
         
             
             
         
         
             
             
         


         
         
     


     
     
        com.googlecode.fluido-skin 
        fluido-skin 
        1.3 
     
 
src/site/fml/faqtest.fml文件(一种用来创建FAQ页面的XML文档格式) 
 
 
       
        title1 
         
            what? 
             
                

answer1

              

answer2

          
      
    
src/site/apt/apttest.apt文件(一种类似于维基的文档格式,用它来快速创建简单而又结构丰富的文档) 
what is apt? 
* aaaa 
* bbbb 
3.执行mvn site生成站点中,生成自定义站点如下: 
TheApache Software Foundation Version: 1.0-SNAPS I youku > check phone apt test Examples Project Documentation Project Information P roject Reports Built maven fqltest title titlel 1. what? titlel what? answerl answer2 Maven Last Published: 2016-10-20 Copyright @ 2016. All Rights Reserved.
五、国际化 
1.要正确的生成简单中文站点,首先要确保项目所有的源码,包括pom.xml、site.xml以及apt文档等,都是使用UTF-8编码保存; 
2.接下来我们配置pom.xml,配置编码格式,本地语言; 
 
    ... ...  
     
     
         
        UTF-8 
        UTF-8 
     


     
     
         
             
             
                org.apache.maven.plugins 
                maven-site-plugin 
                3.3 
                 
                 
                    zh_CN 
                 
             
         
     
    ... ....  
 
3.执行mvn site生成站点中,生成本地化站点如下:

你可能感兴趣的:(Maven,Maven实战)