当我们开发了一个公共模块,将它deploy到Maven仓库时,最好同时提供源码包和Javadoc包,这样使用者可以在IDE中直接进入该依赖的源码查看其实现的细节。
由于生成源码包和Javadoc包是极其常见的需求,因此Maven官方提供了插件来帮助用户完成这个任务 ,配置如下:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-javadoc-plugin.version>2.10.4</maven-javadoc-plugin.version>
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<testSource>${java.version}</testSource>
<testTarget>${java.version}</testTarget>
</configuration>
</plugin>
<!--配置生成Javadoc包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<encoding>UTF-8</encoding>
<aggregate>true</aggregate>
<charset>UTF-8</charset>
<docencoding>UTF-8</docencoding>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!--配置生成源码包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source-plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在使用IDEA生成Java Doc的过程中,可能会出现IDEA控制台输出中文乱码,即使我们在maven-javadoc-plugin插件中指定字符集为UTF-8,依然是乱码,这是因为Maven的默认平台编码是GBK。
解决办法:
在IDEA中,打开File | Settings | Build, Execution, Deployment | Build Tools | Maven | Runner在VM Options中添加-Dfile.encoding=GBK,切记一定是GBK。
参考
Maven实战(九)——打包的技巧:http://www.infoq.com/cn/news/2011/06/xxb-maven-9-package
原文地址: http://blog.csdn.net/top_code/article/details/53586551