为 pom.xml 构建一个 XML 标签

Constructing an XML tag for pom.xml

我正在使用 org.apache.maven.model.Reporting class 创建 Maven <reporting> 标签。我正在尝试构建一个像

这样的标签

预期输出标签

<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>            
        </plugin>
    </plugins>
</reporting>

我使用的代码是,

ReportPlugin reportPlugin = new ReportPlugin();
reportPlugin.setGroupId("org.codehaus.mojo");
reportPlugin.setArtifactId("cobertura-maven-plugin");
Reporting reporting = new Reporting();
reporting.addPlugin(reportPlugin);

但是我得到的输出标签是,

<reporting>
        <excludeDefaults>false</excludeDefaults>
        <plugins>
            <artifactId>cobertura-maven-plugin</artifactId>                
            <groupId>org.codehaus.mojo</groupId>
        </plugins>
</reporting>

我没有看到像 <plugins><plugin>......</plugin></plugins> 这样的 plugin 标签,但我在上面显示的输出中只得到一个 <plugins>......</plugins>。我该如何解决这个问题?

我试着看看我是否得到了相同的结果。 我使用的代码如下:

    Model model = new Model();
    ReportPlugin reportPlugin = new ReportPlugin();
    reportPlugin.setGroupId("org.codehaus.mojo");
    reportPlugin.setArtifactId("cobertura-maven-plugin");
    Reporting reporting = new Reporting();
    reporting.addPlugin(reportPlugin);
    model.setReporting(reporting);
    
    StringWriter writer = new StringWriter();
    MavenXpp3Writer xpp = new MavenXpp3Writer();
    try {
        xpp.write(writer, model);
        System.out.println(writer.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }

此代码产生以下输出:

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </reporting>
</project>

我使用的是Eclipse Luna的嵌入式Maven版本(3.2.1/1.5.1.20150109-1819)。 我使用的 POM 具有以下依赖项:

maven-reporting-api: 3.0-alpha-2

maven-model: 3.2.5

希望对您有所帮助。