You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2022/05/05 12:21:06 UTC

[camel-kamelets] branch main updated: Create a Maven Kamelet Plugin - First commit

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

acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-kamelets.git


The following commit(s) were added to refs/heads/main by this push:
     new ad752a8b Create a Maven Kamelet Plugin - First commit
ad752a8b is described below

commit ad752a8b8d7569b9b51d1543ef4b499a1f4753c1
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu May 5 14:15:45 2022 +0200

    Create a Maven Kamelet Plugin - First commit
---
 library/camel-kamelets-bom/pom.xml                 |  22 ++++
 library/camel-kamelets-catalog/pom.xml             |   6 -
 .../kamelets/catalog/KameletsCatalogTest.java      |  64 ----------
 library/kamelets-maven-plugin/pom.xml              | 132 +++++++++++++++++++++
 .../maven/plugin/ValidateKameletsMojo.java         | 123 +++++++++++++++++++
 pom.xml                                            |   1 +
 6 files changed, 278 insertions(+), 70 deletions(-)

diff --git a/library/camel-kamelets-bom/pom.xml b/library/camel-kamelets-bom/pom.xml
index b0133395..dfb6fa14 100644
--- a/library/camel-kamelets-bom/pom.xml
+++ b/library/camel-kamelets-bom/pom.xml
@@ -68,4 +68,26 @@
       </dependency>
     </dependencies>
   </dependencyManagement>
+
+    <build>
+        <plugins>
+
+            <!-- Validate Kamelets -->
+            <plugin>
+                <groupId>org.apache.camel.kamelets</groupId>
+                <artifactId>kamelets-maven-plugin</artifactId>
+                <version>${project.version}</version>
+                <executions>
+                    <execution>
+                        <phase>compile</phase>
+                        <goals>
+                            <goal>validate</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+
+        </plugins>
+
+    </build>
 </project>
diff --git a/library/camel-kamelets-catalog/pom.xml b/library/camel-kamelets-catalog/pom.xml
index b7f18cea..f13550ba 100644
--- a/library/camel-kamelets-catalog/pom.xml
+++ b/library/camel-kamelets-catalog/pom.xml
@@ -80,12 +80,6 @@
             <version>${classgraph.version}</version>
         </dependency>
 
-        <dependency>
-            <groupId>org.apache.camel</groupId>
-            <artifactId>camel-catalog</artifactId>
-            <version>${camel.version}</version>
-        </dependency>
-
         <!-- logging -->
         <dependency>
             <groupId>org.apache.logging.log4j</groupId>
diff --git a/library/camel-kamelets-catalog/src/test/java/org/apache/camel/kamelets/catalog/KameletsCatalogTest.java b/library/camel-kamelets-catalog/src/test/java/org/apache/camel/kamelets/catalog/KameletsCatalogTest.java
index bada0576..669bba8c 100644
--- a/library/camel-kamelets-catalog/src/test/java/org/apache/camel/kamelets/catalog/KameletsCatalogTest.java
+++ b/library/camel-kamelets-catalog/src/test/java/org/apache/camel/kamelets/catalog/KameletsCatalogTest.java
@@ -19,12 +19,7 @@ package org.apache.camel.kamelets.catalog;
 import io.fabric8.camelk.v1alpha1.Kamelet;
 import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
 import io.github.classgraph.ClassGraph;
-
-import org.apache.camel.catalog.DefaultCamelCatalog;
 import org.apache.camel.kamelets.catalog.model.KameletTypeEnum;
-import org.apache.camel.tooling.model.ApiMethodModel;
-import org.apache.camel.tooling.model.ApiModel;
-import org.apache.camel.tooling.model.ComponentModel;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
@@ -33,12 +28,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import java.util.Arrays;
-import java.util.Collection;
 import java.util.List;
 import java.util.Map;
