You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/01/14 15:59:52 UTC

[camel] 02/02: CAMEL-13051: The component json metafiles need to be enriched later with more details after the compilation process.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit fe7b90dc2182f7b6a9033dc1b3ede50eecdb2c39
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jan 14 16:34:36 2019 +0100

    CAMEL-13051: The component json metafiles need to be enriched later with more details after the compilation process.
---
 .../tools/apt/EndpointAnnotationProcessor.java     | 10 ++--
 .../maven/packaging/PackageComponentMojo.java      | 60 ++++++++++++++++++++--
 .../maven/packaging/PrepareComponentMojo.java      |  9 +++-
 3 files changed, 70 insertions(+), 9 deletions(-)

diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java
index 7bb2c9c..5d41446 100644
--- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java
+++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java
@@ -362,11 +362,11 @@ public class EndpointAnnotationProcessor extends AbstractCamelAnnotationProcesso
         model.setDeprecationNote(deprecationNote);
 
         // these information is not available at compile time and we enrich these later during the camel-package-maven-plugin
-        model.setJavaType("REPLACE-ME");
-        model.setDescription("REPLACE-ME");
-        model.setGroupId("REPLACE-ME");
-        model.setArtifactId("REPLACE-ME");
-        model.setVersionId("REPLACE-ME");
+        model.setJavaType("@@@JAVATYPE@@@");
+        model.setDescription("@@@DESCRIPTION@@@");
+        model.setGroupId("@@@GROUPID@@@");
+        model.setArtifactId("@@@ARTIFACTID@@@");
+        model.setVersionId("@@@VERSIONID@@@");
 
         // favor to use endpoint class javadoc as description
         Elements elementUtils = processingEnv.getElementUtils();
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageComponentMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageComponentMojo.java
index cfa6e39..23f43b4 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageComponentMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageComponentMojo.java
@@ -22,9 +22,11 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 
-import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -36,6 +38,9 @@ import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectHelper;
 import org.sonatype.plexus.build.incremental.BuildContext;
 
+import static org.apache.camel.maven.packaging.PackageHelper.loadText;
+import static org.apache.camel.maven.packaging.PackageHelper.writeText;
+
 /**
  * Analyses the Camel plugins in a project and generates extra descriptor information for easier auto-discovery in Camel.
  */
@@ -55,6 +60,13 @@ public class PackageComponentMojo extends AbstractMojo {
     protected File componentOutDir;
 
     /**
+     * The project build directory
+     *
+     */
+    @Parameter(defaultValue="${project.build.directory}")
+    protected File buildDir;
+
+    /**
      * Maven ProjectHelper.
      */
     @Component
@@ -75,10 +87,10 @@ public class PackageComponentMojo extends AbstractMojo {
      * @throws MojoFailureException something bad happened...
      */
     public void execute() throws MojoExecutionException, MojoFailureException {
-        prepareComponent(getLog(), project, projectHelper, componentOutDir, buildContext);
+        prepareComponent(getLog(), project, projectHelper, buildDir, componentOutDir, buildContext);
     }
 
-    public static void prepareComponent(Log log, MavenProject project, MavenProjectHelper projectHelper, File componentOutDir, BuildContext buildContext) throws MojoExecutionException {
+    public static void prepareComponent(Log log, MavenProject project, MavenProjectHelper projectHelper, File buildDir, File componentOutDir, BuildContext buildContext) throws MojoExecutionException {
 
         File camelMetaDir = new File(componentOutDir, "META-INF/services/org/apache/camel/");
 
@@ -95,6 +107,8 @@ public class PackageComponentMojo extends AbstractMojo {
         StringBuilder buffer = new StringBuilder();
         int count = 0;
 
+        Map<String, String> components = new LinkedHashMap<>();
+
         File f = new File(project.getBasedir(), "target/classes");
         f = new File(f, "META-INF/services/org/apache/camel/component");
         if (f.exists() && f.isDirectory()) {
@@ -113,10 +127,27 @@ public class PackageComponentMojo extends AbstractMojo {
                         }
                         buffer.append(name);
                     }
+
+                    // grab the java class name for the discovered component
+                    try {
+                        Properties prop = new Properties();
+                        prop.load(new FileInputStream(file));
+
+                        String javaType = prop.getProperty("class");
+
+                        components.put(name, javaType);
+                        log.debug("Discovered component: " + name + " with class: " + javaType);
+
+                    } catch (IOException e) {
+                        throw new MojoExecutionException("Failed to load file " + file + ". Reason: " + e, e);
+                    }
                 }
             }
         }
 
+        // we need to enrich the component json files with data we know have from this plugin
+        enrichComponentJsonFiles(log, project, buildDir, components);
+
         if (count > 0) {
             Properties properties = new Properties();
             String names = buffer.toString();
@@ -167,4 +198,27 @@ public class PackageComponentMojo extends AbstractMojo {
         }
     }
 
+    private static void enrichComponentJsonFiles(Log log, MavenProject project, File buildDir, Map<String, String> components) throws MojoExecutionException {
+        final Set<File> files = PackageHelper.findJsonFiles(buildDir, p -> p.isDirectory() || p.getName().endsWith(".json"));
+
+        for (File file : files) {
+            // name without .json
+            String shortName = file.getName().substring(0, file.getName().length() - 5);
+            String javaType = components.getOrDefault(shortName, "");
+            log.debug("Enriching file: " + file);
+
+            try {
+                String text = loadText(new FileInputStream(file));
+                text = text.replace("@@@JAVATYPE@@@", javaType);
+                text = text.replace("@@@DESCRIPTION@@@", project.getDescription());
+                text = text.replace("@@@GROUPID@@@", project.getGroupId());
+                text = text.replace("@@@ARTIFACTID@@@", project.getArtifactId());
+                text = text.replace("@@@VERSIONID@@@", project.getVersion());
+                writeText(file, text);
+            } catch (IOException e) {
+                throw new MojoExecutionException("Failed to update file " + file + ". Reason: " + e, e);
+            }
+        }
+    }
+
 }
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareComponentMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareComponentMojo.java
index c0454ef..e0642f3 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareComponentMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareComponentMojo.java
@@ -88,6 +88,13 @@ public class PrepareComponentMojo extends AbstractMojo {
     protected File schemaOutDir;
 
     /**
+     * The project build directory
+     *
+     */
+    @Parameter(defaultValue="${project.build.directory}")
+    protected File buildDir;
+
+    /**
      * Maven ProjectHelper.
      */
     @Component
@@ -108,7 +115,7 @@ public class PrepareComponentMojo extends AbstractMojo {
      * @throws org.apache.maven.plugin.MojoFailureException   something bad happened...
      */
     public void execute() throws MojoExecutionException, MojoFailureException {
-        prepareComponent(getLog(), project, projectHelper, componentOutDir, buildContext);
+        prepareComponent(getLog(), project, projectHelper, buildDir, componentOutDir, buildContext);
         prepareDataFormat(getLog(), project, projectHelper, dataFormatOutDir, schemaOutDir, buildContext);
         prepareLanguage(getLog(), project, projectHelper, languageOutDir, schemaOutDir, buildContext);
         prepareOthers(getLog(), project, projectHelper, otherOutDir, schemaOutDir, buildContext);