生成项目站点
maven不仅仅是一个自动化构建工具和依赖管理工具,它还能够帮助聚合项目信息,促进团队间的交流。POM可以包含各种项目信息,如项目描述、版本控制系统地址、缺陷跟踪系统地址、许可证信息、开发者信息等。用户可以让maven自动生成一个web站点以web的形式发布这些信息,此外,还有大量的项目插件可以生成各种项目报告,包括测试覆盖率报告、静态代码分析、代码变更等。
最简单的站点
<!--生成项目站点--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId> <version>3.6</version> </plugin>
运行mvn site命令,就可以生成一个简单的项目站点。
如果是一个聚合项目,则生成的web站点中的模块连接是无效的,如果将站点发布到服务器,这个问题会得到解决,这是站点文件目录的问题,项目生成的基本信息是通过pom的配置信息生成的。
丰富项目信息
<!--选择性生成项目信息报告--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> <version>2.1.2</version> <reportSets> <reportSet> <reports> <report>dependencies</report> <report>project-team</report> <report>issue-tracking</report> <report>license</report> </reports> </reportSet> </reportSets> </plugin>
上述配置意思是只包含这四项信息,该插件是配置在reporting元素下的plugins元素下的plugin元素。
项目报告插件
javaDocs:使用JDK的javadoc工具,基于源代码生成的javaDocs文档,最新版支持聚合。
source xref:以web页面的形式展示最新的java源代码,若是聚合模块需要额外的配置。
checkStyle:检查代码规范的插件,可以自定义校验规则。
pmd:源代码分析报告,可以自定义分析规则。
changeLog:基于版本控制系统生成变更报告。
cobertura:生成测试覆盖率报告。
生成的站点还可以进行国际化配置。
生成的站点还可以自动部署,以上插件的配置和部署方式如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--指定pom模型的版本,对于maven2和maven3来说,只能是4.0.0--> <modelVersion>4.0.0</modelVersion> <!--接下来三个元素是定义一个项目的基本坐标--> <!--定义该项目属于哪个组,这个组往往和项目所在的组织或公司存在关联--> <groupId>com.shw.test</groupId> <!--定义当前maven项目在组织中的唯一ID--> <artifactId>test-parent</artifactId> <!--定义当前项目的版本--> <version>1.0.0-SNAPSHOT</version> <!--项目的打包类型 默认为jar--> <packaging>pom</packaging> <!--项目友好的名称,用于用户间交流--> <name>我的测试项目</name> <!--项目描述信息,用于项目文档--> <description>测试项目站点</description> <!--项目地址信息,用于项目文档--> <url>http://maven.apache.org</url> <!--项目组织信息--> <organization> <name>shw.com</name> <url>http://shw.com.cn</url> </organization> <!--源码仓库信息--> <scm> <!--只读的scm地址--> <connection>scm:git:https://github.com/123/test-parent.git</connection> <!--可写的scm地址--> <developerConnection>scm:git:https://github.com/123/test-parent.git</developerConnection> <!--可以在浏览器访问的scm地址--> <url>https://github.com/123/test-parent.git</url> </scm> <!--持续集成服务器信息--> <ciManagement> <system>jikeins</system> <url>http://jikeins</url> </ciManagement> <!--项目成员团队信息--> <developers> <developer> <id>123</id> <name>123</name> <email>123@gomeholdings.com</email> <timezone>8</timezone> </developer> </developers> <!--贡献者--> <contributors> </contributors> <!--邮件列表--> <mailingLists> <mailingList> </mailingList> </mailingLists> <!--问题追踪系统信息--> <issueManagement> <system>jira</system> <url>http//jira</url> </issueManagement> <!--许可证信息--> <licenses> <license> <name>证书</name> <url>http://shw</url> </license> </licenses> <!--项目报告插件--> <reporting> <plugins> <!--生成javadoc文档--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.0.0</version> </plugin> <!--在站点浏览源码--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jxr-plugin</artifactId> <version>2.5</version> <!--聚合项目源码交叉引用报告--> <configuration> <aggregate>true</aggregate> </configuration> </plugin> <!--编码规范报告--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.0.0</version> </plugin> <!--源代码分析--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <version>3.7</version> <!--聚合项目源码交叉引用报告--> <configuration> <aggregate>true</aggregate> </configuration> </plugin> <!--版本控制系统的变更记录报告--> <!--<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-changelog-plugin</artifactId> <version>2.3</version> <!–最近60天–> <configuration> <type>range</type> <range>60</range> </configuration> </plugin>--> <!--测试覆盖率报告--> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.7</version> </plugin> <!--选择性生成项目信息报告--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> <version>2.1.2</version> <reportSets> <reportSet> <reports> <report>dependencies</report> <report>project-team</report> <report>issue-tracking</report> <report>license</report> </reports> </reportSet> </reportSets> </plugin> </plugins> </reporting> <modules> <module>account-email</module> <module>account-presist</module> <module>account-captcha</module> <module>account-service</module> <module>account-web</module> </modules> <!--maven属性--> <properties> <!--指定maven读取文档和源码编码--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--指定maven用什么编码呈现站点的html文档--> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!--依赖归类--> <org.springframework.version>4.3.10.RELEASE</org.springframework.version> <javax.mail.version>1.4.1</javax.mail.version> <com.icegreen.version>1.3.1b</com.icegreen.version> <testng.version>6.13.1</testng.version> <dom4j.version>1.6.1</dom4j.version> </properties> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>${testng.version}</version> <scope>test</scope> </dependency> </dependencies> <!--项目依赖管理--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>${dom4j.version}</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>${javax.mail.version}</version> </dependency> <dependency> <groupId>com.icegreen</groupId> <artifactId>greenmail</artifactId> <version>${com.icegreen.version}</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> <!--项目发布--> <distributionManagement> <site> <id>site</id> <url>dav:http://localhost:8080/webdav/</url> </site> <repository> <id>distribution</id> <name>release</name> <url>http://127.0.0.1:8081/nexus/content/repositories/releases</url> </repository> </distributionManagement> <!--项目构建配置--> <build> <!--标注项目生成的主构件的名称--> <!--<finalName>web-war</finalName>--> <!--设置主要资源文件过滤(maven属性替换)--> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <!--设置测试资源文件过滤(maven属性替换)--> <testResources> <testResource> <directory>src/test/resources</directory> <filtering>true</filtering> </testResource> </testResources> <!--构建功能扩展--> <extensions> <extension> <groupId>org.apache.maven.wagon</groupId> <artifactId>wagon-webdav-jackrabbit</artifactId> <version>3.0.0</version> </extension> </extensions> <!--插件管理--> <pluginManagement> <plugins> <!--指定编译jdk版本,因为maven的内置阶段绑定,子模块不用显示声明就可以生效--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <!--设置处理资源文件使用的编码,因为maven的内置阶段绑定,子模块不用显示声明就可以生效--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>utf-8</encoding> </configuration> </plugin> <!--项目源码编译打包--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1.1</version> <executions> <execution> <id>生成项目源码jar包</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <!--生成项目站点--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId> <version>3.6</version> <dependencies> <dependency><!-- add support for ssh/scp --> <groupId>org.apache.maven.wagon</groupId> <artifactId>wagon-ssh</artifactId> <version>1.0</version> </dependency> </dependencies> <!--配置当地语言--> <configuration> <locales>zh_CN</locales> </configuration> </plugin> <!--测试插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.5</version> <!--解决编码问题--> <configuration> <forkMode>once</forkMode> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> <!--测试代码生成jar包--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> <!--插件定义--> <plugins> <!--版本管理--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>2.5.3</version> <configuration> <username>123</username> <password>123</password> <!--标签--> <tagBase>https://github.com/123/test-parent.git</tagBase> <!--分支--> <branchBase>https://github.com/123/test-parent.git</branchBase> </configuration> </plugin> </plugins> </build> <!--构建环境配置--> <profiles> <!--开发环境配置--> <profile> <id>dev</id> <properties/> </profile> </profiles> </project>
mvn clean site生成站点
mvn clean site-deploy 生成并发布站点