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.akathist.maven.plugins.launch4j.Launch4jMojo.java

License:Open Source License

/**
 * Downloads the platform-specific parts, if necessary.
 *//* w w w. ja  va2s .c o  m*/
private void retrieveBinaryBits(Artifact a) throws MojoExecutionException {
    try {
        resolver.resolve(a, project.getRemoteArtifactRepositories(), localRepository);

    } catch (ArtifactNotFoundException e) {
        throw new MojoExecutionException("Can't find platform-specific components", e);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Can't retrieve platform-specific components", e);
    }
}

From source file:com.alexnederlof.jasperreport.JasperReporter.java

License:Apache License

private void executeTasks(List<CompileTask> tasks) throws MojoExecutionException {
    try {//  www. ja  va2s  .  c o  m
        long t1 = System.currentTimeMillis();
        List<Future<Void>> output = Executors.newFixedThreadPool(numberOfThreads).invokeAll(tasks);
        long time = (System.currentTimeMillis() - t1);
        getLog().info("Generated " + output.size() + " jasper reports in " + (time / 1000.0) + " seconds");
        checkForExceptions(output);
    } catch (InterruptedException e) {
        log.error("Failed to compile Japser reports: Interrupted!", e);
        throw new MojoExecutionException("Error while compiling Jasper reports", e);
    } catch (ExecutionException e) {
        if (e.getCause() instanceof JRException) {
            throw new MojoExecutionException(ERROR_JRE_COMPILE_ERROR, e);
        } else {
            throw new MojoExecutionException("Error while compiling Jasper reports", e);
        }
    }
}

From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.ConfigureWorkspaceMojo.java

License:Open Source License

public void execute() throws MojoExecutionException {
    WorkspaceConfiguration config = new WorkspaceConfiguration();
    config.setWorkspaceDirectory(new File(this.getWorkspace()));
    config.setLocalRepository(this.getLocalRepository());

    if (this.workspaceCodeStylesURL != null) {
        try {/*from  w  ww.  j  a  v  a2 s.  com*/
            config.setCodeStylesURL(new URL(workspaceCodeStylesURL));
        } catch (MalformedURLException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }

        config.setActiveStyleProfileName(workspaceActiveCodeStyleProfileName);
    }

    new EclipseWorkspaceWriter().init(this.getLog(), config).write();
}

From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.EclipseToMavenMojo.java

License:Open Source License

/**
 * Get a {@link EclipseOsgiPlugin} object from a plugin jar/dir found in the target dir.
 *
 * @param file plugin jar or dir//from  ww w  . j  av a2s .co  m
 * @throws MojoExecutionException if anything bad happens while parsing files
 */
private EclipseOsgiPlugin getEclipsePlugin(File file) throws MojoExecutionException {
    if (file.isDirectory()) {
        return new ExplodedPlugin(file);
    } else if (file.getName().endsWith(".jar")) //$NON-NLS-1$
    {
        try {
            return new PackagedPlugin(file);
        } catch (IOException e) {
            throw new MojoExecutionException(
                    Messages.getString("EclipseToMavenMojo.unabletoaccessjar", file.getAbsolutePath()), e); //$NON-NLS-1$
        }
    }

    return null;
}

From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.EclipseToMavenMojo.java

License:Open Source License

/**
 * Create the {@link Model} from a plugin manifest
 *
 * @param plugin Eclipse plugin jar or dir
 * @throws MojoExecutionException if anything bad happens while parsing files
 *//*from   w ww . j  a  v a 2  s .c  o m*/
