Example usage for org.apache.maven.plugin MojoExecutionException MojoExecutionException

List of usage examples for org.apache.maven.plugin MojoExecutionException MojoExecutionException

Introduction

In this page you can find the example usage for org.apache.maven.plugin MojoExecutionException MojoExecutionException.

Prototype

public MojoExecutionException(String message, Throwable cause) 

Source Link

Document

Construct a new MojoExecutionException exception wrapping an underlying Throwable and providing a message.

Usage

From source file:com.azurenight.maven.TroposphereMojo.java

License:Apache License

private Collection<File> extractAllFiles(File outputDirectory, ZipFile ja, Enumeration<JarEntry> en)
        throws MojoExecutionException {
    List<File> files = new ArrayList<File>();
    while (en.hasMoreElements()) {
        JarEntry el = en.nextElement();
        if (!el.isDirectory()) {
            File destFile = new File(outputDirectory, el.getName());
            if (OVERRIDE || !destFile.exists()) {
                destFile.getParentFile().mkdirs();
                try {
                    FileOutputStream fo = new FileOutputStream(destFile);
                    IOUtils.copy(ja.getInputStream(el), fo);
                    fo.close();/*from w  w w .j a va 2  s .  c om*/
                } catch (IOException e) {
                    throw new MojoExecutionException(
                            "extracting " + el.getName() + " from jython artifact jar failed", e);
                }
            }
            files.add(destFile);
        }
    }
    return files;
}

From source file:com.azurenight.maven.TroposphereMojo.java

License:Apache License

private List<String> getEasyInstallArgs(String easy_install_script) throws MojoExecutionException {
    List<String> args = new ArrayList<String>();

    // I want to launch
    args.add("java");
    // to run the generated jython installation here
    args.add("-cp");
    args.add("." + getClassPathSeparator() + "Lib");
    // which should know about itself
    args.add("-Dpython.home=.");
    //args.add("-Dpython.verbose=debug");
    File jythonFakeExecutable = new File(temporaryBuildDirectory, "jython");
    try {//from w  w w.  j  a v a2 s.co  m
        jythonFakeExecutable.createNewFile();
    } catch (IOException e) {
        throw new MojoExecutionException("couldn't create file", e);
    }
    args.add("-Dpython.executable=" + jythonFakeExecutable.getName());
    args.add("org.python.util.jython");
    args.add(easy_install_script);
    args.add("--always-unzip");
    args.add("--upgrade");
    args.add("--verbose");
    args.add("--build-directory");
    args.add(packageDownloadCacheDir.getAbsolutePath());
    // and install these libraries
    args.addAll(libs);

    return args;
}

From source file:com.azurenight.maven.TroposphereMojo.java

License:Apache License

public void runJythonScriptOnInstall(File outputDirectory, List<String> args, File outputFile)
        throws MojoExecutionException {
    getLog().info("running " + args + " in " + outputDirectory);
    ProcessBuilder pb = new ProcessBuilder(args);
    pb.directory(outputDirectory);/* ww  w  . j av a2s .c o  m*/
    pb.environment().put("BASEDIR", project.getBasedir().getAbsolutePath());
    final Process p;
    ByteArrayOutputStream stdoutBaos = null;
    ByteArrayOutputStream stderrBaos = null;
    try {
        p = pb.start();
    } catch (IOException e) {
        throw new MojoExecutionException("Executing jython failed. tried to run: " + pb.command(), e);
    }
    if (outputFile == null) {
        stdoutBaos = new ByteArrayOutputStream();
        copyIO(p.getInputStream(), stdoutBaos);
    } else {
        try {
            copyIO(p.getInputStream(), new FileOutputStream(outputFile));
        } catch (FileNotFoundException e) {
            throw new MojoExecutionException("Failed to copy output to : " + outputFile.getAbsolutePath(), e);
        }
    }
    stderrBaos = new ByteArrayOutputStream();
    copyIO(p.getErrorStream(), stderrBaos);
    copyIO(System.in, p.getOutputStream());
    try {
        boolean error = false;
        if (p.waitFor() != 0) {
            error = true;
        }
        if (getLog().isDebugEnabled() && stdoutBaos != null) {
            getLog().debug(stdoutBaos.toString());
        }
        if (getLog().isErrorEnabled() && stderrBaos != null) {
            getLog().error(stderrBaos.toString());
        }
        if (error) {
            throw new MojoExecutionException("Jython failed with return code: " + p.exitValue());
        }
    } catch (InterruptedException e) {
        throw new MojoExecutionException("Python tests were interrupted", e);
    }

}

From source file:com.baidu.jprotobuf.mojo.PreCompileMojo.java

