我正在使用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>
我没有看到plugin
标记,例如&lt; plugins><plugin>......</plugin></plugins>
,但我在上面显示的输出中只得到<plugins>......</plugins>
。我该如何解决这个问题?
答案 0 :(得分: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-model:3.2.5
我希望这会有所帮助。