Search in sources :

Example 1 with CqCommonUtils

use of org.l2x6.cq.common.CqCommonUtils in project cq-maven-plugin by l2x6.

the class CamelSpringBootProdExcludesMojo 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(CamelSpringBootProdExcludesMojo::isComponent).forEach(module -> {
        final String artifactId = module.getGav().getArtifactId().asConstant();
        final Path jarPath = CqCommonUtils.resolveArtifact(Paths.get(localRepository), "org.apache.camel.springboot", 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);
        }
    });
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) CqCommonUtils(org.l2x6.cq.common.CqCommonUtils) ZipFile(java.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with CqCommonUtils

use of org.l2x6.cq.common.CqCommonUtils 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)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Path (java.nio.file.Path)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2 CqCommonUtils (org.l2x6.cq.common.CqCommonUtils)2 ComponentDslMojo (org.apache.camel.maven.packaging.ComponentDslMojo)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 DefaultBuildContext (org.sonatype.plexus.build.incremental.DefaultBuildContext)1