License:Apache License

/**
 * Add any relevant project dependencies to the classpath. Indirectly takes includePluginDependencies and
 * ExecutableDependency into consideration.
 * /*w ww  .java  2s.c o  m*/
 * @param path classpath of {@link java.net.URL} objects
 * @throws MojoExecutionException if a problem happens
 */
private void addRelevantPluginDependenciesToClasspath(List<URL> path) throws MojoExecutionException {
    if (hasCommandlineArgs()) {
        arguments = parseCommandlineArgs();
    }

    try {
        for (Artifact classPathElement : this.determineRelevantPluginDependencies()) {
            getLog().debug(
                    "Adding plugin dependency artifact: " + classPathElement.getArtifactId() + " to classpath");
            path.add(classPathElement.getFile().toURI().toURL());
        }
    } catch (MalformedURLException e) {
        throw new MojoExecutionException("Error during setting up classpath", e);
    }

}

From source file:com.baidu.jprotobuf.mojo.PreCompileMojo.java

License:Apache License

/**
 * Add any relevant project dependencies to the classpath. Takes includeProjectDependencies into consideration.
 * //from w w  w  .j  a va 2s  .co m
 * @param path classpath of {@link java.net.URL} objects
 * @throws MojoExecutionException if a problem happens
 */
private void addRelevantProjectDependenciesToClasspath(List<URL> path) throws MojoExecutionException {
    if (this.includeProjectDependencies) {
        try {
            getLog().debug("Project Dependencies will be included.");

            List<Artifact> artifacts = new ArrayList<Artifact>();
            List<File> theClasspathFiles = new ArrayList<File>();

            collectProjectArtifactsAndClasspath(artifacts, theClasspathFiles);

            for (File classpathFile : theClasspathFiles) {
                URL url = classpathFile.toURI().toURL();
                getLog().debug("Adding to classpath : " + url);
                path.add(url);
            }

            for (Artifact classPathElement : artifacts) {
                getLog().debug("Adding project dependency artifact: " + classPathElement.getArtifactId()
                        + " to classpath");
                path.add(classPathElement.getFile().toURI().toURL());
            }

        } catch (MalformedURLException e) {
            throw new MojoExecutionException("Error during setting up classpath", e);
        }
    } else {
        getLog().debug("Project Dependencies will be excluded.");
    }

}

From source file:com.baidu.jprotobuf.mojo.PreCompileMojo.java

License:Apache License

/**
 * Resolve the executable dependencies for the specified project
 * /*from  w w  w.  j  a v  a2s .  co m*/
 * @param executablePomArtifact the project's POM
 * @return a set of Artifacts
 * @throws MojoExecutionException if a failure happens
 */
private Set<Artifact> resolveExecutableDependencies(Artifact executablePomArtifact)
        throws MojoExecutionException {

    Set<Artifact> executableDependencies;
    try {
        MavenProject executableProject = this.projectBuilder.buildFromRepository(executablePomArtifact,
                this.remoteRepositories, this.localRepository);

        // get all of the dependencies for the executable project
        List<Dependency> dependencies = executableProject.getDependencies();

        // make Artifacts of all the dependencies
        Set<Artifact> dependencyArtifacts = MavenMetadataSource.createArtifacts(this.artifactFactory,
                dependencies, null, null, null);

        // not forgetting the Artifact of the project itself
        dependencyArtifacts.add(executableProject.getArtifact());

        // resolve all dependencies transitively to obtain a comprehensive list of assemblies
        ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts,
                executablePomArtifact, Collections.emptyMap(), this.localRepository, this.remoteRepositories,
                metadataSource, null, Collections.emptyList());
        executableDependencies = result.getArtifacts();
    } catch (Exception ex) {
        throw new MojoExecutionException("Encountered problems resolving dependencies of the executable "
                + "in preparation for its execution.", ex);
    }

    return executableDependencies;
}

From source file:com.baizhitong.util.MojoSample.java

License:Apache License