-import java.util.function.Predicate;
-import java.util.stream.Collectors;
 
 public class KameletsCatalogTest {
     static KameletsCatalog catalog;
@@ -133,59 +124,4 @@ public class KameletsCatalogTest {
     void testAllKameletDependencies() throws Exception {
         catalog.getAllKameletDependencies();
     }
-
-    @Test
-    void testOptionsParams() throws Exception {
-        String[] bannedDeps = {"mvn:", "camel:gson", "camel:core", "camel:kamelet", "github:apache.camel-kamelets:camel-kamelets-utils:main-SNAPSHOT"};
-        List<String> bannedDepsList = Arrays.asList(bannedDeps);
-        DefaultCamelCatalog cc = new DefaultCamelCatalog();
-        List<String> names = catalog.getKameletsName();
-        for (String name:
-             names) {
-            Map<String, Object> kd = catalog.getKameletTemplate(name);
-            Map<String,Object> f = (Map) kd.get("from");
-            Map<String,Object> p = (Map) f.get("parameters");
-            List<String> deps = catalog.getKameletDependencies(name).stream()
-                    .filter(Predicate.not(bannedDepsList::contains)).collect(Collectors.toList());
-            String cleanName;
-            if (!deps.isEmpty()) {
-                if (deps.get(0).equals("camel:jackson") && deps.size() > 1) {
-                    cleanName = deps.get(1).replace("camel:", "");
-                } else {
-                    cleanName = deps.get(0).replace("camel:", "");
-                }
-                if (cleanName.equalsIgnoreCase("cassandraql")) {
-                    cleanName = "cql";
-                }
-                if (cleanName.equalsIgnoreCase("aws2-ddb") && name.equals("aws-ddb-streams-source")) {
-                    cleanName = "aws2-ddb-streams";
-                }
-                if (cleanName.equalsIgnoreCase("google-sheets") && name.equals("google-sheets-source")) {
-                    cleanName = "google-sheets-stream";
-                }
-                if (cleanName.equalsIgnoreCase("google-mail") && name.equals("google-mail-source")) {
-                    cleanName = "google-mail-stream";
-                }
-                if (cleanName.equalsIgnoreCase("google-calendar") && name.equals("google-calendar-source")) {
-                    cleanName = "google-calendar-stream";
-                }
-                if (p != null && !p.isEmpty()) {
-                    ComponentModel componentModel = cc.componentModel(cleanName);
-                    if (componentModel != null) {
-                        List<ComponentModel.EndpointOptionModel> ce = componentModel.getEndpointOptions();
-                        List<String> ceInternal =
-                                ce.stream()
-                                        .map(ComponentModel.EndpointOptionModel::getName)
-                                        .collect(Collectors.toList());
-
-                        for (Map.Entry<String, Object> entry : p.entrySet()) {
-                            if (!entry.getKey().equals("period") && (!name.equals("kafka-ssl-source") && !name.equals("timer-source") && !name.equals("cron-source") && !name.equals("fhir-source"))) {
-                                assertTrue(ceInternal.contains(entry.getKey()));
-                            }
-                        }
-                    }
-                }
-            }
-        }
-    }
 }
diff --git a/library/kamelets-maven-plugin/pom.xml b/library/kamelets-maven-plugin/pom.xml
new file mode 100644
index 00000000..ac590c5d
--- /dev/null
+++ b/library/kamelets-maven-plugin/pom.xml
@@ -0,0 +1,132 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+         xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel.kamelets</groupId>
+        <artifactId>camel-kamelets-parent</artifactId>
+        <version>main-SNAPSHOT</version>
+        <relativePath>../../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>kamelets-maven-plugin</artifactId>
+    <packaging>maven-plugin</packaging>
+    <name>Camel Kamelets Maven Plugin</name>
+    <description>Camel Kamelets Maven Plugin</description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-util</artifactId>
+            <version>${camel.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-tooling-util</artifactId>
+            <version>${camel.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.maven.resolver</groupId>
+            <artifactId>maven-resolver-api</artifactId>
+            <version>1.8.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.maven</groupId>
+            <artifactId>maven-core</artifactId>
+            <version>3.8.5</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.maven</groupId>
+            <artifactId>maven-artifact</artifactId>
+            <version>3.8.5</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.maven</groupId>
+            <artifactId>maven-plugin-api</artifactId>
+            <version>3.8.5</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.maven.plugin-tools</groupId>
+            <artifactId>maven-plugin-annotations</artifactId>
+            <version>3.6.4</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.maven</groupId>
+            <artifactId>maven-compat</artifactId>
+            <version>3.8.5</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.codehaus.plexus</groupId>
+            <artifactId>plexus-container-default</artifactId>
+            <version>2.1.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.codehaus.plexus</groupId>
+            <artifactId>plexus-utils</artifactId>
+            <version>3.4.1</version>
+        </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.11.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-catalog</artifactId>
+            <version>${camel.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.kamelets</groupId>
+            <artifactId>camel-kamelets-catalog</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-plugin-plugin</artifactId>
+                <version>3.6.4</version>
+                <configuration>
+                    <mojoDependencies>
+                        <dep>org.apache.maven:maven-plugin-api</dep>
+                    </mojoDependencies>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>11</source>
+                    <target>11</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
diff --git a/library/kamelets-maven-plugin/src/main/java/org/apache/camel/kamelets/maven/plugin/ValidateKameletsMojo.java b/library/kamelets-maven-plugin/src/main/java/org/apache/camel/kamelets/maven/plugin/ValidateKameletsMojo.java
new file mode 100644
index 00000000..da50c43a
--- /dev/null
+++ b/library/kamelets-maven-plugin/src/main/java/org/apache/camel/kamelets/maven/plugin/ValidateKameletsMojo.java
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.kamelets.maven.plugin;
+
+import java.io.File;
+import java.util.*;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+import org.apache.camel.catalog.DefaultCamelCatalog;
+import org.apache.camel.kamelets.catalog.KameletsCatalog;
+import org.apache.camel.tooling.model.ComponentModel;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.MavenProject;
+
+/**
+ * Copy the properties from a source POM to a different destination POM for syncing purposes.
+ */
+@Mojo(name = "validate", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
+public class ValidateKameletsMojo extends AbstractMojo {
+
+    /**
+     * The base directory
+     */
+    @Parameter(defaultValue = "${project.basedir}")
+    protected File baseDir;
+
+    /**
+     * The Maven project.
+     */
+    @Parameter(defaultValue = "${project}", readonly = true, required = true)
+    protected MavenProject project;
+
+    /**
+     * Whether to fail if validation failed or not. By default true.
+     */
+    @Parameter(property = "kamelets.failOnError", defaultValue = "true")
+    private boolean failOnError = true;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        String[] bannedDeps = {"mvn:", "camel:gson", "camel:core", "camel:kamelet", "github:apache.camel-kamelets:camel-kamelets-utils:main-SNAPSHOT"};
+        List<String> bannedDepsList = Arrays.asList(bannedDeps);
+        KameletsCatalog catalog = new KameletsCatalog();
+        DefaultCamelCatalog cc = new DefaultCamelCatalog();
+        List<String> names = catalog.getKameletsName();
+        for (String name:
+                names) {
+            Map<String, Object> kd = catalog.getKameletTemplate(name);
+            Map<String,Object> f = (Map) kd.get("from");
+            Map<String,Object> p = (Map) f.get("parameters");
+            List<String> deps = catalog.getKameletDependencies(name).stream()
+                    .filter(Predicate.not(bannedDepsList::contains)).collect(Collectors.toList());
+            String cleanName;
+            if (!deps.isEmpty()) {
+                if (deps.get(0).equals("camel:jackson") && deps.size() > 1) {
+                    cleanName = deps.get(1).replace("camel:", "");
+                } else {
+                    cleanName = deps.get(0).replace("camel:", "");
+                }
+                if (cleanName.equalsIgnoreCase("cassandraql")) {
+                    cleanName = "cql";
+                }
+                if (cleanName.equalsIgnoreCase("aws2-ddb") && name.equals("aws-ddb-streams-source")) {
+                    cleanName = "aws2-ddb-streams";
+                }
+                if (cleanName.equalsIgnoreCase("google-sheets") && name.equals("google-sheets-source")) {
+                    cleanName = "google-sheets-stream";
+                }
+                if (cleanName.equalsIgnoreCase("google-mail") && name.equals("google-mail-source")) {
+                    cleanName = "google-mail-stream";
+                }
+                if (cleanName.equalsIgnoreCase("google-calendar") && name.equals("google-calendar-source")) {
+                    cleanName = "google-calendar-stream";
+                }
+                if (p != null && !p.isEmpty()) {
+                    ComponentModel componentModel = cc.componentModel(cleanName);
+                    if (componentModel != null) {
+                        List<ComponentModel.EndpointOptionModel> ce = componentModel.getEndpointOptions();
+                        List<String> ceInternal =
+                                ce.stream()
+                                        .map(ComponentModel.EndpointOptionModel::getName)
+                                        .collect(Collectors.toList());
+                        for (Map.Entry<String, Object> entry : p.entrySet()) {
+                            if (!entry.getKey().equals("period") && (!name.equals("kafka-ssl-source") && !name.equals("timer-source") && !name.equals("cron-source") && !name.equals("fhir-source"))) {
+                                if (!ceInternal.contains(entry.getKey())) {
+                                    getLog().error("Kamelet Name: " + name);
+                                    getLog().error("Scheme Name: " + cleanName);
+                                    getLog().error("Parameter: " + entry.getKey());
+                                    getLog().error("The parameter " + entry.getKey() + " doesn't exist in the endpoint options of " + cleanName + " component");
+                                    if (failOnError) {
+                                        throw new MojoExecutionException("The Kamelets Validation failed. See logs for more information." + "\n");
+                                    }
+                                    break;
+                                }
+                            }
+                        }
+                        }
+                    }
+                }
+            }
+            getLog().info("Validation passed");
+        }
+    }
diff --git a/pom.xml b/pom.xml
index a9b0f9d7..519d1c3f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -36,6 +36,7 @@
     <modules>
         <module>library/camel-kamelets</module>
         <module>library/camel-kamelets-catalog</module>
+        <module>library/kamelets-maven-plugin</module>
         <module>library/camel-kamelets-utils</module>
         <module>library/camel-kamelets-bom</module>
     </modules>