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/15 10:42:30 UTC

[camel] 02/02: CAMEL-13055: generate legal can be done a bit later in the maven lifecycle.

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 f3c62a82400184f36ed1dbb0de5c358bf9605171
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jan 15 11:14:51 2019 +0100

    CAMEL-13055: generate legal can be done a bit later in the maven lifecycle.
---
 camel-core/pom.xml                                 |  5 +-
 components/pom.xml                                 |  9 +--
 .../camel/maven/packaging/PackageLegalMojo.java    | 93 ++++++----------------
 3 files changed, 29 insertions(+), 78 deletions(-)

diff --git a/camel-core/pom.xml b/camel-core/pom.xml
index 9eae541..1da0224 100644
--- a/camel-core/pom.xml
+++ b/camel-core/pom.xml
@@ -438,7 +438,6 @@
               <goal>generate-components-list</goal>
               <goal>generate-dataformats-list</goal>
               <goal>generate-languages-list</goal>
-              <goal>generate-legal</goal>
             </goals>
             <phase>process-classes</phase>
           </execution>
@@ -448,6 +447,7 @@
               <goal>validate-components</goal>
               <goal>prepare-spring-boot-starter</goal>
               <goal>prepare-spring-boot-auto-configuration</goal>
+              <goal>generate-legal</goal>
             </goals>
             <phase>prepare-package</phase>
           </execution>
@@ -486,9 +486,6 @@
                 <resource>
                   <directory>${basedir}/target/generated/camel/jaxb</directory>
                 </resource>
-                <resource>
-                  <directory>${basedir}/target/generated/camel/legal</directory>
-                </resource>
               </resources>
             </configuration>
           </execution>
diff --git a/components/pom.xml b/components/pom.xml
index c255ba7..09646c3 100644
--- a/components/pom.xml
+++ b/components/pom.xml
@@ -313,8 +313,7 @@
           <execution>
             <id>generate</id>
             <goals>
-              <goal>prepare-components</goal>
-              <goal>generate-legal</goal>
+              <goal>prepare-components</goal>             
             </goals>
             <phase>process-classes</phase>
           </execution>
@@ -324,13 +323,14 @@
               <goal>validate-components</goal>
               <goal>prepare-spring-boot-starter</goal>
               <goal>prepare-spring-boot-auto-configuration</goal>
+              <goal>generate-legal</goal>
             </goals>
             <phase>prepare-package</phase>
           </execution>
           <execution>
             <id>readme</id>
             <goals>
-              <goal>update-readme</goal>
+              <goal>update-readme</goal>             
             </goals>
             <phase>package</phase>
           </execution>
@@ -359,9 +359,6 @@
                 <resource>
                   <directory>${basedir}/target/generated/camel/others</directory>
                 </resource>
-                <resource>
-                  <directory>${basedir}/target/generated/camel/legal</directory>
-                </resource>
               </resources>
             </configuration>
           </execution>
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLegalMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLegalMojo.java
index 917d180..5cb3713 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLegalMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLegalMojo.java
@@ -17,19 +17,14 @@
 package org.apache.camel.maven.packaging;
 
 import java.io.File;
-import java.io.IOError;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.Writer;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.Collections;
-import java.util.Objects;
 
 import org.apache.commons.io.IOUtils;
-import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -39,12 +34,11 @@ import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectHelper;
-import org.sonatype.plexus.build.incremental.BuildContext;
 
 /**
  * Analyses the Camel plugins in a project and generates legal files.
  */
-@Mojo(name = "generate-legal", threadSafe = true, defaultPhase = LifecyclePhase.PROCESS_CLASSES)
+@Mojo(name = "generate-legal", threadSafe = true, defaultPhase = LifecyclePhase.PREPARE_PACKAGE)
 public class PackageLegalMojo extends AbstractMojo {
 
     /**
@@ -56,7 +50,7 @@ public class PackageLegalMojo extends AbstractMojo {
     /**
      * The output directory for generated components file
      */
-    @Parameter(defaultValue = "${project.build.directory}/generated/camel/legal")
+    @Parameter(defaultValue = "${project.build.directory}/classes")
     protected File legalOutDir;
 
     /**
@@ -66,13 +60,6 @@ public class PackageLegalMojo extends AbstractMojo {
     private MavenProjectHelper projectHelper;
 
     /**
-     * build context to check changed files and mark them for refresh (used for
-     * m2e compatibility)
-     */
-    @Component
-    private BuildContext buildContext;
-
-    /**
      * Execute goal.
      *
      * @throws MojoExecutionException execution of the main class or one of the
@@ -81,8 +68,6 @@ public class PackageLegalMojo extends AbstractMojo {
      */
     public void execute() throws MojoExecutionException, MojoFailureException {
         processLegal(legalOutDir.toPath());
-
-        projectHelper.addResource(project, legalOutDir.getPath(), Collections.singletonList("**/*"), Collections.emptyList());
     }
 
     public void processLegal(Path legalOutDir) throws MojoExecutionException {
@@ -91,62 +76,34 @@ public class PackageLegalMojo extends AbstractMojo {
             return;
         }
 
-        boolean hasLicense = project.getResources().stream()
-                .map(Resource::getDirectory)
-                .map(Paths::get)
-                .map(p -> p.resolve("META-INF").resolve("LICENSE.txt"))
-                .anyMatch(Files::isRegularFile);
-        if (!hasLicense) {
-            try (InputStream isLicense = getClass().getResourceAsStream("/camel-LICENSE.txt")) {
-                String license = IOUtils.toString(isLicense, StandardCharsets.UTF_8);
-                updateResource(legalOutDir.resolve("META-INF").resolve("LICENSE.txt"), license);
-            } catch (IOException e) {
-                throw new MojoExecutionException("Failed to write legal files. Reason: " + e, e);
-            }
-        }
-        boolean hasNotice = project.getResources().stream()
-                .map(Resource::getDirectory)
-                .map(Paths::get)
-                .map(p -> p.resolve("META-INF").resolve("NOTICE.txt"))
-                .anyMatch(Files::isRegularFile);
-        if (!hasNotice) {
-            try (InputStream isNotice = getClass().getResourceAsStream("/camel-NOTICE.txt")) {
-                String notice = IOUtils.toString(isNotice, StandardCharsets.UTF_8);
-                updateResource(legalOutDir.resolve("META-INF").resolve("NOTICE.txt"), notice);
-            } catch (IOException e) {
-                throw new MojoExecutionException("Failed to write legal files. Reason: " + e, e);
-            }
+        try (InputStream isLicense = getClass().getResourceAsStream("/camel-LICENSE.txt")) {
+            String license = IOUtils.toString(isLicense, StandardCharsets.UTF_8);
+            updateResource(legalOutDir.resolve("META-INF").resolve("LICENSE.txt"), license);
+        } catch (IOException e) {
+            throw new MojoExecutionException("Failed to write legal files. Reason: " + e, e);
         }
-    }
-
-    protected void updateResource(Path out, String data) {
-        try {
-            if (data == null) {
-                if (Files.isRegularFile(out)) {
-                    Files.delete(out);
-                    refresh(out);
-                }
-            } else {
-                if (Files.isRegularFile(out) && Files.isReadable(out)) {
-                    String content = new String(Files.readAllBytes(out), StandardCharsets.UTF_8);
-                    if (Objects.equals(content, data)) {
-                        return;
-                    }
-                }
-                Files.createDirectories(out.getParent());
-                try (Writer w = Files.newBufferedWriter(out, StandardCharsets.UTF_8)) {
-                    w.append(data);
-                }
-                refresh(out);
-            }
+        try (InputStream isNotice = getClass().getResourceAsStream("/camel-NOTICE.txt")) {
+            String notice = IOUtils.toString(isNotice, StandardCharsets.UTF_8);
+            updateResource(legalOutDir.resolve("META-INF").resolve("NOTICE.txt"), notice);
         } catch (IOException e) {
-            throw new IOError(e);
+            throw new MojoExecutionException("Failed to write legal files. Reason: " + e, e);
         }
     }
 
-    protected void refresh(Path file) {
-        if (buildContext != null) {
-            buildContext.refresh(file.toFile());
+    protected void updateResource(Path out, String data) throws IOException {
+        if (Files.isRegularFile(out)) {
+            // file already exists
+            return;
+        }
+        if (data == null) {
+            if (Files.isRegularFile(out)) {
+                Files.delete(out);
+            }
+        } else {
+            Files.createDirectories(out.getParent());
+            try (Writer w = Files.newBufferedWriter(out, StandardCharsets.UTF_8)) {
+                w.append(data);
+            }
         }
     }