public void execute() throws MojoExecutionException {
    //       System.out.println(this.getPluginContext());

    File f = outputDirectory;/* w w w .ja  v a 2  s.c  o m*/

    getLog().info("Hello world :" + greeting + ",:" + outputDirectory.getAbsolutePath()
            + ",??:" + sourceDirectory + ",baseDirectory:" + baseDirectory.getAbsolutePath()
            + ",srcDirectory:" + srcDirectory);

    if (!f.exists()) {
        f.mkdirs();
    }

    File touch = new File(f, "touch.txt");

    FileWriter w = null;
    try {
        w = new FileWriter(touch);

        w.write("touch.txt");
    } catch (IOException e) {
        throw new MojoExecutionException("Error creating file " + touch, e);
    } finally {
        if (w != null) {
            try {
                w.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

From source file:com.basistech.bbhmp.RosapiBundleCollectorMojo.java

License:Open Source License

private boolean isJarFragment(File outputFile) throws MojoExecutionException, MojoFailureException {
    JarFile jar;/*from  w ww .ja  va 2 s  . co  m*/
    try {
        jar = new JarFile(outputFile);
    } catch (IOException e) {
        throw new MojoExecutionException(
                "Failed to open dependency we just copied " + outputFile.getAbsolutePath(), e);
    }
    final Manifest manifest;
    try {
        manifest = jar.getManifest();
    } catch (IOException e) {
        throw new MojoFailureException(
                "Failed to read manifest from dependency we just copied " + outputFile.getAbsolutePath(), e);
    }
    final Attributes mattr = manifest.getMainAttributes();
    // getValue is case-insensitive.
    String mfVersion = mattr.getValue("Bundle-ManifestVersion");
    /*
     * '2' is the only legitimate bundle manifest version. Version 1 is long obsolete, and not supported
     * in current containers. There's no plan on the horizon for a version 3. No version at all indicates
     * that the jar file is not an OSGi bundle at all.
     */
    if (!"2".equals(mfVersion)) {
        throw new MojoFailureException("Bundle-ManifestVersion is not '2' from dependency we just copied "
                + outputFile.getAbsolutePath());
    }
    String host = mattr.getValue("Fragment-Host");
    return host != null;
}

From source file:com.basistech.bbhmp.RosapiBundleCollectorMojo.java

License:Open Source License

private void writeMetadata(Map<Integer, List<BundleSpec>> bundlesByLevel) throws MojoExecutionException {
    OutputStream os = null;/*  www.j  a  v a 2  s.  co m*/
    File md = new File(outputDirectory, "bundles.xml");

    try {
        os = new FileOutputStream(md);
        XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(os);
        writer = new IndentingXMLStreamWriter(writer);
        writer.writeStartDocument("utf-8", "1.0");
        writer.writeStartElement("bundles");
        for (Integer level : bundlesByLevel.keySet()) {
            writer.writeStartElement("level");
            writer.writeAttribute("level", Integer.toString(level));
            for (BundleSpec spec : bundlesByLevel.get(level)) {
                writer.writeStartElement("bundle");
                writer.writeAttribute("start", Boolean.toString(spec.start));
                writer.writeCharacters(spec.filename);
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();

    } catch (IOException | XMLStreamException e) {
        throw new MojoExecutionException("Failed to write metadata file " + md.toString(), e);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:com.basistech.bbhmp.RosapiBundleCollectorMojo.java

License:Open Source License

private Artifact getArtifact(BundleInfo bundle) throws MojoExecutionException, MojoFailureException {
    Artifact artifact;/*from w ww . ja  v  a2  s .c  o m*/

    /*
     * Anything in the gav may be interpolated.
     * Version may be "-dependency-" to look for the artifact as a dependency.
     */

    String gav = interpolator.interpolate(bundle.gav);
    String[] pieces = gav.split("/");
    String groupId = pieces[0];
    String artifactId = pieces[1];
    String versionStr;
    String classifier = null;
    if (pieces.length == 3) {
        versionStr = pieces[2];
    } else {
        classifier = pieces[2];
        versionStr = pieces[3];
    }

    if ("-dependency-".equals(versionStr)) {
        versionStr = getArtifactVersionFromDependencies(groupId, artifactId);
        if (versionStr == null) {
            throw new MojoFailureException(String.format(
                    "Request for %s:%s as a dependency, but it is not a dependency", groupId, artifactId));
        }
    }

    VersionRange vr;
    try {
        vr = VersionRange.createFromVersionSpec(versionStr);
    } catch (InvalidVersionSpecificationException e1) {
        throw new MojoExecutionException("Bad version range " + versionStr, e1);
    }

    artifact = factory.createDependencyArtifact(groupId, artifactId, vr, "jar", classifier,
            Artifact.SCOPE_COMPILE);

    // Maven 3 will search the reactor for the artifact but Maven 2 does not
    // to keep consistent behaviour, we search the reactor ourselves.
    Artifact result = getArtifactFomReactor(artifact);
    if (result != null) {
        return result;
    }

    try {
        resolver.resolve(artifact, remoteRepos, local);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Unable to resolve artifact.", e);
    } catch (ArtifactNotFoundException e) {
        throw new MojoExecutionException("Unable to find artifact.", e);
    }

    return artifact;
}