Search in sources :

Example 56 with MojoFailureException

use of org.apache.maven.plugin.MojoFailureException in project maven-plugins by apache.

the class PmdViolationCheckMojoTest method testFailurePriority.

public void testFailurePriority() throws Exception {
    File testPom = new File(getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml");
    final PmdReport mojo = (PmdReport) lookupMojo("pmd", testPom);
    mojo.execute();
    testPom = new File(getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml");
    PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo("check", testPom);
    pmdViolationMojo.execute();
    testPom = new File(getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml");
    pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo("check", testPom);
    try {
        pmdViolationMojo.execute();
        fail("Exception Expected");
    } catch (final MojoFailureException e) {
        String message = e.getMessage();
        if (message.contains("You have 5 PMD violations and 3 warnings.")) {
            // expected
            System.out.println("Caught expected message: " + e.getMessage());
        } else {
            throw new AssertionError("Expected: '" + message + "' to contain 'You have 5 PMD violations and 3 warnings.'");
        }
    }
}
Also used : MojoFailureException(org.apache.maven.plugin.MojoFailureException) File(java.io.File)

Example 57 with MojoFailureException

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

the class ArchiveMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    getLog().debug("Setting artifact file: " + targetFile);
    org.apache.maven.artifact.Artifact artifact = project.getArtifact();
    artifact.setFile(targetFile);
    try {
        //now pack up the server.
        if (archiveTarGz) {
            archive("tar.gz");
        }
        if (archiveZip) {
            archive("zip");
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Could not archive plugin", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Artifact(org.apache.maven.artifact.Artifact) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 58 with MojoFailureException

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

the class RunMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    if (karafDirectory.exists()) {
        getLog().info("Using Karaf container located " + karafDirectory.getAbsolutePath());
    } else {
        getLog().info("Extracting Karaf container");
        try {
            File karafArchiveFile = resolveFile(karafDistribution);
            extract(karafArchiveFile, karafDirectory);
        } catch (Exception e) {
            throw new MojoFailureException("Can't extract Karaf container", e);
        }
    }
    getLog().info("Starting Karaf container");
    System.setProperty("karaf.home", karafDirectory.getAbsolutePath());
    System.setProperty("karaf.base", karafDirectory.getAbsolutePath());
    System.setProperty("karaf.data", karafDirectory.getAbsolutePath() + "/data");
    System.setProperty("karaf.etc", karafDirectory.getAbsolutePath() + "/etc");
    System.setProperty("karaf.instances", karafDirectory.getAbsolutePath() + "/instances");
    System.setProperty("karaf.startLocalConsole", "false");
    System.setProperty("karaf.startRemoteShell", startSsh);
    System.setProperty("karaf.lock", "false");
    Main main = new Main(new String[0]);
    try {
        main.launch();
        while (main.getFramework().getState() != Bundle.ACTIVE) {
            Thread.sleep(1000);
        }
        BundleContext bundleContext = main.getFramework().getBundleContext();
        Object bootFinished = null;
        while (bootFinished == null) {
            Thread.sleep(1000);
            ServiceReference ref = bundleContext.getServiceReference(BootFinished.class);
            if (ref != null) {
                bootFinished = bundleContext.getService(ref);
            }
        }
        deploy(bundleContext);
        if (keepRunning)
            main.awaitShutdown();
        main.destroy();
    } catch (Throwable e) {
        throw new MojoExecutionException("Can't start container", e);
    } finally {
        System.gc();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Main(org.apache.karaf.main.Main) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 59 with MojoFailureException

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

the class GenerateDescriptorMojo method saveDependencyChanges.

protected void saveDependencyChanges(Collection<Bundle> addedBundles, Collection<Bundle> removedBundles, Collection<Dependency> addedDependencys, Collection<Dependency> removedDependencys, ObjectFactory objectFactory) throws Exception {
    File addedFile = new File(filteredDependencyCache.getParentFile(), "dependencies.added.xml");
    Features added = toFeatures(addedBundles, addedDependencys, objectFactory);
    writeDependencies(added, addedFile);
    File removedFile = new File(filteredDependencyCache.getParentFile(), "dependencies.removed.xml");
    Features removed = toFeatures(removedBundles, removedDependencys, objectFactory);
    writeDependencies(removed, removedFile);
    StringWriter out = new StringWriter();
    out.write(saveTreeListing());
    out.write("Dependencies have changed:\n");
    if (!addedBundles.isEmpty() || !addedDependencys.isEmpty()) {
        out.write("\tAdded dependencies are saved here: " + addedFile.getAbsolutePath() + "\n");
        if (logDependencyChanges) {
            JaxbUtil.marshal(added, out);
        }
    }
    if (!removedBundles.isEmpty() || !removedDependencys.isEmpty()) {
        out.write("\tRemoved dependencies are saved here: " + removedFile.getAbsolutePath() + "\n");
        if (logDependencyChanges) {
            JaxbUtil.marshal(removed, out);
        }
    }
    out.write("Delete " + dependencyCache.getAbsolutePath() + " if you are happy with the dependency changes.");
    if (failOnDependencyChange) {
        throw new MojoFailureException(out.toString());
    } else {
        getLog().warn(out.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Features(org.apache.karaf.features.internal.model.Features) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) File(java.io.File)

Example 60 with MojoFailureException

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

the class GenerateServiceMetadata method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        boolean addSourceDirectory = false;
        StringBuilder requirements = new StringBuilder();
        StringBuilder capabilities = new StringBuilder();
        ClassFinder finder = createFinder(classLoader);
        List<Class<?>> classes = finder.findAnnotatedClasses(Services.class);
        List<Class<?>> activators = new ArrayList<>();
        for (Class<?> clazz : classes) {
            URL classUrl = clazz.getClassLoader().getResource(clazz.getName().replace('.', '/') + ".class");
            URL outputDirectoryUrl = new File(project.getBuild().getOutputDirectory()).toURI().toURL();
            if (classUrl == null || !classUrl.getPath().startsWith(outputDirectoryUrl.getPath())) {
                System.out.println("Ignoring " + classUrl);
                continue;
            }
            if (BundleActivator.class.isAssignableFrom(clazz)) {
                activators.add(clazz);
            }
            Properties props = new Properties();
            Services services = clazz.getAnnotation(Services.class);
            if (services != null) {
                for (RequireService req : services.requires()) {
                    String fltWithClass = combine(req.filter(), "(objectClass=" + req.value().getName() + ")");
                    addServiceReq(requirements, fltWithClass);
                    props.setProperty(req.value().getName(), req.filter());
                }
                for (ProvideService cap : services.provides()) {
                    addServiceCap(capabilities, cap);
                }
            }
            Managed managed = clazz.getAnnotation(Managed.class);
            if (managed != null) {
                props.setProperty("pid", managed.value());
            }
            File file = new File(outputDirectory, "OSGI-INF/karaf-tracker/" + clazz.getName());
            file.getParentFile().mkdirs();
            try (OutputStream os = buildContext.newFileOutputStream(file)) {
                props.store(os, null);
            }
            addSourceDirectory = true;
        }
        if (addSourceDirectory) {
            Resource resource = new Resource();
            resource.setDirectory(outputDirectory);
            project.addResource(resource);
        }
        project.getProperties().setProperty(requirementsProperty, requirements.toString());
        project.getProperties().setProperty(capabilitiesProperty, capabilities.toString());
        if (activators.size() == 1) {
            project.getProperties().setProperty(activatorProperty, activators.get(0).getName());
        }
        project.getProperties().setProperty("BNDExtension-Private-Package", "org.apache.karaf.util.tracker");
        project.getProperties().setProperty("BNDPrependExtension-Import-Package", "!org.apache.karaf.util.tracker.annotation");
        List<Class<?>> services = finder.findAnnotatedClasses(Service.class);
        Set<String> packages = new TreeSet<>();
        for (Class<?> clazz : services) {
            packages.add(clazz.getPackage().getName());
        }
        if (!packages.isEmpty()) {
            project.getProperties().setProperty("BNDExtension-Karaf-Commands", join(packages, ","));
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error building commands help", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) RequireService(org.apache.karaf.util.tracker.annotation.RequireService) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Resource(org.apache.maven.model.Resource) Properties(java.util.Properties) URL(java.net.URL) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Services(org.apache.karaf.util.tracker.annotation.Services) ProvideService(org.apache.karaf.util.tracker.annotation.ProvideService) TreeSet(java.util.TreeSet) ClassFinder(org.apache.xbean.finder.ClassFinder) File(java.io.File) Managed(org.apache.karaf.util.tracker.annotation.Managed)

Aggregations

MojoFailureException (org.apache.maven.plugin.MojoFailureException)165 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)101 File (java.io.File)96 IOException (java.io.IOException)93 ArrayList (java.util.ArrayList)53 TreeSet (java.util.TreeSet)33 FileInputStream (java.io.FileInputStream)32 List (java.util.List)23 FileOutputStream (java.io.FileOutputStream)22 Map (java.util.Map)22 MavenProject (org.apache.maven.project.MavenProject)20 MalformedURLException (java.net.MalformedURLException)16 Set (java.util.Set)15 Artifact (org.apache.maven.artifact.Artifact)15 HashMap (java.util.HashMap)14 AbstractMojo (org.apache.maven.plugin.AbstractMojo)14 InputStream (java.io.InputStream)13 URLClassLoader (java.net.URLClassLoader)13 HashSet (java.util.HashSet)13 URL (java.net.URL)12