Search in sources :

Example 1 with SettingsXpp3Writer

use of org.apache.maven.settings.io.xpp3.SettingsXpp3Writer in project pom-manipulation-ext by release-engineering.

the class SettingsIO method write.

public void write(Settings settings, File settingsFile) throws ManipulationException {
    try {
        PrintWriter printWriter = new PrintWriter(settingsFile, "UTF-8");
        new SettingsXpp3Writer().write(printWriter, settings);
    } catch (IOException e) {
        throw new ManipulationException("Failed to create repo removal backup settings.xml file.", e, settingsFile, e.getMessage());
    }
}
Also used : ManipulationException(org.commonjava.maven.ext.common.ManipulationException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) SettingsXpp3Writer(org.apache.maven.settings.io.xpp3.SettingsXpp3Writer)

Example 2 with SettingsXpp3Writer

use of org.apache.maven.settings.io.xpp3.SettingsXpp3Writer in project maven-plugins by apache.

the class AbstractInvokerMojo method writeMergedSettingsFile.

private File writeMergedSettingsFile(Settings mergedSettings) throws IOException {
    File mergedSettingsFile;
    mergedSettingsFile = File.createTempFile("invoker-settings", ".xml");
    SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
    FileWriter fileWriter = null;
    try {
        fileWriter = new FileWriter(mergedSettingsFile);
        settingsWriter.write(fileWriter, mergedSettings);
        fileWriter.close();
        fileWriter = null;
    } finally {
        IOUtil.close(fileWriter);
    }
    if (getLog().isDebugEnabled()) {
        getLog().debug("Created temporary file for invoker settings.xml: " + mergedSettingsFile.getAbsolutePath());
    }
    return mergedSettingsFile;
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) SettingsXpp3Writer(org.apache.maven.settings.io.xpp3.SettingsXpp3Writer)

Example 3 with SettingsXpp3Writer

use of org.apache.maven.settings.io.xpp3.SettingsXpp3Writer in project maven-plugins by apache.

the class EffectiveSettingsMojo method writeEffectiveSettings.

/**
 * Method for writing the effective settings informations.
 *
 * @param settings the settings, not null.
 * @param writer the XML writer used, not null.
 * @throws MojoExecutionException if any
 */
private static void writeEffectiveSettings(Settings settings, XMLWriter writer) throws MojoExecutionException {
    cleanSettings(settings);
    String effectiveSettings;
    StringWriter sWriter = new StringWriter();
    SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
    try {
        settingsWriter.write(sWriter, settings);
    } catch (IOException e) {
        throw new MojoExecutionException("Cannot serialize Settings to XML.", e);
    }
    effectiveSettings = addMavenNamespace(sWriter.toString(), false);
    writeComment(writer, "Effective Settings for '" + getUserName() + "' on '" + getHostName() + "'");
    writer.writeMarkup(effectiveSettings);
}
Also used : StringWriter(java.io.StringWriter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) SettingsXpp3Writer(org.apache.maven.settings.io.xpp3.SettingsXpp3Writer)

Example 4 with SettingsXpp3Writer

use of org.apache.maven.settings.io.xpp3.SettingsXpp3Writer in project maven-plugins by apache.

the class EvaluateMojo method handleResponse.

/**
 * @param expr the user expression asked.
 * @param output the file where to write the result, or <code>null</code> to print in standard output.
 * @throws MojoExecutionException if any
 * @throws MojoFailureException if any reflection exceptions occur or missing components.
 */
