Search in sources :

Example 11 with XMLWriter

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();
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) JDOMException(org.jdom.JDOMException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) StringWriter(java.io.StringWriter) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) File(java.io.File)

Example 12 with XMLWriter

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();
}
Also used : MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) RemoteException(java.rmi.RemoteException) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with XMLWriter

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();
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ArrayList(java.util.ArrayList) List(java.util.List) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) File(java.io.File) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter)

Example 14 with XMLWriter

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();
    }
}
Also used : StringWriter(java.io.StringWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter)

Example 15 with XMLWriter

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);
}
Also used : XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) Writer(java.io.Writer)

Aggregations

XMLWriter (org.codehaus.plexus.util.xml.XMLWriter)20 PrettyPrintXMLWriter (org.codehaus.plexus.util.xml.PrettyPrintXMLWriter)13 StringWriter (java.io.StringWriter)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 IOException (java.io.IOException)4 File (java.io.File)3 MavenProject (org.apache.maven.project.MavenProject)3 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)2 ProjectBuilder (org.apache.maven.project.ProjectBuilder)2 ProjectBuildingResult (org.apache.maven.project.ProjectBuildingResult)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 RemoteException (java.rmi.RemoteException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DecorationModel (org.apache.maven.doxia.site.decoration.DecorationModel)1 Contributor (org.apache.maven.model.Contributor)1