private Model createModel(EclipseOsgiPlugin plugin) throws MojoExecutionException {

    String name, bundleName, version, groupId, artifactId, requireBundle;

    try {
        if (!plugin.hasManifest()) {
            getLog().warn(Messages.getString("EclipseToMavenMojo.plugindoesnothavemanifest", plugin)); //$NON-NLS-1$
            return null;
        }

        Analyzer analyzer = new Analyzer();

        Map bundleSymbolicNameHeader = analyzer
                .parseHeader(plugin.getManifestAttribute(Analyzer.BUNDLE_SYMBOLICNAME));
        bundleName = (String) bundleSymbolicNameHeader.keySet().iterator().next();
        version = plugin.getManifestAttribute(Analyzer.BUNDLE_VERSION);

        if (bundleName == null || version == null) {
            getLog().error(Messages.getString("EclipseToMavenMojo.unabletoreadbundlefrommanifest")); //$NON-NLS-1$
            return null;
        }

        version = osgiVersionToMavenVersion(version);

        name = plugin.getManifestAttribute(Analyzer.BUNDLE_NAME);

        requireBundle = plugin.getManifestAttribute(Analyzer.REQUIRE_BUNDLE);
    } catch (IOException e) {
        throw new MojoExecutionException(Messages.getString("EclipseToMavenMojo.errorprocessingplugin", plugin), //$NON-NLS-1$
                e);
    }

    Dependency[] deps = parseDependencies(requireBundle);

    groupId = createGroupId(bundleName);
    artifactId = createArtifactId(bundleName);

    Model model = new Model();
    model.setModelVersion("4.0.0"); //$NON-NLS-1$
    model.setGroupId(groupId);
    model.setArtifactId(artifactId);
    model.setName(name);
    model.setVersion(version);

    model.setProperties(plugin.getPomProperties());

    if (groupId.startsWith("org.eclipse")) //$NON-NLS-1$
    {
        // why do we need a parent?

        // Parent parent = new Parent();
        // parent.setGroupId( "org.eclipse" );
        // parent.setArtifactId( "eclipse" );
        // parent.setVersion( "1" );
        // model.setParent( parent );

        // infer license for know projects, everything at eclipse is licensed under EPL
        // maybe too simplicistic, but better than nothing
        License license = new License();
        license.setName("Eclipse Public License - v 1.0"); //$NON-NLS-1$
        license.setUrl("http://www.eclipse.org/org/documents/epl-v10.html"); //$NON-NLS-1$
        model.addLicense(license);
    }

    if (deps.length > 0) {
        for (int k = 0; k < deps.length; k++) {
            model.getDependencies().add(deps[k]);
        }
    }

    return model;
}

From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.EclipseToMavenMojo.java

License:Open Source License

/**
 * Writes the artifact to the repo/* w  ww . ja  v  a2  s  .  c om*/
 *
 * @param model
 * @param remoteRepo remote repository (if set)
 * @throws MojoExecutionException
 */
private void writeArtifact(EclipseOsgiPlugin plugin, Model model, ArtifactRepository remoteRepo)
        throws MojoExecutionException {
    Writer fw = null;
    ArtifactMetadata metadata = null;
    File pomFile = null;
    Artifact pomArtifact = artifactFactory.createArtifact(model.getGroupId(), model.getArtifactId(),
            model.getVersion(), null, "pom"); //$NON-NLS-1$
    Artifact artifact = artifactFactory.createArtifact(model.getGroupId(), model.getArtifactId(),
            model.getVersion(), null, Constants.PROJECT_PACKAGING_JAR);
    try {
        pomFile = File.createTempFile("pom-", ".xml"); //$NON-NLS-1$ //$NON-NLS-2$

        // TODO use WriterFactory.newXmlWriter() when plexus-utils is upgraded to 1.4.5+
        fw = new OutputStreamWriter(new FileOutputStream(pomFile), "UTF-8"); //$NON-NLS-1$
        model.setModelEncoding("UTF-8"); // to be removed when encoding is detected instead of forced to UTF-8 //$NON-NLS-1$
        pomFile.deleteOnExit();
        new MavenXpp3Writer().write(fw, model);
        metadata = new ProjectArtifactMetadata(pomArtifact, pomFile);
        pomArtifact.addMetadata(metadata);
    } catch (IOException e) {
        throw new MojoExecutionException(
                Messages.getString("EclipseToMavenMojo.errorwritingtemporarypom", e.getMessage()), e); //$NON-NLS-1$
    } finally {
        IOUtil.close(fw);
    }

    try {
        File jarFile = plugin.getJarFile();

        if (remoteRepo != null) {
            deployer.deploy(pomFile, pomArtifact, remoteRepo, localRepository);
            deployer.deploy(jarFile, artifact, remoteRepo, localRepository);
        } else {
            installer.install(pomFile, pomArtifact, localRepository);
            installer.install(jarFile, artifact, localRepository);
        }
    } catch (ArtifactDeploymentException e) {
        throw new MojoExecutionException(
                Messages.getString("EclipseToMavenMojo.errordeployartifacttorepository"), e); //$NON-NLS-1$
    } catch (ArtifactInstallationException e) {
        throw new MojoExecutionException(
                Messages.getString("EclipseToMavenMojo.errorinstallartifacttorepository"), e); //$NON-NLS-1$
    } catch (IOException e) {
        throw new MojoExecutionException(
                Messages.getString("EclipseToMavenMojo.errorgettingjarfileforplugin", plugin), e); //$NON-NLS-1$
    } finally {
        pomFile.delete();
    }
}

From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.EclipseToMavenMojo.java

License:Open Source License

/**
 * Resolves the deploy<code>deployTo</code> parameter to an <code>ArtifactRepository</code> instance (if set).
 *
 * @return ArtifactRepository instance of null if <code>deployTo</code> is not set.
 * @throws MojoFailureException//from   w ww  .  j  a  va  2 s.co m
 * @throws MojoExecutionException
 */
private ArtifactRepository resolveRemoteRepo() throws MojoFailureException, MojoExecutionException {
    if (deployTo != null) {
        Matcher matcher = DEPLOYTO_PATTERN.matcher(deployTo);

        if (!matcher.matches()) {
            throw new MojoFailureException(deployTo,
                    Messages.getString("EclipseToMavenMojo.invalidsyntaxforrepository"),
                    //$NON-NLS-1$
                    Messages.getString("EclipseToMavenMojo.invalidremoterepositorysyntax")); //$NON-NLS-1$
        } else {
            String id = matcher.group(1).trim();
            String layout = matcher.group(2).trim();
            String url = matcher.group(3).trim();

            ArtifactRepositoryLayout repoLayout;
            try {
                repoLayout = (ArtifactRepositoryLayout) container.lookup(ArtifactRepositoryLayout.ROLE, layout);
            } catch (ComponentLookupException e) {
                throw new MojoExecutionException(
                        Messages.getString("EclipseToMavenMojo.cannotfindrepositorylayout", layout), e); //$NON-NLS-1$
            }

            return artifactRepositoryFactory.createDeploymentArtifactRepository(id, url, repoLayout, true);
        }
    }
    return null;
}

From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.InstallPluginsMojo.java

License:Open Source License

/**
 * Traverse the list of resolved dependency artifacts. For each one having a type that is listed in the
 * pluginDependencyTypes parameter value, resolve the associated project metadata (POM), and perform install(..) on
 * that artifact.//w  ww  .  j a va2s . co m
 */
public void execute() throws MojoExecutionException, MojoFailureException {
    if (eclipseDir == null) {
        getLog().info("Eclipse directory? ");

        String eclipseDirString;
        try {
            eclipseDirString = inputHandler.readLine();
        } catch (IOException e) {
            throw new MojoExecutionException("Unable to read from standard input", e);
        }

        eclipseDir = new File(eclipseDirString);
    }

    if (eclipseDir.exists() && !eclipseDir.isDirectory()) {
        throw new MojoFailureException("Invalid Eclipse directory: " + eclipseDir);
    } else if (!eclipseDir.exists()) {
        eclipseDir.mkdirs();
    }

    for (Iterator it = artifacts.iterator(); it.hasNext();) {
        Artifact artifact = (Artifact) it.next();

        if (pluginDependencyTypes.indexOf(artifact.getType()) > -1) {
            getLog().debug("Processing Eclipse plugin dependency: " + artifact.getId());

            MavenProject project;

            try {
                project = projectBuilder.buildFromRepository(artifact, Collections.EMPTY_LIST, localRepository,
                        true);
            } catch (ProjectBuildingException e) {
                throw new MojoExecutionException(
                        "Failed to load project metadata (POM) for: " + artifact.getId(), e);
            }

            install(artifact, project);
        } else {
            getLog().debug("Skipping dependency: " + artifact.getId()
                    + ". Set pluginDependencyTypes with a comma-separated list of types to change this.");
        }
    }
}

From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.InstallPluginsMojo.java

License:Open Source License

/**
 * <p>//w  ww .  j  a va 2 s  .co m
 * Install the plugin into the eclipse instance's /plugins directory
 * </p>
 * <ol>
 * <li>Determine whether the plugin should be extracted into a directory or not</li>
 * <li>If the plugin's target location exists, or overwrite is set to true:
 * <ol type="a">
 * <li>if extract, ensure the plugin target location exists (mkdirs), and extract there.</li>
 * <li>copy the plugin file from the local repository to the target location</li>
 * </ol>
 * <p>
 * Warn whenever a plugin will overwrite an existing file or directory, and emit an INFO message whenever a plugin
 * installation is skipped because of an existing file and overwrite == false.
 * </p>
 *
 * @param artifact The plugin dependency as it has been resolved.
 * @param project  The project metadata for the accompanying plugin-dependency artifact, used to determine whether to
 *                 install as a jar or as a directory
 * @throws MojoExecutionException In the event the plugin should be extracted but cannot, or the file copy fails (in
 *                                the event it should not be extracted)
 * @throws MojoFailureException   In the event that the plugins target directory (inside the Eclipse instance
 *                                directory) does not exist, or is not a directory.
 */
private void install(Artifact artifact, MavenProject project)
        throws MojoExecutionException, MojoFailureException {
    if (pluginsDir == null) {
        pluginsDir = new File(eclipseDir, "plugins");
    }

    if (!pluginsDir.exists() || !pluginsDir.isDirectory()) {
        throw new MojoFailureException("Invalid Eclipse directory: " + eclipseDir
                + " (plugins directory is missing or not a directory).");
    }

    boolean installAsJar = true;

    Properties properties = project.getProperties();
    if (properties != null) {
        installAsJar = !Boolean.valueOf(properties.getProperty(PROP_UNPACK_PLUGIN, "false")).booleanValue();
    }

    Attributes attributes = null;
    try {
        // don't verify, plugins zipped by eclipse:make-artifacts could have a bad signature
        JarFile jar = new JarFile(artifact.getFile(), false);
        Manifest manifest = jar.getManifest();
        if (manifest == null) {
            getLog().debug("Ignoring " + artifact.getArtifactId()
                    + " as it is does not have a Manifest (and so is not an OSGi bundle)");
            return;
        }
        attributes = manifest.getMainAttributes();
    } catch (IOException e) {
        throw new MojoExecutionException(
                "Unable to read manifest of plugin " + artifact.getFile().getAbsolutePath(), e);
    }

    String pluginName = formatEclipsePluginName(artifact);

    File pluginFile = new File(pluginsDir, pluginName + ".jar");
    File pluginDir = new File(pluginsDir, pluginName);

    boolean skipped = true;

    /* check if artifact is an OSGi bundle and ignore if not */
    Object bundleName = attributes.getValue("Bundle-Name");
    Object bundleSymbolicName = attributes.getValue("Bundle-SymbolicName");
    if (bundleSymbolicName == null && bundleName == null) {
        getLog().debug("Ignoring " + artifact.getArtifactId()
                + " as it is not an OSGi bundle (no Bundle-SymbolicName or Bundle-Name in manifest)");
        return;
    }

    if (overwrite) {
        if (pluginFile.exists() || pluginDir.exists()) {
            getLog().warn("Overwriting old plugin with contents of: " + artifact.getId());

            getLog().debug("Removing old plugin from both: " + pluginFile + " and: " + pluginDir);

            try {
                FileUtils.forceDelete(pluginDir);
                FileUtils.forceDelete(pluginFile);
            } catch (IOException e) {
                throw new MojoExecutionException(
                        "Failed to remove old plugin from: " + pluginFile + " or: " + pluginDir, e);
            }

            getLog().debug("Removal of old plugin is complete; proceeding with plugin installation.");
        }

        performFileOperations(installAsJar, artifact, pluginFile, pluginDir);

        skipped = false;
    } else if (installAsJar && !pluginFile.exists()) {
        performFileOperations(installAsJar, artifact, pluginFile, pluginDir);

        skipped = false;
    } else if (!installAsJar && !pluginDir.exists()) {
        performFileOperations(installAsJar, artifact, pluginFile, pluginDir);

        skipped = false;
    }

    if (skipped) {
        if (installAsJar) {
            getLog().info("Skipping plugin installation for: " + artifact.getId() + "; file: " + pluginFile
                    + " already exists. Set overwrite = true to override this.");
        } else if (!installAsJar) {
            getLog().info("Skipping plugin installation for: " + artifact.getId() + "; directory: " + pluginDir
                    + " already exists. Set overwrite = true to override this.");
        }
    }
}

From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.InstallPluginsMojo.java

License:Open Source License

private void performFileOperations(boolean installAsJar, Artifact artifact, File pluginFile, File pluginDir)
        throws MojoExecutionException {
    File artifactFile = artifact.getFile();

    if (installAsJar) {
        try {/* ww  w  .ja va2  s.c  o m*/
            getLog().debug("Copying: " + artifact.getId() + " to: " + pluginFile);

            FileUtils.copyFile(artifactFile, pluginFile);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to copy Eclipse plugin: " + artifact.getId() + "\nfrom: "
                    + artifact.getFile() + "\nto: " + pluginFile, e);
        }
    } else {
        try {
            getLog().debug("Expanding: " + artifact.getId() + " into: " + pluginDir);

            pluginDir.mkdirs();

            UnArchiver unarchiver = archiverManager.getUnArchiver(artifactFile);

            unarchiver.setSourceFile(artifactFile);
            unarchiver.setDestDirectory(pluginDir);
            unarchiver.extract();
        } catch (NoSuchArchiverException e) {
            throw new MojoExecutionException("Could not find unarchiver for: " + artifactFile, e);
        } catch (ArchiverException e) {
            throw new MojoExecutionException("Could not extract: " + artifactFile, e);
        } catch (IOException e) {
            throw new MojoExecutionException("Could not extract: " + artifactFile, e);
        }
    }
}