use of org.codehaus.plexus.util.xml.XMLWriter in project che by eclipse.
the class EffectivePomWriter method getEffectivePom.
public static String getEffectivePom(MavenServerImpl server, final File pom, List<String> activeProfiles, List<String> inactiveProfiles) {
StringWriter stringWriter = new StringWriter();
try {
MavenExecutionRequest request = server.newMavenRequest(pom, activeProfiles, inactiveProfiles, Collections.emptyList());
server.runMavenRequest(request, () -> {
try {
ProjectBuilder builder = server.getMavenComponent(ProjectBuilder.class);
ProjectBuildingResult projectBuildingResult = builder.build(new File(pom.getPath()), request.getProjectBuildingRequest());
MavenProject project = projectBuildingResult.getProject();
XMLWriter writer = new PrettyPrintXMLWriter(stringWriter, " ");
writeHeader(writer);
writeEffectivePom(project, writer);
} catch (ProjectBuildingException | MojoExecutionException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
});
} catch (Exception e) {
return null;
}
return stringWriter.toString();
}
use of org.codehaus.plexus.util.xml.XMLWriter in project intellij-community by JetBrains.
the class MavenEffectivePomDumper method evaluateEffectivePom.
// See org.apache.maven.plugins.help.EffectivePomMojo#execute from maven-help-plugin
@Nullable
public static String evaluateEffectivePom(final Maven3ServerEmbedder embedder, @NotNull final File file, @NotNull List<String> activeProfiles, @NotNull List<String> inactiveProfiles) throws RemoteException, MavenServerProcessCanceledException {
final StringWriter w = new StringWriter();
try {
final MavenExecutionRequest request = embedder.createRequest(file, activeProfiles, inactiveProfiles, Collections.<String>emptyList());
embedder.executeWithMavenSession(request, new Runnable() {
@Override
public void run() {
try {
// copied from DefaultMavenProjectBuilder.buildWithDependencies
ProjectBuilder builder = embedder.getComponent(ProjectBuilder.class);
ProjectBuildingResult buildingResult = builder.build(new File(file.getPath()), request.getProjectBuildingRequest());
MavenProject project = buildingResult.getProject();
XMLWriter writer = new PrettyPrintXMLWriter(new PrintWriter(w), StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), "\n", null, null);
writeHeader(writer);
writeEffectivePom(project, writer);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
} catch (Exception e) {
return null;
}
return w.toString();
}
use of org.codehaus.plexus.util.xml.XMLWriter in project maven-plugins by apache.
the class AntBuildWriter method writeGeneratedBuildXml.
/**
* Generate an <code>maven-build.xml</code>
*
* @throws IOException
* @see #DEFAULT_MAVEN_BUILD_FILENAME
*/
private void writeGeneratedBuildXml() throws IOException {
// TODO: parameter
File outputFile = new File(project.getBasedir(), DEFAULT_MAVEN_BUILD_FILENAME);
String encoding = "UTF-8";
OutputStreamWriter w = new OutputStreamWriter(new FileOutputStream(outputFile), encoding);
XMLWriter writer = new PrettyPrintXMLWriter(w, StringUtils.repeat(" ", DEFAULT_INDENTATION_SIZE), encoding, null);
// ----------------------------------------------------------------------
// <!-- comments -->
// ----------------------------------------------------------------------
AntBuildWriterUtil.writeHeader(writer);
// ----------------------------------------------------------------------
// <project/>
// ----------------------------------------------------------------------
writer.startElement("project");
writer.addAttribute("name", project.getArtifactId() + "-from-maven");
writer.addAttribute("default", "package");
writer.addAttribute("basedir", ".");
XmlWriterUtil.writeLineBreak(writer);
// ----------------------------------------------------------------------
// <property/>
// ----------------------------------------------------------------------
writeProperties(writer);
// ----------------------------------------------------------------------
// <path/>
// ----------------------------------------------------------------------
writeBuildPathDefinition(writer);
// ----------------------------------------------------------------------
// <target name="clean" />
// ----------------------------------------------------------------------
writeCleanTarget(writer);
// ----------------------------------------------------------------------
// <target name="compile" />
// ----------------------------------------------------------------------
List compileSourceRoots = AntBuildWriterUtil.removeEmptyCompileSourceRoots(project.getCompileSourceRoots());
writeCompileTarget(writer, compileSourceRoots);
// ----------------------------------------------------------------------
// <target name="compile-tests" />
// ----------------------------------------------------------------------
List testCompileSourceRoots = AntBuildWriterUtil.removeEmptyCompileSourceRoots(project.getTestCompileSourceRoots());
writeCompileTestsTarget(writer, testCompileSourceRoots);
// ----------------------------------------------------------------------
// <target name="test" />
// ----------------------------------------------------------------------
writeTestTargets(writer, testCompileSourceRoots);
// ----------------------------------------------------------------------
// <target name="javadoc" />
// ----------------------------------------------------------------------
writeJavadocTarget(writer);
// ----------------------------------------------------------------------
// <target name="package" />
// ----------------------------------------------------------------------
writePackageTarget(writer);
// ----------------------------------------------------------------------
// <target name="get-deps" />
// ----------------------------------------------------------------------
writeGetDepsTarget(writer);
XmlWriterUtil.writeLineBreak(writer);
// project
writer.endElement();
XmlWriterUtil.writeLineBreak(writer);
w.close();
}
use of org.codehaus.plexus.util.xml.XMLWriter in project maven-plugins by apache.
the class DoapUtilTest method testWriteElement.
/**
* Test method for {@link DoapUtil#writeElement(XMLWriter, String, String, String)}.
*
* @throws Exception if any
*/
public void testWriteElement() throws Exception {
StringWriter w = new StringWriter();
XMLWriter writer = new PrettyPrintXMLWriter(w);
DoapUtil.writeElement(writer, null, "name", "value");
w.close();
assertEquals(w.toString(), "<name>value</name>");
w = new StringWriter();
writer = new PrettyPrintXMLWriter(w);
try {
DoapUtil.writeElement(writer, null, null, null);
assertTrue("Null not catched", false);
} catch (IllegalArgumentException e) {
assertTrue("IllegalArgumentException catched", true);
} finally {
w.close();
}
}
use of org.codehaus.plexus.util.xml.XMLWriter in project maven-plugins by apache.
the class ApplicationXmlWriter method write.
public void write(ApplicationXmlWriterContext context) throws EarPluginException {
Writer w = initializeWriter(context.getDestinationFile());
XMLWriter writer = null;
if (JavaEEVersion.ONE_DOT_THREE.eq(version)) {
writer = initializeRootElementOneDotThree(w);
} else if (JavaEEVersion.ONE_DOT_FOUR.eq(version)) {
writer = initializeRootElementOneDotFour(w);
} else if (JavaEEVersion.FIVE.eq(version)) {
writer = initializeRootElementFive(w);
} else if (JavaEEVersion.SIX.eq(version)) {
writer = initializeRootElementSix(w);
} else if (JavaEEVersion.SEVEN.eq(version)) {
writer = initializeRootElementSeven(w);
}
// writer is still on root element, so we can still add this attribute
if (context.getApplicationId() != null) {
writer.addAttribute("id", context.getApplicationId());
}
// As from JavaEE6
if (version.ge(JavaEEVersion.SIX)) {
writeApplicationName(context.getApplicationName(), writer);
}
// reversed between J2EE 1.3 and J2EE 1.4.
if (version.eq(JavaEEVersion.ONE_DOT_THREE)) {
writeDisplayName(context.getDisplayName(), writer);
writeDescription(context.getDescription(), writer);
} else {
writeDescription(context.getDescription(), writer);
writeDisplayName(context.getDisplayName(), writer);
}
// As from JavaEE6
if (version.ge(JavaEEVersion.SIX)) {
writeInitializeInOrder(context.getInitializeInOrder(), writer);
}
// Do not change this unless you really know what you're doing :)
for (EarModule module : context.getEarModules()) {
module.appendModule(writer, version.getVersion(), generateModuleId);
}
for (SecurityRole securityRole : context.getSecurityRoles()) {
securityRole.appendSecurityRole(writer);
}
if (version.ge(JavaEEVersion.FIVE)) {
writeLibraryDirectory(context.getLibraryDirectory(), writer);
}
if (version.ge(JavaEEVersion.SIX)) {
for (EnvEntry envEntry : context.getEnvEntries()) {
envEntry.appendEnvEntry(writer);
}
for (EjbRef ejbEntry : context.getEjbEntries()) {
ejbEntry.appendEjbRefEntry(writer);
}
for (ResourceRef resourceEntry : context.getResourceRefs()) {
resourceEntry.appendResourceRefEntry(writer);
}
}
writer.endElement();
close(w);
}
Aggregations