You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2023/06/05 14:20:40 UTC

[camel] 01/01: Adapt camel-package-maven-plugin to incremental build

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

nfilotto pushed a commit to branch camel-package-maven-plugin-compatible-wthin-incremental-build
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 93d0082a10fa5f195e25f053c4b19da8eaa09612
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Mon Jun 5 09:59:59 2023 +0200

    Adapt camel-package-maven-plugin to incremental build
---
 .../camel/maven/packaging/PrepareCatalogMojo.java  | 71 +++++++++++++++++++++-
 1 file changed, 70 insertions(+), 1 deletion(-)

diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCatalogMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCatalogMojo.java
index c47d8f8c01d..aa03cd9dfbc 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCatalogMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCatalogMojo.java
@@ -18,6 +18,9 @@ package org.apache.camel.maven.packaging;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.URI;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.attribute.BasicFileAttributes;
@@ -56,6 +59,13 @@ 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.eclipse.aether.RepositorySystem;
+import org.eclipse.aether.RepositorySystemSession;
+import org.eclipse.aether.artifact.Artifact;
+import org.eclipse.aether.artifact.DefaultArtifact;
+import org.eclipse.aether.repository.RemoteRepository;
+import org.eclipse.aether.resolution.ArtifactRequest;
+import org.eclipse.aether.resolution.ArtifactResult;
 
 import static org.apache.camel.maven.packaging.MojoHelper.getComponentPath;
 import static org.apache.camel.tooling.util.PackageHelper.loadText;
@@ -183,6 +193,15 @@ public class PrepareCatalogMojo extends AbstractMojo {
     @Component
     protected MavenProjectHelper projectHelper;
 
+    @Component
+    private RepositorySystem repoSystem;
+
+    @Parameter(defaultValue = "${repositorySystemSession}", readonly = true, required = true)
+    private RepositorySystemSession repoSession;
+
+    @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true)
+    private List<RemoteRepository> repositories;
+
     private Collection<Path> allJsonFiles;
     private Collection<Path> allPropertiesFiles;
     private final Map<Path, BaseModel<?>> allModels = new HashMap<>();
@@ -197,6 +216,56 @@ public class PrepareCatalogMojo extends AbstractMojo {
         return name;
     }
 
+    /**
+     * Gives the {@code target/classes} directory if it already exists, otherwise downloads the corresponding artifact
+     * and unzip its content into the {@code target/classes} directory to support incremental build.
+     * @param componentRootDirectory the root directory of a given component
+     * @return the path to the {@code target/classes} directory.
+     */
+    private Path getComponentClasses(Path componentRootDirectory) {
+        Path result = componentRootDirectory.resolve("target/classes");
+        if (Files.exists(result)) {
+            return result;
+        }
+        String artifactId = componentRootDirectory.getFileName().toString();
+        Artifact aetherArtifact = new DefaultArtifact("org.apache.camel", artifactId, "jar", project.getVersion());
+        ArtifactRequest req = new ArtifactRequest().setRepositories(this.repositories).setArtifact(aetherArtifact);
+        try {
+            ArtifactResult resolutionResult = this.repoSystem.resolveArtifact(this.repoSession, req);
+            File file = resolutionResult.getArtifact().getFile();
+            if (file != null && file.exists()) {
+                Path targetRoot = result.normalize();
+                try (FileSystem fs = FileSystems.newFileSystem(URI.create("jar:" + file.toURI()), Map.of())) {
+                    for (Path root : fs.getRootDirectories()) {
+                        try (Stream<Path> walk = Files.walk(root)) {
+                            walk.forEach(
+                                    source -> {
+                                        Path target = targetRoot.resolve(root.relativize(source).toString()).normalize();
+                                        if (target.startsWith(targetRoot)) {
+                                            try {
+                                                if (Files.isDirectory(source)) {
+                                                    Files.createDirectories(target);
+                                                } else {
+                                                    Files.copy(source, target);
+                                                }
+                                            } catch (IOException e) {
+                                                getLog().warn("Artifact " + artifactId
+                                                              + " could not unzip jar into the target directory.",
+                                                        e);
+                                            }
+                                        }
+                                    });
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            getLog().warn("Artifact " + artifactId + " could not be resolved.", e);
+        }
+
+        return result;
+    }
+
     /**
      * Execute goal.
      *
@@ -217,7 +286,7 @@ public class PrepareCatalogMojo extends AbstractMojo {
                          .filter(dir -> !"target".equals(dir.getFileName().toString()))
                          .flatMap(p -> getComponentPath(p).stream())
                          .filter(dir -> Files.isDirectory(dir.resolve("src")))
-                         .map(p -> p.resolve("target/classes"))
+                         .map(this::getComponentClasses)
                          .flatMap(PackageHelper::walk)
                          .filter(Files::isRegularFile)) {
                 stream