Search in sources :

Example 1 with ComponentDslMojo

use of org.apache.camel.maven.packaging.ComponentDslMojo in project cq-maven-plugin by l2x6.

the class CamelProdExcludesMojo method handleExcludedTargetDirectories.

void handleExcludedTargetDirectories(final Path basePath, final MavenSourceTree fullTree, final Set<Ga> excludes, Predicate<Profile> profiles) {
    /* Clean the target folders in all excluded modules so that Camel plugins do not see any stale content there */
    excludes.stream().map(ga -> fullTree.getModulesByGa().get(ga)).map(Module::getPomPath).map(basePath::resolve).map(Path::getParent).map(absPath -> absPath.resolve("target")).filter(Files::isDirectory).forEach(CqCommonUtils::deleteDirectory);
    /*
         * Unpack the community jars of excluded components to their target/classes so that Camel plugins find it there
         */
    project.getBasedir();
    excludes.stream().map(ga -> fullTree.getModulesByGa().get(ga)).filter(CamelProdExcludesMojo::isComponent).forEach(module -> {
        final String artifactId = module.getGav().getArtifactId().asConstant();
        final Path jarPath = CqCommonUtils.resolveArtifact(Paths.get(localRepository), "org.apache.camel", artifactId, camelCommunityVersion, "jar", repositories, repoSystem, repoSession);
        final Path pomFilePath = basePath.resolve(module.getPomPath());
        final Path moduleBaseDir = pomFilePath.getParent();
        final File outputDir = moduleBaseDir.resolve("target/classes").toFile();
        try (ZipFile zipFile = new ZipFile(jarPath.toFile())) {
            final Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                final ZipEntry entry = entries.nextElement();
                final File entryDestination = new File(outputDir, entry.getName());
                if (entry.isDirectory()) {
                    entryDestination.mkdirs();
                } else {
                    entryDestination.getParentFile().mkdirs();
                    try (InputStream in = zipFile.getInputStream(entry);
                        OutputStream out = new FileOutputStream(entryDestination)) {
                        IOUtils.copy(in, out);
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Could not extract " + jarPath + " to " + outputDir);
        }
        /* Execute ComponentDslMojo in the excluded component modules */
        getLog().info("Executing ComponentDslMojo in " + moduleBaseDir);
        final File origFile = project.getFile();
        project.setFile(pomFilePath.toFile());
        final String originalBuildDir = project.getBuild().getDirectory();
        project.getBuild().setDirectory(moduleBaseDir.resolve("target").toString());
        try {
            ComponentDslMojo mojo = new ComponentDslMojo();
            mojo.setLog(getLog());
            mojo.setPluginContext(getPluginContext());
            mojo.execute(project, projectHelper, new DefaultBuildContext());
        } catch (Exception e) {
            throw new RuntimeException("Could not excute ComponentDslMojo in " + moduleBaseDir, e);
        } finally {
            project.setFile(origFile);
            project.getBuild().setDirectory(originalBuildDir);
        }
    });
}
Also used : Path(java.nio.file.Path) ComponentDslMojo(org.apache.camel.maven.packaging.ComponentDslMojo) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) CqCommonUtils(org.l2x6.cq.common.CqCommonUtils) DefaultBuildContext(org.sonatype.plexus.build.incremental.DefaultBuildContext) ZipFile(java.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ComponentDslMojo (org.apache.camel.maven.packaging.ComponentDslMojo)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 CqCommonUtils (org.l2x6.cq.common.CqCommonUtils)1 DefaultBuildContext (org.sonatype.plexus.build.incremental.DefaultBuildContext)1