CAMEL-7999: Maven plugin to generate a list of all Camel components.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/251010ef Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/251010ef Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/251010ef Branch: refs/heads/master Commit: 251010efd3fedb87ce693f0e3fcf9deefbb4a0c4 Parents: 908b575 Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Nov 13 18:13:41 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Nov 13 18:13:41 2014 +0100 ---------------------------------------------------------------------- components/pom.xml | 9 -- platforms/commands/commands-core/pom.xml | 32 +------ .../apache/camel/commands/CamelController.java | 2 +- .../commands/catalog/CamelComponentCatalog.java | 3 + .../maven/packaging/CommandsPrepareMojo.java | 91 ++++++++++++++++++-- 5 files changed, 91 insertions(+), 46 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/251010ef/components/pom.xml ---------------------------------------------------------------------- diff --git a/components/pom.xml b/components/pom.xml index f18e37c..d761d6f 100644 --- a/components/pom.xml +++ b/components/pom.xml @@ -270,15 +270,6 @@ </plugin> </plugins> </build> - <reporting> - <plugins> - <plugin> - <groupId>org.apache.camel</groupId> - <artifactId>camel-package-maven-plugin</artifactId> - <version>${project.version}</version> - </plugin> - </plugins> - </reporting> <profiles> <!-- enables the APT dependency so that it can be disabled in IDE builds --> http://git-wip-us.apache.org/repos/asf/camel/blob/251010ef/platforms/commands/commands-core/pom.xml ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/pom.xml b/platforms/commands/commands-core/pom.xml index bb616c7..03f9bdb 100644 --- a/platforms/commands/commands-core/pom.xml +++ b/platforms/commands/commands-core/pom.xml @@ -33,8 +33,6 @@ <properties> <camel.osgi.export.pkg>org.apache.camel.commands.*</camel.osgi.export.pkg> - <camel.core.dir>../../../camel-core/</camel.core.dir> - <components.dir>../../../components/</components.dir> </properties> <dependencies> @@ -81,35 +79,7 @@ <build> <plugins> - <plugin> - <artifactId>maven-antrun-plugin</artifactId> - <version>1.7</version> - <executions> - <execution> - <phase>process-resources</phase> - <configuration> - <target> - <echo>Copying component json descriptors</echo> - <copy todir="${project.basedir}/target/classes/org/apache/camel/commands/catalog/components/" flatten="true"> - <fileset dir="${camel.core.dir}"> - <include name="**/target/classes/org/apache/camel/**/*.json"/> - </fileset> - </copy> - <copy todir="${project.basedir}/target/classes/org/apache/camel/commands/catalog/components/" flatten="true"> - <fileset dir="${components.dir}"> - <include name="**/target/classes/org/apache/camel/**/*.json"/> - </fileset> - </copy> - <!-- create a single file that has a full list of all the component names, with a fixed name --> - </target> - </configuration> - <goals> - <goal>run</goal> - </goals> - </execution> - </executions> - </plugin> - + <!-- generate and include all components in the catalog --> <plugin> <groupId>org.apache.camel</groupId> <artifactId>camel-package-maven-plugin</artifactId> http://git-wip-us.apache.org/repos/asf/camel/blob/251010ef/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java index 645f865..199f58e 100644 --- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/CamelController.java @@ -117,7 +117,7 @@ public interface CamelController { String explainEndpoint(String camelContextName, String uri, boolean allOptions) throws Exception; /** - * Lists all components and include information + * Lists Components which are in use or available on the classpath and include information * * @param camelContextName the Camel context. * @return a list of key/value pairs with component information http://git-wip-us.apache.org/repos/asf/camel/blob/251010ef/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalog.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalog.java b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalog.java index 65ba2af..7bac695 100644 --- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalog.java +++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/catalog/CamelComponentCatalog.java @@ -18,6 +18,9 @@ package org.apache.camel.commands.catalog; import java.util.List; +/** + * Catalog of all the Camel components from this Apache Camel release. + */ public interface CamelComponentCatalog { /** http://git-wip-us.apache.org/repos/asf/camel/blob/251010ef/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/CommandsPrepareMojo.java ---------------------------------------------------------------------- diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/CommandsPrepareMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/CommandsPrepareMojo.java index 6f2ff69..b807a54 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/CommandsPrepareMojo.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/CommandsPrepareMojo.java @@ -17,11 +17,16 @@ package org.apache.camel.maven.packaging; import java.io.File; +import java.io.FileFilter; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -37,6 +42,8 @@ import org.apache.maven.project.MavenProjectHelper; */ public class CommandsPrepareMojo extends AbstractMojo { + public static final int BUFFER_SIZE = 128 * 1024; + /** * The maven project. * @@ -56,14 +63,14 @@ public class CommandsPrepareMojo extends AbstractMojo { /** * The components directory where all the Apache Camel components are * - * @parameter default-value="${project.build.directory}/../../../components" + * @parameter default-value="${project.build.directory}/../../../../components" */ protected File componentsDir; /** * The camel-core directory where camel-core components are * - * @parameter default-value="${project.build.directory}/../../../camel-core" + * @parameter default-value="${project.build.directory}/../../../../camel-core" */ protected File coreDir; @@ -85,7 +92,38 @@ public class CommandsPrepareMojo extends AbstractMojo { public void execute() throws MojoExecutionException, MojoFailureException { getLog().info("Copying all Camel component json descriptors"); - // TODO: add the scan and copy code here + Set<File> files = new LinkedHashSet<File>(); + + // find all json files in components and camel-core + if (componentsDir != null && componentsDir.isDirectory()) { + File[] components = componentsDir.listFiles(); + if (components != null) { + for (File dir : components) { + if (dir.isDirectory()) { + File target = new File(dir, "target/classes"); + findFilesRecursive(target, files, new JsonAndDirFileFilter()); + } + } + } + } + if (coreDir != null && coreDir.isDirectory()) { + File target = new File(coreDir, "target/classes"); + findFilesRecursive(target, files, new JsonAndDirFileFilter()); + } + + getLog().info("Found " + files.size() + " component json files"); + + // make sure to create out dir + outDir.mkdirs(); + + for (File file : files) { + File to = new File(outDir, file.getName()); + try { + copyFile(file, to); + } catch (IOException e) { + throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e); + } + } File all = new File(outDir, "../components-catalog"); try { @@ -110,11 +148,54 @@ public class CommandsPrepareMojo extends AbstractMojo { fos.close(); - getLog().info("Found " + components.size() + " components to include in file: " + all); - + getLog().info("Camel components catalog includes " + files.size() + " components"); } catch (IOException e) { throw new MojoFailureException("Error writing to file " + all); } } + private void findFilesRecursive(File dir, Set<File> found, FileFilter filter) { + File[] files = dir.listFiles(filter); + if (files != null) { + for (File file : files) { + if (file.isFile()) { + found.add(file); + } else if (file.isDirectory()) { + findFilesRecursive(file, found, filter); + } + } + } + } + + private class JsonAndDirFileFilter implements FileFilter { + + @Override + public boolean accept(File pathname) { + return pathname.isDirectory() || pathname.getName().endsWith(".json"); + } + } + + public static void copyFile(File from, File to) throws IOException { + FileChannel in = null; + FileChannel out = null; + try { + in = new FileInputStream(from).getChannel(); + out = new FileOutputStream(to).getChannel(); + + long size = in.size(); + long position = 0; + while (position < size) { + position += in.transferTo(position, BUFFER_SIZE, out); + } + } finally { + if (in != null) { + in.close(); + } + if (out != null) { + out.close(); + } + } + } + + } \ No newline at end of file