private void handleResponse(String expr, File output) throws MojoExecutionException, MojoFailureException {
    StringBuilder response = new StringBuilder();
    Object obj;
    try {
        obj = getEvaluator().evaluate(expr);
    } catch (ExpressionEvaluationException e) {
        throw new MojoExecutionException("Error when evaluating the Maven expression", e);
    }
    if (obj != null && expr.equals(obj.toString())) {
        getLog().warn("The Maven expression was invalid. Please use a valid expression.");
        return;
    }
    // handle null
    if (obj == null) {
        response.append("null object or invalid expression");
    } else // handle primitives objects
    if (obj instanceof String) {
        response.append(obj.toString());
    } else if (obj instanceof Boolean) {
        response.append(obj.toString());
    } else if (obj instanceof Byte) {
        response.append(obj.toString());
    } else if (obj instanceof Character) {
        response.append(obj.toString());
    } else if (obj instanceof Double) {
        response.append(obj.toString());
    } else if (obj instanceof Float) {
        response.append(obj.toString());
    } else if (obj instanceof Integer) {
        response.append(obj.toString());
    } else if (obj instanceof Long) {
        response.append(obj.toString());
    } else if (obj instanceof Short) {
        response.append(obj.toString());
    } else // handle specific objects
    if (obj instanceof File) {
        File f = (File) obj;
        response.append(f.getAbsolutePath());
    } else // handle Maven pom object
    if (obj instanceof MavenProject) {
        MavenProject projectAsked = (MavenProject) obj;
        StringWriter sWriter = new StringWriter();
        MavenXpp3Writer pomWriter = new MavenXpp3Writer();
        try {
            pomWriter.write(sWriter, projectAsked.getModel());
        } catch (IOException e) {
            throw new MojoExecutionException("Error when writing pom", e);
        }
        response.append(sWriter.toString());
    } else // handle Maven Settings object
    if (obj instanceof Settings) {
        Settings settingsAsked = (Settings) obj;
        StringWriter sWriter = new StringWriter();
        SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
        try {
            settingsWriter.write(sWriter, settingsAsked);
        } catch (IOException e) {
            throw new MojoExecutionException("Error when writing settings", e);
        }
        response.append(sWriter.toString());
    } else {
        // others Maven objects
        response.append(toXML(expr, obj));
    }
    if (output != null) {
        try {
            writeFile(output, response);
        } catch (IOException e) {
            throw new MojoExecutionException("Cannot write evaluation of expression to output: " + output, e);
        }
        getLog().info("Result of evaluation written to: " + output);
    } else {
        getLog().info(LS + response.toString());
    }
}
Also used : ExpressionEvaluationException(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) MavenProject(org.apache.maven.project.MavenProject) StringWriter(java.io.StringWriter) File(java.io.File) Settings(org.apache.maven.settings.Settings) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) SettingsXpp3Writer(org.apache.maven.settings.io.xpp3.SettingsXpp3Writer)

Example 5 with SettingsXpp3Writer

use of org.apache.maven.settings.io.xpp3.SettingsXpp3Writer in project karaf by apache.

the class MavenConfigurationSupport method updateSettings.

/**
 * Stores changed {@link org.apache.maven.settings.Settings} in new settings.xml file and updates
 * <code>org.ops4j.pax.url.mvn.settings</code> property. Does not update
 * {@link org.osgi.service.cm.ConfigurationAdmin} config.
 */
protected void updateSettings(String prefix, Dictionary<String, Object> config) throws IOException {
    File dataDir = context.getDataFile(".");
    if (!dataDir.isDirectory()) {
        throw new RuntimeException("Can't access data directory for " + context.getBundle().getSymbolicName() + " bundle");
    }
    File newSettingsFile = nextSequenceFile(dataDir, RE_SETTINGS, PATTERN_SETTINGS);
    config.put(prefix + PROPERTY_SETTINGS_FILE, newSettingsFile.getCanonicalPath());
    try (FileWriter fw = new FileWriter(newSettingsFile)) {
        new SettingsXpp3Writer().write(fw, mavenSettings);
    }
    System.out.println("New settings stored in \"" + newSettingsFile.getCanonicalPath() + "\"");
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) SettingsXpp3Writer(org.apache.maven.settings.io.xpp3.SettingsXpp3Writer)

Aggregations

SettingsXpp3Writer (org.apache.maven.settings.io.xpp3.SettingsXpp3Writer)5 File (java.io.File)3 IOException (java.io.IOException)3 FileWriter (java.io.FileWriter)2 StringWriter (java.io.StringWriter)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 PrintWriter (java.io.PrintWriter)1 MavenXpp3Writer (org.apache.maven.model.io.xpp3.MavenXpp3Writer)1 MavenProject (org.apache.maven.project.MavenProject)1 Settings (org.apache.maven.settings.Settings)1 ExpressionEvaluationException (org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException)1 ManipulationException (org.commonjava.maven.ext.common.ManipulationException)1