Search in sources :

Example 6 with MavenXpp3Writer

use of org.apache.maven.model.io.xpp3.MavenXpp3Writer in project maven-plugins by apache.

the class BundlePackMojo method execute.

@SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException {
    readArtifactDataFromUser();
    Artifact artifact = artifactFactory.createProjectArtifact(groupId, artifactId, version);
    try {
        artifactResolver.resolve(artifact, Collections.EMPTY_LIST, localRepository);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Unable to resolve artifact " + artifact.getId(), e);
    } catch (ArtifactNotFoundException e) {
        throw new MojoExecutionException("Artifact " + artifact.getId() + " not found in local repository", e);
    }
    File pom = artifact.getFile();
    File dir = pom.getParentFile();
    Model model = readPom(pom);
    boolean rewrite = false;
    try {
        if (model.getPackaging() == null) {
            model.setPackaging("jar");
            rewrite = true;
        }
        if (model.getName() == null) {
            getLog().info("Project name is missing, please type the project name [" + artifactId + "]:");
            model.setName(inputHandler.readLine());
            if (model.getName() == null) {
                model.setName(artifactId);
            }
            rewrite = true;
        }
        if (model.getDescription() == null) {
            getLog().info("Project description is missing, please type the project description:");
            model.setDescription(inputHandler.readLine());
            rewrite = true;
        }
        if (model.getUrl() == null) {
            getLog().info("Project URL is missing, please type the project URL:");
            model.setUrl(inputHandler.readLine());
            rewrite = true;
        }
        List<License> licenses = model.getLicenses();
        if (licenses.isEmpty()) {
            License license = new License();
            getLog().info("License name is missing, please type the license name:");
            license.setName(inputHandler.readLine());
            getLog().info("License URL is missing, please type the license URL:");
            license.setUrl(inputHandler.readLine());
            licenses.add(license);
            rewrite = true;
        }
        if (disableMaterialization) {
            getLog().warn("Validations to confirm support for project materialization have been DISABLED." + "\n\nYour project may not provide the POM elements necessary to allow " + "users to retrieve sources on-demand," + "\nor to easily checkout your project in an IDE. " + "THIS CAN SERIOUSLY INCONVENIENCE YOUR USERS.\n\nContinue? [y/N]");
            try {
                if ('y' != inputHandler.readLine().toLowerCase().charAt(0)) {
                    disableMaterialization = false;
                }
            } catch (IOException e) {
                getLog().debug("Error reading confirmation: " + e.getMessage(), e);
            }
        }
        if (!disableMaterialization) {
            Scm scm = model.getScm();
            if (scm == null) {
                scm = new Scm();
                model.setScm(scm);
            }
            if (scm.getUrl() == null) {
                if (scmUrl != null) {
                    scm.setUrl(scmUrl);
                } else {
                    getLog().info("SCM view URL is missing, please type the URL for the viewable SCM interface:");
                    scm.setUrl(inputHandler.readLine());
                    rewrite = true;
                }
            }
            if (scm.getConnection() == null) {
                if (scmConnection != null) {
                    scm.setConnection(scmConnection);
                } else {
                    getLog().info("SCM read-only connection URL is missing, please type the read-only SCM URL:");
                    scm.setConnection(inputHandler.readLine());
                    rewrite = true;
                }
            }
        }
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    try {
        if (rewrite) {
            new MavenXpp3Writer().write(WriterFactory.newXmlWriter(pom), model);
        }
        String finalName = null;
        if (model.getBuild() != null) {
            finalName = model.getBuild().getFinalName();
        }
        if (finalName == null) {
            finalName = model.getArtifactId() + "-" + model.getVersion();
        }
        boolean batchMode = settings == null ? false : !settings.isInteractiveMode();
        List<File> files = BundleUtils.selectProjectFiles(dir, inputHandler, finalName, pom, getLog(), batchMode);
        File bundle = new File(basedir, finalName + "-bundle.jar");
        jarArchiver.addFile(pom, POM);
        boolean artifactChecks = !"pom".equals(model.getPackaging());
        boolean sourcesFound = false;
        boolean javadocsFound = false;
        for (File f : files) {
            if (artifactChecks && f.getName().endsWith(finalName + "-sources.jar")) {
                sourcesFound = true;
            } else if (artifactChecks && f.getName().equals(finalName + "-javadoc.jar")) {
                javadocsFound = true;
            }
            jarArchiver.addFile(f, f.getName());
        }
        if (artifactChecks && !sourcesFound) {
            getLog().warn("Sources not included in upload bundle.");
        }
        if (artifactChecks && !javadocsFound) {
            getLog().warn("Javadoc not included in upload bundle.");
        }
        jarArchiver.setDestFile(bundle);
        jarArchiver.createArchive();
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (ArchiverException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) License(org.apache.maven.model.License) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) Model(org.apache.maven.model.Model) Scm(org.apache.maven.model.Scm) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) File(java.io.File) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer)

Example 7 with MavenXpp3Writer

use of org.apache.maven.model.io.xpp3.MavenXpp3Writer in project maven-plugins by apache.

the class SignAndDeployFileMojo method generatePomFile.

/**
     * Generates a minimal POM from the user-supplied artifact information.
     * 
     * @return The path to the generated POM file, never <code>null</code>.
     * @throws MojoExecutionException If the generation failed.
     */
private File generatePomFile() throws MojoExecutionException {
    Model model = generateModel();
    Writer fw = null;
    try {
        File tempFile = File.createTempFile("mvndeploy", ".pom");
        tempFile.deleteOnExit();
        fw = WriterFactory.newXmlWriter(tempFile);
        new MavenXpp3Writer().write(fw, model);
        fw.close();
        fw = null;
        return tempFile;
    } catch (IOException e) {
        throw new MojoExecutionException("Error writing temporary pom file: " + e.getMessage(), e);
    } finally {
        IOUtil.close(fw);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Model(org.apache.maven.model.Model) IOException(java.io.IOException) File(java.io.File) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) Writer(java.io.Writer) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer)

Example 8 with MavenXpp3Writer

use of org.apache.maven.model.io.xpp3.MavenXpp3Writer 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)

Aggregations

MavenXpp3Writer (org.apache.maven.model.io.xpp3.MavenXpp3Writer)8 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)8 IOException (java.io.IOException)7 Model (org.apache.maven.model.Model)7 File (java.io.File)5 StringWriter (java.io.StringWriter)3 Writer (java.io.Writer)3 JarFile (java.util.jar.JarFile)2 Artifact (org.apache.maven.artifact.Artifact)1 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)1 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)1 License (org.apache.maven.model.License)1 Scm (org.apache.maven.model.Scm)1 MavenProject (org.apache.maven.project.MavenProject)1 Settings (org.apache.maven.settings.Settings)1 SettingsXpp3Writer (org.apache.maven.settings.io.xpp3.SettingsXpp3Writer)1 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)1 ExpressionEvaluationException (org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException)1