`
snoopy7713
  • 浏览: 1127491 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

maven2站点的定制和发布

阅读更多

mvn site功能强大,特别是在生成的项目站点中可以添加很多测试报告及文档。

一般需要解决3个问题:

1、如何定制站点的内容和样式?

2、mvn site生成的文件分布在各子模块目录下,如何统一管理?

3、如何将报告发布成站点,以便于项目组其它成员通过浏览器能够方便的查看?

 

 

--------------------------- ----------------------------------------- ------------------------------

站点定制:

一、如果不做任何配置,执行mvn site命令生成默认样式的站点,主要包括以下内容:

1、子模块列表Modules;

2、文档列表Project Documentation,主要有两大块:项目信息Project Information 、项目报告Project Reports;

3、 项目信息Project Information,默认包括项目相关概要信息、持续集成、依赖、插件、配置库等报告,详见官网(Apache > Maven > Plugins > Maven Project Info Reports Plugin):

http://maven.apache.org/plugins/maven-project-info-reports-plugin/

4、项目报告Project Reports,取决与pom.xml文件中<reporting>部分的配置,可集成checkstyle、cobertura、Findbugs等报告;

 

二、如果想要增加站点内容,或者减少项目信息报告,则需要建立site.xml文件,修改项目pom文件

1、在项目根目录下(与根pom同级)新建src\site目录,其下新建site.xml文件;

2、site.xml的标准格式和校验文件可以参见官网(Apache > Maven > Plugins > Maven Site Plugin > Configuring the Site Descriptor):

     http://maven.apache.org/plugins/maven-site-plugin/examples/sitedescriptor.html

3、编写site.xml,参考官网;
4、一个例子:
<?xml version="1.0" encoding="ISO-8859-1"?>
<project xmlns="http://maven.apache.org/DECORATION/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/DECORATION/1.0.0 http://maven.apache.org/xsd/decoration-1.0.0.xsd">
<!-- <bannerLeft>
    <name>TEST</name>
    <src>http://maven.apache.org/images/apache-maven-project.png</src>
        <href>http://maven.apache.org/</href> 

  </bannerLeft>
  <bannerRight>
    <src>http://maven.apache.org/images/maven-small.gif</src>
  </bannerRight>          -->
<body>

    <links>
      <item name="Up" href="../dashboard-report.html" />
      <item name="Home" href="http://www.yourcompany.com/projects/build/maven/site/projectname/dashboard-report.html" />     
    </links>
<menu ref="parent" />
<!--  <menu name="Overview">
     <item name="Introduction" href="index.html" />
     <item name="Project Summary" href="project-summary.html"/>      
     <item name="Plugin Management" href="plugin-management.html"/>
     <item name="Project Plugins" href="plugins.html"/>    
     <item name="Source Repository" href="source-repository.html"/>
   </menu>                                -->
<menu ref="modules" />
<menu ref="reports"/>
</body>
</project>
 
 
5、以上例子中,parent、modules、reports为保留字,生成的站点将包含以下内容:模块名(默认与各级pom文件中 的<name>对应)、上级模块链接(parent,方便返回上级目录)、子模块列表链接(modules)、文档列表(reports,包 括Project Information 和Project Reports)、返回上级目录和返回首页的链接
6、需要注意的是,无需配置Project Info Reports Plugin即默认包含所有Project Information报告,如果想要去掉其中某些报告,需要在pom文件中对Project Info Reports Plugin进行配置:
<reporting>
     <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>2.1.2</version>
        <reportSets>
          <reportSet>
            <reports>
             <report>index</report>
              report>summary</report>
          <!--         
             <report>project-team</report>
              <report>mailing-list</report>
              <report>cim</report>
              <report>issue-tracking</report>
              <report>license</report>      
           -->
              <report>scm</report>
            </reports>
          </reportSet>
        </reportSets>
      </plugin>
     </plugins>
   </reporting>
   
7、一般情况下,index页面需要生成,以免进入子模块时找不到链接。
 
三、javadoc报告和jxr代码链接

build pluginManagement和 build plugins 以及 reporting 部分增加 javadoc 插件,如果 pom 文件中已经配置,则只需作相应修改。 charset encoding docencoding 配置用于解决生成的 javadoc 文件中文乱码问题; aggregate 配置为 true javadoc 报告会集中显示所有子模块的 javadoc

reporting 部分增加JXR插件。

The JXR plugin produces a cross-reference of the project's sources. The generated reports make it easier for the user to reference or find specific lines of code. It is also handy when used with the PMD plugin and checkstyle pluginfor referencing errors found in the code.

 

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <aggregate>true</aggregate>
                <charset>UTF-8</charset>
                <encoding>UTF-8</encoding>
                <docencoding>UTF-8</docencoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jxr-plugin</artifactId>
        </plugin>
    </plugins>
</reporting>
     

四、集成Checkstyle、 Cobertura、Surefire、Findbugs

Checkstyle: Report on coding style conventions.
Cobertura Test Coverage: Cobertura Test Coverage Report.

Surefire Report: Report on the test results of the project.

FindBugs Report:Generates a source code report with the FindBugs Library.

在超级pom文件的build pluginManagement和 build plugins以及 reporting部分增加以上插件(详见《CheckStyle、cobertura、Findbugs与Maven2的集成》)。


<project>
 ...
 <build>
<!-- To define the plugin version in your parent POM -->
 <pluginManagement>
 <plugins>
      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.3</version>             
     </plugin> 
     <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>findbugs-maven-plugin</artifactId>
            <version>2.0.1</version>        
         </plugin> 
         <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-report-plugin</artifactId>
          <version>2.5</version>
         </plugin>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.4</version>
         </plugin>
		 ...
 </plugins>
 </pluginManagement>
 <!-- To use the plugin goals in your POM or parent POM -->
 <plugins>
      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.3</version>             
     </plugin> 
     <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>findbugs-maven-plugin</artifactId>
            <version>2.0.1</version>        
         </plugin> 
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-report-plugin</artifactId>
           <version>2.5</version>
         </plugin>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.4</version>
         </plugin>
  ...
 </plugins>
 </build>
 ...
 <!-- To use the report goals in your POM or parent POM -->
 <reporting>
 <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
            <configuration>
                <configLocation>Loong_Checks.xml</configLocation>
            </configuration>
            <version>2.3</version>      
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>2.5</version>
    </plugin>     
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.4</version>
    </plugin>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>findbugs-maven-plugin</artifactId>
        <version>2.0.1</version>
        <configuration>
            <xmlOutput>true</xmlOutput>
            <effort>Max</effort>
        </configuration>         
    </plugin>  
    ...
 </plugins>
 </reporting>
 ...
 </project>
 

五、全局聚合报告

当项目有多个子模块时,生成的站点需要到各级子目录下面才能看到。如果想要生成一个整体情况的报告,则需要使用dashboard插件来生成全局报告。官网:http://mojo.codehaus.org/dashboard-maven-plugin/usage.html

1、在项目pom文件build pluginManagement和 build plugins以及 reporting部分配置dashboard插件

<project>
 ...
      <build>
<!-- To define the plugin version in your parent POM -->
                   <pluginManagement>
                          <plugins>
                                 <plugin>
                                       <groupId>org.codehaus.mojo</groupId>
                                        <artifactId>dashboard-maven-plugin</artifactId>
                                        <version>1.0.0-beta-1</version>
                                    </plugin>
                                     ...
                          </plugins>
                   </pluginManagement>
          <!-- To use the plugin goals in your POM or parent POM -->
                   <plugins>
                            <plugin>
                                     <groupId>org.codehaus.mojo</groupId>
                                     <artifactId>dashboard-maven-plugin</artifactId>
                                     <version>1.0.0-beta-1</version>
                            </plugin>
                            ...
                   </plugins>
          </build>
          ...
 <!-- To use the report goals in your POM or parent POM -->
          <reporting>
                 <plugins>
                            <plugin>
                                     <groupId>org.codehaus.mojo</groupId>
                                     <artifactId>dashboard-maven-plugin</artifactId>
                                     <version>1.0.0-beta-1</version>
                            </plugin>
                            ...
                   </plugins>
          </reporting>
          ...
 </project>
 
2、在项目pom文件中配置下载地址
<pluginRepositories>
<pluginRepository>
<id>Codehaus repository</id>
<url>http://repository.codehaus.org/</url>
</pluginRepository>
</pluginRepositories>
 

3、使用dashboard的步骤如下:

1)       mvn site

(1)   generate the site .

(2)   let each report plugin generate its xml file.

(3)   add the dashboard report item in the left menu.

2)       mvn dashboard:dashboard

(1)   aggregate all results of each report.

(2)   re-generate the dashboard HTML file.

4、dashboard报告中默认聚集以下报告(如果有的话):

  • Test Coverage :
    • Cobertura : calculates the percentage of code accessed by tests.
    • Clover : calculates Test Coverage metrics
  • Code Quality :
    • CheckStyle : performs static code style analysis
    • PMD/CPD : performs Java source code analysis and Copy/Paste Detection
    • FindBugs : performs Java source code analysis to detect Bug patterns
    • JDepend : calculates design quality metrics by package
    • Taglist : performs static code analysis to find tags in the code, like @todo or //TODO tags.
  • Unit testing execution :
    • Surefire : executes the unit tests of an application.

 

六、一个典型的pom配置(reporting部分)

 

  <reporting>
             <plugins>  
               <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-project-info-reports-plugin</artifactId>
                           <version>2.1.2</version>
                           <reportSets>
                              <reportSet>
                                       <reports>
                                           <report>index</report>
                               <!--
                                           <report>summary</report>
                                           <report>project-team</report>
                                           <report>mailing-list</report>
                                           <report>cim</report>
                                           <report>issue-tracking</report>
                                           <report>license</report>
                                           <report>scm</report>
                                  -->
                        </reports>
                    </reportSet>
                </reportSets>
             </plugin>
        <!--         
              <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-javadoc-plugin</artifactId>
                           <version>2.4</version>
                           <configuration>
                               <aggregate>true</aggregate>
                               <charset>UTF-8</charset>
                                <encoding>UTF-8</encoding>
                                <docencoding>UTF-8</docencoding>
                         </configuration>
                </plugin>
           -->
               <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>dashboard-maven-plugin</artifactId>
                            <version>1.0.0-beta-1</version>
                   </plugin>
              <plugin>
                   <groupId>org.codehaus.mojo</groupId>
                   <artifactId>findbugs-maven-plugin</artifactId>
                   <version>2.0.1</version>
                   <configuration>
                            <xmlOutput>true</xmlOutput>
                            <effort>Max</effort>
                    </configuration>         
               </plugin>    
               <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-checkstyle-plugin</artifactId>
                          <configuration>
                                    <configLocation>Loong_Checks.xml</configLocation>
                          </configuration>
                       <version>2.3</version>      
               </plugin>
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-surefire-report-plugin</artifactId>
                                <version>2.5</version>
               </plugin>     
               <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>cobertura-maven-plugin</artifactId>
                    <version>2.4</version>
               </plugin>             
               <plugin>
                               <groupId>org.apache.maven.plugins</groupId>
                               <artifactId>maven-jxr-plugin</artifactId>                          
               </plugin>             
             </plugins>
       </reporting>
 

-------------------------------- ----------------------------------------------- ---------------------------------

站点的生成分两种情况:

当然,不论哪种情况,首先必须确认根目录pom文件中配置了site插件。

<project>
  ...
  <build>
    <!-- To define the plugin version in your parent POM -->
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-site-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        ...
      </plugins>
    </pluginManagement>
    <!-- To use the plugin goals in your POM or parent POM -->
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <version>2.1</version>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>
 

WARNING1 :

maven site插件2.1及之前的版本存在bug,当项目pom文件中的version使用了变量的时候(见以下示例),会导致site执行失败或者取不到version、parent的值,见MSITE-337

2.1之前的版本,site命令可以完成,只是如果site.xml中定义了version、parent,将取不到值,并且子模块无法继承根目录下配置的site样式,在各子模块下增加site样式目录和文件即可。

2.1版本,此种情况会报错,site终止。

 

<project>
     <modelVersion>4.0.0</modelVersion>
     <groupId>group</groupId>
     <artifactId>artifact</artifactId>
     <packaging>pom</packaging>
     <version>${version.major}.${version.minor}</version>
  <properties>
      <version.major>1</version.major>
      <version.minor>0</version.minor>
  </properties>
  <modules>
      <module>module</module>
  </modules>
</project>
 WARNING2 :

如果发现某些代码检查插件被跳过,可能是因为项目目录结构与maven默认结构不一致,但pom文件中未进行配置。

maven默认目录:

<sourceDirectory>src/main/java</sourceDirectory>
<outputDirectory>target/classes</outputDirectory>
 配置超级pom,覆盖默认的源码和输出目录:
<build>
...
<sourceDirectory>src/java</sourceDirectory>
<outputDirectory>classes</outputDirectory>
...
</build>
 

一、生成本地测试报告

1、修改pom文件,配置site信息

<distributionManagement>
    <site>
      <id>www.yourcompany.com</id>     
      <url>file:///opt/nfs/build/site/projectname</url>
    </site>
</distributionManagement>
 

2、到项目根目录下(与根pom同级)执行mvn clean site dashboard:dashboard site:stage -DstagingDirectory=D:\fullsite -fn

可在本地生成包含Surefire单元测试报告、dashboard聚合报告的文件。

clean:清空旧的文件;

dashboard:dashboard:生成dashboard聚合报告;

stage(maven2.0- beta-5以上版本才支持):将site集成生成到stagingDirectory下面,如果不指定-DstagingDirectory,则默认集 成到当前根目录下(\target\staging\localhost\opt\nfs\build\site\projectname);

-fn:忽略编译失败,不会为一个失败停止,也不会报告失败。

3、如果嫌目录太长,可以做如下配置:

 

<distributionManagement>
    <site>
      <id>www.yourcompany.com</id>     
      <url>file://</url>
    </site>
</distributionManagement>
 

则生成的文件在当前根目录下(\target\staging\localhost)。

4、使用site:stage可以将报告统一管理,而不必到各子目录下找,比较方便。

5、注意在站点尚未生成完毕的时候不要打开dashboard报告文件,否则可能导致dashboard报告文件为空。

 

二、将站点发布到HTTP server

1、首先,需要得到站点发布地址的服务器信息,修改maven安装目录下的settings.xml文件,配置server信息:

<servers>
    <server>
      <id>www.yourcompany.com</id>
      <username>my_login</username>
      <password>my_password</password>
   </server>
</servers>
 

id:HTTP server 的id。

username, password:用于登陆此服务器的用户名和密码

2、修改pom文件,指定站点发布地址。

//Homepage信息,注意与http访问地址保持一致

 

<groupId>org.yourcompany.test</groupId>
 <artifactId>test</artifactId>
 <version>1</version>
 <description>test</description>
 <name>test</name>
 <url>http://www.yourcompany.com/projects/build/maven/site/projectname</url>
 

//发布地址信息,与httpserver管理员沟通,需要相关目录权限,以下两种方式二选其一

//scp方式

<distributionManagement>
    <site>
      <id>www.yourcompany.com</id>
      <url>scp://www.yourcompany.com/www/docs/project</url>
    </site>
</distributionManagement>
 //file方式
<distributionManagement>
    <site>
      <id>www.yourcompany.com</id>     
      <url>file:///opt/nfs/build/site/projectname</url>
    </site> 
</distributionManagement>
 如果想要通过file方式从本机发布到httpserver,可在httpserver设置文件夹共享(httpserver是linux操作系统可通过 samb设置),然后映射到本地磁盘。修改pom文件中的distributionManagement配置即可,其他不变:

<distributionManagement>
    <site>
      <id>www.yourcompany.com</id>     
      <url>file://z:/projectname</url>
    </site> 
</distributionManagement>
 

id:HTTP server的id;

url:文件存放目录,目前支持scp(需要配置用户、权限等)和file方式传送。使用file方式表示直接拷贝文件到本机,如果http server不在本机,使用共享文件夹的方式。

3、到项目根目录下(与根pom同级)执行mvn clean site dashboard:dashboard site:deploy -fn:发布站点,即可通过浏览器访问(http)。

4、此时不能使用site:stage,否则子module的链接不正确,通过浏览器无法正确访问子模块页面。

5、注意在maven2.1之前的版本,存在maven site在linux上生成的子module的link不正确的bug。若要正确访问子module,需要使用maven2.1以上的版本。   

6、执行site dashboard:dashboard site:deploy,生成的dashboard页面没有默认name作为页面标题;

执 行site site:deploy,发现生成的dashboard页面标题正常,但dashboard报告只会聚合子目录报告,而不会聚合同级报告,如果项目有多个 子modules,且根目录级别没有代码,报告为空,那么site site:deploy根目录下的dashboard报告数据是没有问题的;

一个折衷的办法是site dashboard:dashboard site site:deploy,则页面标题正常,dashboard报告内容也正确。

-------------------------------- ----------------------------------------------- ---------------------------------

 

转载地址:http://julianlali.blog.163.com/blog/static/58133643201041024047675/

 

 




 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics