Search in sources :

Example 71 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project sling by apache.

the class AttachPartialBundleListMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    final BundleList initializedBundleList;
    if (bundleListFile.exists()) {
        try {
            initializedBundleList = readBundleList(bundleListFile);
        } catch (IOException e) {
            throw new MojoExecutionException("Unable to read bundle list file", e);
        } catch (XmlPullParserException e) {
            throw new MojoExecutionException("Unable to read bundle list file", e);
        }
    } else {
        throw new MojoFailureException(String.format("Bundle list file %s does not exist.", bundleListFile.getAbsolutePath()));
    }
    interpolateProperties(initializedBundleList, this.project, this.mavenSession);
    final BundleListXpp3Writer writer = new BundleListXpp3Writer();
    try {
        this.bundleListOutput.getParentFile().mkdirs();
        writer.write(new FileWriter(bundleListOutput), initializedBundleList);
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to write bundle list", e);
    }
    // if this project is a partial bundle list, it's the main artifact
    if (project.getPackaging().equals(PARTIAL)) {
        project.getArtifact().setFile(bundleListOutput);
    } else {
        // otherwise attach it as an additional artifact
        projectHelper.attachArtifact(project, TYPE, CLASSIFIER, bundleListOutput);
    }
    this.getLog().info("Attaching bundle list configuration");
    try {
        this.attachConfigurations();
    } catch (final IOException ioe) {
        throw new MojoExecutionException("Unable to attach configuration.", ioe);
    } catch (final ArchiverException ioe) {
        throw new MojoExecutionException("Unable to attach configuration.", ioe);
    }
}
Also used : BundleListXpp3Writer(org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.io.xpp3.BundleListXpp3Writer) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) BundleListUtils.readBundleList(org.apache.sling.maven.projectsupport.BundleListUtils.readBundleList) BundleList(org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) FileWriter(java.io.FileWriter) MojoFailureException(org.apache.maven.plugin.MojoFailureException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException)

Example 72 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project sling by apache.

the class StartMojo method findLaunchpadJar.

/**
     * Finds the launchpad.jar artifact of the project being built.
     *
     * @return the launchpad.jar artifact
     * @throws MojoFailureException if a launchpad.jar artifact was not found
     */
private File findLaunchpadJar() throws MojoFailureException, MojoExecutionException {
    // If a launchpad JAR is specified, use it
    if (launchpadJar != null) {
        getLog().info("Using launchpad jar from '" + launchpadJar + "' given as configuration parameter!");
        return launchpadJar;
    }
    // If a launchpad dependency is configured, resolve it
    if (launchpadDependency != null) {
        getLog().info("Using launchpad dependency '" + launchpadDependency + "' given as configuration parameter!");
        return getArtifact(launchpadDependency).getFile();
    }
    // If the current project is a slingstart project, use its JAR artifact
    if (this.project.getPackaging().equals(BuildConstants.PACKAGING_SLINGSTART)) {
        File jarFile = project.getArtifact().getFile();
        if (jarFile != null && jarFile.exists()) {
            getLog().info("Using launchpad jar being generated as this project's primary artifact: '" + jarFile + "'!");
            return jarFile;
        } else {
            jarFile = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".jar");
            if (jarFile.exists()) {
                getLog().info("Using launchpad jar being generated as this project's primary artifact: '" + jarFile + "'!");
                return jarFile;
            }
        }
    }
    // In case there was a provisioning model found but this is not a slingstart project, the JAR might be attached already through goal "package"
    for (Artifact attachedArtifact : project.getAttachedArtifacts()) {
        // find the attached artifact with classifier "standalone"
        if (BuildConstants.TYPE_JAR.equals(attachedArtifact.getType()) && BuildConstants.CLASSIFIER_APP.equals(attachedArtifact.getClassifier())) {
            getLog().info("Using launchpad jar being attached as additional project artifact: '" + attachedArtifact.getFile() + "'!");
            return attachedArtifact.getFile();
        }
    }
    // Last chance: use the first declared dependency with type "slingstart"
    final Set<Artifact> dependencies = this.project.getDependencyArtifacts();
    for (final Artifact dep : dependencies) {
        if (BuildConstants.PACKAGING_SLINGSTART.equals(dep.getType())) {
            final Dependency d = new Dependency();
            d.setGroupId(dep.getGroupId());
            d.setArtifactId(dep.getArtifactId());
            d.setVersion(dep.getVersion());
            d.setScope(Artifact.SCOPE_RUNTIME);
            d.setType(BuildConstants.TYPE_JAR);
            getLog().info("Using launchpad jar from first dependency of type 'slingstart': '" + d + "'!");
            return getArtifact(d).getFile();
        }
    }
    // Launchpad has not been found, throw an exception
    throw new MojoFailureException("Could not find the launchpad jar. " + "Either specify the 'launchpadJar' configuration or use this inside a slingstart project.");
}
Also used : MojoFailureException(org.apache.maven.plugin.MojoFailureException) Dependency(org.apache.maven.model.Dependency) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 73 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project sling by apache.

the class StopMojo method doExecute.

@Override
protected void doExecute() throws MojoExecutionException, MojoFailureException {
    // read configurations
    final Properties launchpadConfigProps = new Properties();
    Reader reader = null;
    try {
        reader = new FileReader(this.systemPropertiesFile);
        launchpadConfigProps.load(reader);
    } catch (final IOException ioe) {
        throw new MojoExecutionException("Unable to read launchpad runner configuration properties.", ioe);
    } finally {
        IOUtils.closeQuietly(reader);
    }
    final int instances = Integer.valueOf(launchpadConfigProps.getProperty("launchpad.instances"));
    final List<ProcessDescription> configurations = new ArrayList<ProcessDescription>();
    for (int i = 1; i <= instances; i++) {
        final String id = launchpadConfigProps.getProperty("launchpad.instance.id." + String.valueOf(i));
        final ProcessDescription config = ProcessDescriptionProvider.getInstance().getRunConfiguration(id);
        if (config == null) {
            getLog().warn("No launchpad configuration found for instance " + id);
        } else {
            configurations.add(config);
        }
    }
    blockIfNecessary();
    if (configurations.size() > 0) {
        getLog().info(new StringBuilder("Stopping ").append(configurations.size()).append(" Launchpad instances").toString());
        for (final ProcessDescription cfg : configurations) {
            try {
                LauncherCallable.stop(this.getLog(), cfg);
                ProcessDescriptionProvider.getInstance().removeRunConfiguration(cfg.getId());
            } catch (Exception e) {
                throw new MojoExecutionException("Could not stop launchpad " + cfg.getId(), e);
            }
        }
    } else {
        getLog().warn("No stored configuration file was found at " + this.systemPropertiesFile + " - no Launchapd will be stopped");
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) Reader(java.io.Reader) FileReader(java.io.FileReader) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 74 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project sling by apache.

the class StartMojo method doExecute.

@Override
protected void doExecute() throws MojoExecutionException, MojoFailureException {
    // delete properties
    if (systemPropertiesFile != null && systemPropertiesFile.exists()) {
        FileUtils.deleteQuietly(this.systemPropertiesFile);
    }
    // get configurations
    final Collection<ServerConfiguration> configurations = getLaunchpadConfigurations();
    // create the common environment
    final LaunchpadEnvironment env = new LaunchpadEnvironment(this.findLaunchpadJar(), this.cleanWorkingDirectory, !this.keepLaunchpadRunning, this.launchpadReadyTimeOutSec, this.debug);
    // create callables
    final Collection<LauncherCallable> tasks = new LinkedList<LauncherCallable>();
    for (final ServerConfiguration launchpadConfiguration : configurations) {
        validateConfiguration(launchpadConfiguration);
        tasks.add(createTask(launchpadConfiguration, env));
    }
    // create the launchpad runner properties
    this.createLaunchpadRunnerProperties(configurations);
    if (parallelExecution) {
        // ExecutorService for starting launchpad instances in parallel
        final ExecutorService executor = Executors.newCachedThreadPool();
        try {
            final List<Future<ProcessDescription>> resultsCollector = executor.invokeAll(tasks);
            for (final Future<ProcessDescription> future : resultsCollector) {
                try {
                    if (null == future.get()) {
                        throw new MojoExecutionException("Cannot start all the instances");
                    }
                } catch (final ExecutionException e) {
                    throw new MojoExecutionException(e.getLocalizedMessage(), e);
                }
            }
        } catch (final InterruptedException e) {
            throw new MojoExecutionException(e.getLocalizedMessage(), e);
        }
    } else {
        for (final LauncherCallable task : tasks) {
            try {
                if (null == task.call()) {
                    throw new MojoExecutionException("Cannot start all the instances");
                }
            } catch (final Exception e) {
                throw new MojoExecutionException(e.getLocalizedMessage(), e);
            }
        }
    }
    if (this.keepLaunchpadRunning) {
        getLog().info("Press CTRL-C to stop launchpad instance(s)...");
        while (true && this.isRunning(tasks)) {
            try {
                Thread.sleep(5000);
            } catch (final InterruptedException ie) {
                break;
            }
        }
    }
    blockIfNecessary();
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) LinkedList(java.util.LinkedList) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ExecutionException(java.util.concurrent.ExecutionException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 75 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project tomee by apache.

the class AbstractTomEEMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    fixConfig();
    if ("-1".equals(tomeeVersion)) {
        tomeeVersion = OpenEjbVersion.get().getVersion();
    }
    if (!tomeeAlreadyInstalled) {
        // added before using the plugin with maven dependency plugin or sthg like that
        final Collection<String> existingWebapps;
        if (removeDefaultWebapps) {
            existingWebapps = webappsAlreadyAdded();
        } else {
            existingWebapps = Collections.emptyList();
        }
        unzip(resolve());
        if (inlinedServerXml != null && inlinedServerXml.getChildCount() > 0) {
            final File serverXml = new File(catalinaBase, "conf/server.xml");
            try {
                FileUtils.forceMkdir(serverXml.getParentFile());
                FileUtils.fileWrite(serverXml, XmlFormatter.format(inlinedServerXml.getChild(0).toString()));
            } catch (final Exception e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
        if (inlinedTomEEXml != null && inlinedTomEEXml.getChildCount() > 0) {
            final File tomeeXml = new File(catalinaBase, "conf/tomee.xml");
            try {
                FileUtils.forceMkdir(tomeeXml.getParentFile());
                FileUtils.fileWrite(tomeeXml, XmlFormatter.format(inlinedTomEEXml.getChild(0).toString()));
            } catch (final Exception e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
        overrideConf(config, "conf");
        overrideServerXml();
        alignConfigOnServerXmlCurrentConfig();
        if (removeDefaultWebapps) {
            // do it first to let add other war
            removeDefaultWebapps(removeTomeeWebapp, existingWebapps);
        }
        if (classpaths == null) {
            // NPE protection for activateSimpleLog() and run()
            classpaths = new ArrayList<>();
        }
        if (simpleLog) {
            activateSimpleLog();
        }
        copyLibs(libs, new File(catalinaBase, libDir), "jar");
        copyLibs(endorsedLibs, new File(catalinaBase, "endorsed"), "jar");
        copyLibs(webapps, new File(catalinaBase, webappDir), "war");
        copyLibs(apps, new File(catalinaBase, appDir), "jar");
        overrideConf(lib, "lib");
        final Collection<File> copied = overrideConf(bin, "bin");
        for (final File copy : copied) {
            if (copy.getName().endsWith(".sh")) {
                if (!copy.setExecutable(true)) {
                    getLog().warn("can't make " + copy.getPath() + " executable");
                }
            }
        }
        if (!skipCurrentProject) {
            copyWar();
        }
        if (customizers != null) {
            final Thread thread = Thread.currentThread();
            final ClassLoader currentLoader = thread.getContextClassLoader();
            final ClassLoader tccl = createClassLoader(currentLoader);
            thread.setContextClassLoader(tccl);
            try {
                // if really needed we could introduce a Customizer interface but then it has more impacts on your packaging/config
                for (final String customizer : customizers) {
                    try {
                        final Class<?> clazz = tccl.loadClass(customizer);
                        try {
                            clazz.getMethod("main", String[].class).invoke(null, new String[] { catalinaBase.getAbsolutePath() });
                        } catch (final NoSuchMethodException noMainEx) {
                            try {
                                final Constructor<?> cons = clazz.getConstructor(File.class);
                                Runnable.class.cast(cons.newInstance(catalinaBase)).run();
                            } catch (final NoSuchMethodException e) {
                                try {
                                    Runnable.class.cast(clazz.newInstance()).run();
                                } catch (final Exception e1) {
                                    throw new MojoExecutionException("can't create customizer: " + currentLoader, e);
                                }
                            } catch (final InvocationTargetException | InstantiationException | IllegalAccessException e) {
                                throw new MojoExecutionException("can't create customizer: " + currentLoader, e);
                            }
                        } catch (final InvocationTargetException | IllegalAccessException e) {
                            throw new MojoExecutionException("can't find customizer: " + currentLoader, e);
                        }
                    } catch (final ClassNotFoundException e) {
                        throw new MojoExecutionException("can't find customizer: " + currentLoader, e);
                    }
                }
            } finally {
                try {
                    if (tccl != null && Closeable.class.isInstance(tccl)) {
                        Closeable.class.cast(tccl).close();
                    }
                } catch (final IOException e) {
                // no-op
                }
                thread.setContextClassLoader(currentLoader);
            }
        }
        scriptCustomization(jsCustomizers, "js");
        scriptCustomization(groovyCustomizers, "groovy");
    } else {
        alignConfigOnServerXmlCurrentConfig();
    }
    run();
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Constructor(java.lang.reflect.Constructor) Closeable(java.io.Closeable) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) ScriptException(javax.script.ScriptException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchElementException(java.util.NoSuchElementException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) URLClassLoader(java.net.URLClassLoader) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

MojoFailureException (org.apache.maven.plugin.MojoFailureException)157 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)94 File (java.io.File)93 IOException (java.io.IOException)91 ArrayList (java.util.ArrayList)50 TreeSet (java.util.TreeSet)33 FileInputStream (java.io.FileInputStream)32 FileOutputStream (java.io.FileOutputStream)21 List (java.util.List)21 Map (java.util.Map)21 MavenProject (org.apache.maven.project.MavenProject)18 Set (java.util.Set)15 MalformedURLException (java.net.MalformedURLException)14 HashMap (java.util.HashMap)13 HashSet (java.util.HashSet)13 InputStream (java.io.InputStream)12 URLClassLoader (java.net.URLClassLoader)12 Matcher (java.util.regex.Matcher)12 Artifact (org.apache.maven.artifact.Artifact)12 AbstractMojo (org.apache.maven.plugin.AbstractMojo)12