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


The following commit(s) were added to refs/heads/master by this push:
     new 5273887  CAMEL-13702: camel-main-maven-plugin - Only download sources 
JAR if really neesed and optimise to only write files if they are 
changed/needed.
5273887 is described below

commit 527388788a24c2badab14784ccde491c021ee5b7
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Mon Jul 1 08:24:57 2019 +0200

    CAMEL-13702: camel-main-maven-plugin - Only download sources JAR if really 
neesed and optimise to only write files if they are changed/needed.
---
 .../java/org/apache/camel/maven/GenerateMojo.java  | 72 +++++++++++++++++-----
 1 file changed, 58 insertions(+), 14 deletions(-)

diff --git 
a/catalog/camel-main-maven-plugin/src/main/java/org/apache/camel/maven/GenerateMojo.java
 
b/catalog/camel-main-maven-plugin/src/main/java/org/apache/camel/maven/GenerateMojo.java
index a10300c..47621f0 100644
--- 
a/catalog/camel-main-maven-plugin/src/main/java/org/apache/camel/maven/GenerateMojo.java
+++ 
b/catalog/camel-main-maven-plugin/src/main/java/org/apache/camel/maven/GenerateMojo.java
@@ -18,6 +18,7 @@ package org.apache.camel.maven;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -30,6 +31,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.stream.Collectors;
 
 import org.apache.camel.maven.model.AutowireData;
@@ -158,8 +160,28 @@ public class GenerateMojo extends AbstractMainMojo {
         final List<SpringBootPropertyData> propertyData = new ArrayList<>();
         final List<SpringBootGroupData> groupData = new ArrayList<>();
 
+        String existingText = null;
+        if (springBootEnabled) {
+            // is this a new component
+            File file = new File(outSpringBootFolder, 
"spring-configuration-metadata.json");
+            if (file.exists()) {
+                try {
+                    InputStream is = new FileInputStream(file);
+                    existingText = IOHelper.loadText(is);
+                    IOHelper.close(is);
+                } catch (FileNotFoundException e) {
+                    // ignore
+                } catch (IOException e) {
+                    throw new MojoExecutionException("Error loading " + 
outSpringBootFolder + "/spring-configuration-metadata.json file");
+                }
+            }
+        }
+        final String springBootExistingText = existingText;
+        final AtomicBoolean springBootChanged = new AtomicBoolean(existingText 
== null);
+
         ComponentCallback callback = (componentName, componentJavaType, 
componentDescription,
                                       name, type, javaType, description, 
defaultValue, deprecated) -> {
+
             // gather spring boot data
             // we want to use dash in the name
             String dash = camelCaseToDash(name);
@@ -172,6 +194,9 @@ public class GenerateMojo extends AbstractMainMojo {
                 if (!groupData.contains(group)) {
                     groupData.add(group);
                 }
+                if (springBootExistingText != null && 
!springBootExistingText.contains(group.getName())) {
+                    springBootChanged.set(true);
+                }
             }
 
             // check if we can do automatic autowire to complex singleton 
objects from classes in the classpath
@@ -199,7 +224,7 @@ public class GenerateMojo extends AbstractMainMojo {
                             getLog().debug("Autowire: " + key + "=" + value);
                             autowireData.add(new AutowireData(key, value));
 
-                            if (springBootEnabled) {
+                            if (springBootEnabled && springBootChanged.get()) {
                                 // gather additional spring boot data for this 
class
                                 // we dont have documentation or default values
                                 List<Method> setters = new ArrayList<>();
@@ -267,7 +292,9 @@ public class GenerateMojo extends AbstractMainMojo {
 
         // write the output files
         writeAutowireFile(autowireData);
-        writeSpringBootFile(propertyData, groupData);
+        if (springBootChanged.get()) {
+            writeSpringBootFile(propertyData, groupData);
+        }
     }
 
     protected void writeSpringBootFile(List<SpringBootPropertyData> 
propertyData, List<SpringBootGroupData> groupData) throws MojoFailureException {
@@ -338,19 +365,36 @@ public class GenerateMojo extends AbstractMainMojo {
         if (!autowireData.isEmpty()) {
             outAutowireFolder.mkdirs();
             File file = new File(outAutowireFolder, "autowire.properties");
-            try {
-                FileOutputStream fos = new FileOutputStream(file, false);
-                fos.write("# Generated by camel build tools\n".getBytes());
-                for (AutowireData data : autowireData) {
-                    fos.write(data.getKey().getBytes());
-                    fos.write('=');
-                    fos.write(data.getValue().getBytes());
-                    fos.write('\n');
+
+            String existingText = null;
+            if (file.exists()) {
+                try {
+                    InputStream is = new FileInputStream(file);
+                    existingText = IOHelper.loadText(is);
+                    IOHelper.close(is);
+                } catch (Exception e) {
+                    // ignore
+                }
+            }
+            final String autowireExisting = existingText;
+
+            // only write the file if its changed
+            boolean matches = autowireExisting != null && 
autowireData.stream().allMatch(d -> autowireExisting.contains(d.getKey() + "=" 
+ d.getValue()));
+            if (!matches) {
+                try {
+                    FileOutputStream fos = new FileOutputStream(file, false);
+                    fos.write("# Generated by camel build tools\n".getBytes());
+                    for (AutowireData data : autowireData) {
+                        fos.write(data.getKey().getBytes());
+                        fos.write('=');
+                        fos.write(data.getValue().getBytes());
+                        fos.write('\n');
+                    }
+                    IOHelper.close(fos);
+                    getLog().info("Created file: " + file + " (autowire by 
classpath: " + autowireData.size() + ")");
+                } catch (Throwable e) {
+                    throw new MojoFailureException("Cannot write to file " + 
file + " due " + e.getMessage(), e);
                 }
-                IOHelper.close(fos);
-                getLog().info("Created file: " + file + " (autowire by 
classpath: " + autowireData.size() + ")");
-            } catch (Throwable e) {
-                throw new MojoFailureException("Cannot write to file " + file 
+ " due " + e.getMessage(), e);
             }
         }
     }

Reply via email to