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 2021/12/12 08:47:41 UTC

[camel] branch main updated: [CAMEL-17310] Add dev goal enable live reloading (#6522)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 1d6404a  [CAMEL-17310] Add dev goal enable live reloading (#6522)
1d6404a is described below

commit 1d6404a92d21b7e5b4b1011907c221e353b671a1
Author: Federico Mariani <34...@users.noreply.github.com>
AuthorDate: Sun Dec 12 09:47:10 2021 +0100

    [CAMEL-17310] Add dev goal enable live reloading (#6522)
---
 .../support/FileWatcherResourceReloadStrategy.java |  8 +--
 .../camel/support/RouteWatcherReloadStrategy.java  |  6 ++-
 .../modules/ROOT/pages/camel-maven-plugin.adoc     | 21 ++++++++
 .../main/java/org/apache/camel/maven/DevMojo.java  | 62 ++++++++++++++++++++++
 .../main/java/org/apache/camel/maven/RunMojo.java  |  6 ++-
 5 files changed, 97 insertions(+), 6 deletions(-)

diff --git a/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java b/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java
index 87506f3..b1f3ffa 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/FileWatcherResourceReloadStrategy.java
@@ -34,6 +34,7 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.api.management.ManagedAttribute;
 import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.spi.Resource;
@@ -44,6 +45,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
+import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
 import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
 
 /**
@@ -179,7 +181,7 @@ public class FileWatcherResourceReloadStrategy extends ResourceReloadStrategySup
                         "FileWatcherReloadStrategy");
                 executorService.submit(task);
             } catch (IOException e) {
-                throw ObjectHelper.wrapRuntimeCamelException(e);
+                throw RuntimeCamelException.wrapRuntimeCamelException(e);
             }
         }
     }
@@ -187,9 +189,9 @@ public class FileWatcherResourceReloadStrategy extends ResourceReloadStrategySup
     private WatchKey registerPathToWatcher(WatchEvent.Modifier modifier, Path path, WatchService watcher) throws IOException {
         WatchKey key;
         if (modifier != null) {
-            key = path.register(watcher, new WatchEvent.Kind<?>[] { ENTRY_CREATE, ENTRY_MODIFY }, modifier);
+            key = path.register(watcher, new WatchEvent.Kind<?>[] { ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE }, modifier);
         } else {
-            key = path.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
+            key = path.register(watcher, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
         }
         return key;
     }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java b/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
index a9848e3..f2d84be 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/RouteWatcherReloadStrategy.java
@@ -17,6 +17,8 @@
 package org.apache.camel.support;
 
 import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
@@ -103,7 +105,7 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
                     // strip starting directory, so we have a relative name to the starting folder
                     String path = f.getAbsolutePath();
                     if (path.startsWith(base)) {
-                        path = path.substring(base.length());
+                        path = path.substring(path.lastIndexOf("/"));
                     }
                     path = FileUtil.stripLeadingSeparator(path);
 
@@ -165,7 +167,7 @@ public class RouteWatcherReloadStrategy extends FileWatcherResourceReloadStrateg
                 getCamelContext().getEndpointRegistry().clear();
             }
 
-            if (resource != null) {
+            if (resource != null && Files.exists(Paths.get(resource.getURI()))) {
                 sources.add(resource);
             }
 
diff --git a/docs/user-manual/modules/ROOT/pages/camel-maven-plugin.adoc b/docs/user-manual/modules/ROOT/pages/camel-maven-plugin.adoc
index 240f911..066360b 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-maven-plugin.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-maven-plugin.adoc
@@ -165,3 +165,24 @@ Remember to specify the class name of *your* main class where it says `com.foo.N
     </plugins>
   </build>
 ----
+
+
+== camel:dev
+
+The `camel:dev` goal of the Camel Maven Plugin is used to run your Camel integrations, it offers live reload of xml, yml and java files:
+
+----
+cd examples/routeloader
+mvn camel:dev
+----
+
+By adding, editing or removing files in `src/main/resources/myroutes` running routes will be stopped and new ones will be created.
+
+=== Options
+
+The maven plugin *dev* extends from the run one, so all the properties and behaviour are still valid, moreover a new property is defined:
+
+|===
+| Parameter | Default Value | Description
+| routesDirectory | null | Path to integration files, by default maven resource folder (src/main/resources) will be used.
+|===
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/DevMojo.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/DevMojo.java
new file mode 100644
index 0000000..e34613a
--- /dev/null
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/DevMojo.java
@@ -0,0 +1,62 @@
+/*
+ * 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.maven;
+
+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.plugins.annotations.ResolutionScope;
+
+@Mojo(name = "dev", defaultPhase = LifecyclePhase.PREPARE_PACKAGE,
+      requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
+public class DevMojo extends RunMojo {
+
+    /**
+     * To watch the directory for file changes which triggers a live reload of the Camel routes on-the-fly.
+     */
+    @Parameter(property = "camel.routesDirectory")
+    private String routesDirectory;
+
+    /**
+     * Enable routes reloading
+     *
+     * @throws Exception
+     */
+    @Override
+    protected void beforeBootstrapCamel() throws Exception {
+        String routesReloadDirectory = "src/main/resources";
+
+        if (routesDirectory != null) {
+            routesReloadDirectory = routesDirectory;
+        } else if (routesDirectory == null && project.getResources().size() == 1) {
+            routesReloadDirectory = project.getResources().get(0).getDirectory();
+        }
+
+        System.setProperty("camel.main.routesReloadEnabled", "true");
+        System.setProperty("camel.main.routesReloadDirectory", routesReloadDirectory);
+        System.setProperty("camel.main.routesReloadDirectoryRecursive", "true");
+        System.setProperty("camel.main.durationMaxAction", "stop");
+        System.setProperty("camel.main.routesReloadPattern",
+                "*.xml,*.yaml,*.java");
+    }
+
+    @Override
+    protected String goal() {
+        return "camel:dev";
+    }
+
+}
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
index 6b7ded2..c290477 100644
--- a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
@@ -309,7 +309,7 @@ public class RunMojo extends AbstractExecMojo {
         String skip = System.getProperties().getProperty("maven.test.skip");
         if (skip == null || "false".equals(skip)) {
             // lets log a INFO about how to skip tests if you want to so you can run faster
-            getLog().info("You can skip tests from the command line using: mvn camel:run -Dmaven.test.skip=true");
+            getLog().info("You can skip tests from the command line using: mvn " + goal() + " -Dmaven.test.skip=true");
         }
 
         boolean usingSpringJavaConfigureMain = false;
@@ -478,6 +478,10 @@ public class RunMojo extends AbstractExecMojo {
         registerSourceRoots();
     }
 
+    protected String goal() {
+        return "camel:run";
+    }
+
     /**
      * Allows plugin extensions to do custom logic before bootstrapping Camel.
      */