Search in sources :

Example 11 with PrettyPrintXMLWriter

use of org.codehaus.plexus.util.xml.PrettyPrintXMLWriter 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 12 with PrettyPrintXMLWriter

use of org.codehaus.plexus.util.xml.PrettyPrintXMLWriter in project maven-plugins by apache.

the class AbstractAnalyzeMojo method writeDependencyXML.

private void writeDependencyXML(Set<Artifact> artifacts) {
    if (!artifacts.isEmpty()) {
        getLog().info("Add the following to your pom to correct the missing dependencies: ");
        StringWriter out = new StringWriter();
        PrettyPrintXMLWriter writer = new PrettyPrintXMLWriter(out);
        for (Artifact artifact : artifacts) {
            // called because artifact will set the version to -SNAPSHOT only if I do this. MNG-2961
            artifact.isSnapshot();
            writer.startElement("dependency");
            writer.startElement("groupId");
            writer.writeText(artifact.getGroupId());
            writer.endElement();
            writer.startElement("artifactId");
            writer.writeText(artifact.getArtifactId());
            writer.endElement();
            writer.startElement("version");
            writer.writeText(artifact.getBaseVersion());
            if (!StringUtils.isBlank(artifact.getClassifier())) {
                writer.startElement("classifier");
                writer.writeText(artifact.getClassifier());
                writer.endElement();
            }
            writer.endElement();
            if (!Artifact.SCOPE_COMPILE.equals(artifact.getScope())) {
                writer.startElement("scope");
                writer.writeText(artifact.getScope());
                writer.endElement();
            }
            writer.endElement();
        }
        getLog().info("\n" + out.getBuffer());
    }
}
Also used : StringWriter(java.io.StringWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) Artifact(org.apache.maven.artifact.Artifact)

Example 13 with PrettyPrintXMLWriter

use of org.codehaus.plexus.util.xml.PrettyPrintXMLWriter 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 14 with PrettyPrintXMLWriter

use of org.codehaus.plexus.util.xml.PrettyPrintXMLWriter in project maven-plugins by apache.

the class EffectiveSettingsMojo method execute.

// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
/**
 * {@inheritDoc}
 */
public void execute() throws MojoExecutionException {
    Settings copySettings;
    if (showPasswords) {
        copySettings = settings;
    } else {
        copySettings = copySettings(settings);
        hidePasswords(copySettings);
    }
    StringWriter w = new StringWriter();
    XMLWriter writer = new PrettyPrintXMLWriter(w, StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), copySettings.getModelEncoding(), null);
    writeHeader(writer);
    writeEffectiveSettings(copySettings, writer);
    String effectiveSettings = w.toString();
    if (output != null) {
        try {
            writeXmlFile(output, effectiveSettings, copySettings.getModelEncoding());
        } catch (IOException e) {
            throw new MojoExecutionException("Cannot write effective-settings to output: " + output, e);
        }
        getLog().info("Effective-settings written to: " + output);
    } else {
        StringBuilder message = new StringBuilder();
        message.append(LS).append("Effective user-specific configuration settings:").append(LS).append(LS);
        message.append(effectiveSettings);
        message.append(LS);
        getLog().info(message.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) Settings(org.apache.maven.settings.Settings)

Example 15 with PrettyPrintXMLWriter

use of org.codehaus.plexus.util.xml.PrettyPrintXMLWriter in project maven-plugins by apache.

the class EffectivePomMojo method execute.

// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
/**
 * {@inheritDoc}
 */
public void execute() throws MojoExecutionException {
    if (StringUtils.isNotEmpty(artifact)) {
        project = getMavenProject(artifact);
    }
    StringWriter w = new StringWriter();
    XMLWriter writer = new PrettyPrintXMLWriter(w, StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), project.getModel().getModelEncoding(), null);
    writeHeader(writer);
    String effectivePom;
    if (shouldWriteAllEffectivePOMsInReactor()) {
        // outer root element
        writer.startElement("projects");
        for (MavenProject subProject : projects) {
            writeEffectivePom(subProject, writer);
        }
        writer.endElement();
        effectivePom = w.toString();
        effectivePom = prettyFormat(effectivePom);
    } else {
        writeEffectivePom(project, writer);
        effectivePom = w.toString();
    }
    if (output != null) {
        try {
            writeXmlFile(output, effectivePom, project.getModel().getModelEncoding());
        } catch (IOException e) {
            throw new MojoExecutionException("Cannot write effective-POM to output: " + output, e);
        }
        getLog().info("Effective-POM written to: " + output);
    } else {
        StringBuilder message = new StringBuilder();
        message.append(LS);
        message.append("Effective POMs, after inheritance, interpolation, and profiles are applied:");
        message.append(LS).append(LS);
        message.append(effectivePom);
        message.append(LS);
        getLog().info(message.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) MavenProject(org.apache.maven.project.MavenProject) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter)

Aggregations

PrettyPrintXMLWriter (org.codehaus.plexus.util.xml.PrettyPrintXMLWriter)16 XMLWriter (org.codehaus.plexus.util.xml.XMLWriter)10 StringWriter (java.io.StringWriter)9 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 IOException (java.io.IOException)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 MavenProject (org.apache.maven.project.MavenProject)3 Artifact (org.apache.maven.artifact.Artifact)2 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)2 ProjectBuilder (org.apache.maven.project.ProjectBuilder)2 ProjectBuildingResult (org.apache.maven.project.ProjectBuildingResult)2 BufferedWriter (java.io.BufferedWriter)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1