如何使用Maven PDF插件从Surefire报告生成PDF?

4

运行完JUnit测试后,我使用Maven Surefire Report插件(http://maven.apache.org/plugins/maven-surefire-report-plugin/)生成HTML测试报告。这将生成以下文件:

./target/site/surefire-report.html

我知道有一个Maven PDF插件可以生成PDF文件(http://maven.apache.org/plugins/maven-pdf-plugin/surefire-report.html)。但是我无法让它正常工作。我已将其包含在我的pom.xml文件中:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-pdf-plugin</artifactId>
  <executions>
    <execution>
      <id>pdf</id>
      <phase>test</phase>
      <goals>
        <goal>pdf</goal>
      </goals>
      <configuration>
        <outputDirectory>/tmp</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

当我尝试运行它时,出现了一个错误,指出某个源目录不存在:
  ...
  [mvn] [INFO] [pdf:pdf {execution: default-cli}]
  [mvn] [INFO] ------------------------------------------------------------------------
  [mvn] [INFO] Building VB Common
  [mvn] [INFO]    task-segment: [pdf:pdf]
  [mvn] [INFO] ------------------------------------------------------------------------
  [mvn] [INFO] [pdf:pdf {execution: default-cli}]
  [mvn] [INFO] ------------------------------------------------------------------------
  [mvn] [ERROR] BUILD ERROR
  [mvn] [INFO] ------------------------------------------------------------------------
  [mvn] [INFO] Error during document generation: Source directory doesn't exists (/home/user/myproject/src/site).
  [mvn]
  ...

错误并不奇怪,因为我没有./src/site文件夹。 但是我该如何配置PDF插件,以便将我的HTML报告从./target/site/surefire-report.html作为PDF的源文件呢?

1
如果一个回答对您来说可以,那么请接受它(不管是 Raghuram 的还是我的,但要推广它)。 - Jean-Rémy Revy
2个回答

4
我可以帮你将报告转换为 PDF ,请看以下翻译:

我不会在将我的报告转换为 PDF 的过程中遇到任何问题:

<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">
    <modelVersion>4.0.0</modelVersion>
<build>
    <extensions>
        <!-- Enabling the use of FTP -->
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ftp</artifactId>
            <version>2.2</version>
        </extension>
    </extensions>

    <plugins>
        <!-- Génération d'un PDF à l'identique du site maven -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pdf-plugin</artifactId>
            <executions>
                <execution>
                    <id>pdf</id>
                    <phase>site</phase>
                    <goals>
                        <goal>pdf</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <!-- Gestion des plugins pour ce projet et ses sous modules -->
    <pluginManagement>
        <plugins>
            <!-- Indication de compilation sur la version de Java à utiliser -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>


            <!-- Gestion du tagage des releases -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.2.2</version>
                <configuration>
                    <tagBase>http://svn.xxx.fr.sopra/corprepo/svn/myproject/repository/tags/</tagBase>
                    <scmCommentPrefix>[DEV#GCL]</scmCommentPrefix>
                </configuration>
            </plugin>

            <!-- Gestion de la fabrication du site maven -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <locales>fr</locales>
                    <outputEncoding>UTF-8</outputEncoding>
                </configuration>
            </plugin>
            <!-- Génération d'un PDF à l'identique du site maven -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pdf-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <id>pdf</id>
                        <phase>site</phase>
                        <goals>
                            <goal>pdf</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </pluginManagement>
</build>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- - - - - - - - - - - - - - Reporting & GCL - - - - - - - - - - - - - -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.8.1</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.9.1</version>
            <configuration>
                <configLocation>/src/main/config/checkstyle-sopra.xml</configLocation>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>2.12</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>2.7.1</version>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jxr-plugin</artifactId>
            <version>2.3</version>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-project-info-reports-plugin</artifactId>
            <version>2.4</version>
            <reportSets>
                <reportSet>
                    <reports>
                        <report>summary</report>
                        <report>modules</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>

我的src/main/pdf.xml文件

<document xmlns="http://maven.apache.org/DOCUMENT/1.0.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DOCUMENT/1.0.1 http://maven.apache.org/xsd/document-1.0.1.xsd"
outputName="${project.artifactId}">

     <meta>
      <title>Maven PDF Plugin</title>
      <author>The Apache Maven Project</author>
 </meta>

 <toc name="Table of Contents">
      <item name="Introduction" ref="index.apt"/>

 </toc>

 <cover>
      <coverTitle>${project.name}</coverTitle>
      <coverSubTitle>v. ${project.version}</coverSubTitle>
      <coverType>User Guide</coverType>
      <projectName>${project.name}</projectName>
      <projectLogo>images/logo_myorg.png</projectLogo>
      <companyName>${project.organization.name}</companyName>
      <companyLogo>images/logo_myorg.png</companyLogo>
 </cover>

由于保密政策,我无法提供PDF文件,但它的操作良好:)


1
index.apt的内容应该放在哪里? - spy
五年后,这有点困难,但是看着我当前的工作空间,它是: /src/site/pdf.xml希望这有所帮助。 - Jean-Rémy Revy

3
根据maven pdf插件的文档,该插件需要一个包含PDF DocumentModel的文件以生成PDF。默认情况下,它是src/site/pdf.xml,因此当找不到该文件时,插件将无法正常工作。

该插件旨在生成由maven站点插件生成的报告的pdf版本。因此,它会为项目生成所有报告的pdf版本,其中也包括surefire-report(如果已配置)。


这样的pdf.xml应该长什么样子?我尝试了以下代码:<?xml version="1.0" encoding="UTF-8"?> <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"> <body> <menu name="Surefire Report" href="target/site/surefire-report.html"/> <menu ref="reports"/> </body> </project>但是这会导致NullPointerException。 - Peter
此外,我仍然有一个问题,即使我定义了这样一个pdf.xml文件,它仍然会扫描我的子组件./src/site目录,而这个目录并不存在... - Peter
@Peter。请参考http://maven.apache.org/plugins/maven-pdf-plugin/usage.html获取pdf.xml文件。也许插件源代码库中有更多信息。 - Raghuram
我已经尝试过阅读文档了...但是没有成功。我无法让这个插件运行起来 :-(。人们本应该期望像生成可靠报告的PDF一样的功能能够得到支持,而不需要付出更大的努力吧? - Peter
@Peter。插件网站有一个PDF链接(左侧菜单),可以为他们的网站生成PDF报告:)这就是我建议浏览他们存储库的原因。 - Raghuram

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接