【问题标题】:Constructing an XML tag for pom.xml为 pom.xml 构造一个 XML 标记
【发布时间】:2015-03-19 17:12:11
【问题描述】:

我正在使用 org.apache.maven.model.Reporting 类创建一个 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 标记,但在上面显示的输出中我只看到了&lt;plugins&gt;......&lt;/plugins&gt;。我该如何解决这个问题?

【问题讨论】:

    标签: java xml maven xml-parsing pom.xml


    【解决方案1】:

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

        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 模型:3.2.5

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 1970-01-01
      • 2018-09-18
      • 2018-09-04
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      相关资源
      最近更新 更多