You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gn...@apache.org on 2025/07/03 12:45:41 UTC
(camel) branch main updated: Add camel-repackager-maven-plugin for self-executing JARs using Spring Boot loader (#18398)
This is an automated email from the ASF dual-hosted git repository.
gnodet 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 a33333526df Add camel-repackager-maven-plugin for self-executing JARs using Spring Boot loader (#18398)
a33333526df is described below
commit a33333526df988966247620205c7799597e4e343
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Thu Jul 3 14:45:34 2025 +0200
Add camel-repackager-maven-plugin for self-executing JARs using Spring Boot loader (#18398)
---
dsl/camel-jbang/camel-launcher/README.md | 38 ++++-
dsl/camel-jbang/camel-launcher/pom.xml | 45 +-----
.../maven/camel-repackager-maven-plugin/README.md | 90 ++++++++++++
.../maven/camel-repackager-maven-plugin/pom.xml | 107 ++++++++++++++
.../java/org/apache/camel/maven/RepackageMojo.java | 157 +++++++++++++++++++++
.../org/apache/camel/maven/RepackageMojoTest.java | 64 +++++++++
tooling/maven/pom.xml | 1 +
7 files changed, 460 insertions(+), 42 deletions(-)
diff --git a/dsl/camel-jbang/camel-launcher/README.md b/dsl/camel-jbang/camel-launcher/README.md
index c0a598d999e..7f1fada6b7d 100644
--- a/dsl/camel-jbang/camel-launcher/README.md
+++ b/dsl/camel-jbang/camel-launcher/README.md
@@ -1,7 +1,9 @@
-# Camel JBang Fat-Jar Launcher
+# Camel JBang Self-Executing Launcher
This module provides a self-contained executable JAR that includes all dependencies required to run Camel JBang without the need for the JBang two-step process.
+The launcher uses Spring Boot's loader tools to create a self-executing JAR with a nested structure, similar to Spring Boot's executable JARs. This provides better performance and avoids classpath conflicts compared to traditional fat JARs.
+
## Building
To build the fat-jar launcher:
@@ -11,7 +13,7 @@ mvn clean package
```
This will create:
-1. A fat-jar (`camel-launcher-<version>.jar`) in the `target` directory
+1. A self-executing JAR (`camel-launcher-<version>.jar`) in the `target` directory using Spring Boot's nested JAR structure
2. Distribution archives (`camel-launcher-<version>-bin.zip` and `camel-launcher-<version>-bin.tar.gz`) in the `target` directory
## Usage
@@ -51,6 +53,34 @@ java -jar camel-launcher-<version>.jar run hello.java
- No need for JBang installation
- Single executable JAR with all dependencies included
-- Faster startup time (no dependency resolution step)
-- Each fat-jar is its own release, avoiding version complexity
+- Faster startup time (no dependency resolution step, on-demand class loading)
+- Better memory usage (only loads classes that are actually used)
+- Avoids classpath conflicts (dependencies kept as separate JARs)
+- Each self-executing JAR is its own release, avoiding version complexity
- Can still be used with JBang if preferred
+
+## JAR Structure
+
+The launcher uses Spring Boot's nested JAR structure:
+
+```
+camel-launcher-<version>.jar
+ |
+ +-META-INF
+ | +-MANIFEST.MF (Main-Class: org.springframework.boot.loader.launch.JarLauncher)
+ +-org
+ | +-springframework
+ | +-boot
+ | +-loader
+ | +-<spring boot loader classes>
+ +-BOOT-INF
+ +-classes
+ | +-org/apache/camel/dsl/jbang/launcher/CamelLauncher.class
+ | +-<other application classes>
+ +-lib
+ +-camel-jbang-core-<version>.jar
+ +-camel-main-<version>.jar
+ +-<other dependency JARs>
+```
+
+This structure provides better performance and reliability compared to traditional fat JARs where all classes are merged together.
diff --git a/dsl/camel-jbang/camel-launcher/pom.xml b/dsl/camel-jbang/camel-launcher/pom.xml
index 34554c344b5..82bf575b9ee 100644
--- a/dsl/camel-jbang/camel-launcher/pom.xml
+++ b/dsl/camel-jbang/camel-launcher/pom.xml
@@ -239,51 +239,20 @@
<build>
<plugins>
+ <!-- Create self-executing JAR using Spring Boot loader tools -->
<plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-repackager-maven-plugin</artifactId>
+ <version>${project.version}</version>
<executions>
<execution>
+ <id>repackage-executable</id>
<phase>package</phase>
<goals>
- <goal>shade</goal>
+ <goal>repackage</goal>
</goals>
<configuration>
- <createDependencyReducedPom>false</createDependencyReducedPom>
- <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <mainClass>org.apache.camel.dsl.jbang.launcher.CamelLauncher</mainClass>
- <manifestEntries>
- <Multi-Release>true</Multi-Release>
- </manifestEntries>
- </transformer>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
- <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
- <resource>META-INF/services/org/apache/camel/TypeConverterLoader</resource>
- </transformer>
- <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
- <resource>META-INF/services/org/apache/camel/component.properties</resource>
- </transformer>
- <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
- <resource>META-INF/services/org/apache/camel/dataformat.properties</resource>
- </transformer>
- <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
- <resource>META-INF/services/org/apache/camel/language.properties</resource>
- </transformer>
- <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
- <resource>META-INF/services/org/apache/camel/camel-jbang-plugin</resource>
- </transformer>
- </transformers>
- <filters>
- <filter>
- <artifact>*:*</artifact>
- <excludes>
- <exclude>META-INF/*.SF</exclude>
- <exclude>META-INF/*.DSA</exclude>
- <exclude>META-INF/*.RSA</exclude>
- </excludes>
- </filter>
- </filters>
+ <mainClass>org.apache.camel.dsl.jbang.launcher.CamelLauncher</mainClass>
</configuration>
</execution>
</executions>
diff --git a/tooling/maven/camel-repackager-maven-plugin/README.md b/tooling/maven/camel-repackager-maven-plugin/README.md
new file mode 100644
index 00000000000..f48f5e721c9
--- /dev/null
+++ b/tooling/maven/camel-repackager-maven-plugin/README.md
@@ -0,0 +1,90 @@
+# Camel Repackager Maven Plugin
+
+This Maven plugin creates self-executing JARs using Spring Boot's loader tools, providing the same nested JAR structure that Spring Boot uses for its executable JARs.
+
+## Overview
+
+Instead of creating traditional "fat JARs" where all dependencies are unpacked and merged into a single JAR (like maven-shade-plugin does), this plugin creates JARs with Spring Boot's nested structure:
+
+```
+example.jar
+ |
+ +-META-INF
+ | +-MANIFEST.MF
+ +-org
+ | +-springframework
+ | +-boot
+ | +-loader
+ | +-<spring boot loader classes>
+ +-BOOT-INF
+ +-classes
+ | +-<your application classes>
+ +-lib
+ +-dependency1.jar
+ +-dependency2.jar
+```
+
+## Benefits
+
+- **Faster startup**: No need to unpack all dependencies
+- **Better memory usage**: Dependencies are loaded on-demand
+- **Avoids classpath conflicts**: Preserves original JAR signatures and avoids file conflicts
+- **Smaller memory footprint**: Only loads classes that are actually used
+- **Better debugging**: Easier to identify which JAR a class comes from
+
+## Usage
+
+Add the plugin to your `pom.xml`:
+
+```xml
+<plugin>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-repackager-maven-plugin</artifactId>
+ <version>${camel.version}</version>
+ <executions>
+ <execution>
+ <id>repackage-executable</id>
+ <phase>package</phase>
+ <goals>
+ <goal>repackage</goal>
+ </goals>
+ <configuration>
+ <mainClass>com.example.MyMainClass</mainClass>
+ </configuration>
+ </execution>
+ </executions>
+</plugin>
+```
+
+## Configuration
+
+| Parameter | Description | Default | Required |
+|-----------|-------------|---------|----------|
+| `mainClass` | The main class to use for the executable JAR | | Yes |
+| `sourceJar` | The source JAR file to repackage | `${project.build.directory}/${project.build.finalName}.jar` | No |
+| `outputDirectory` | The output directory for the repackaged JAR | `${project.build.directory}` | No |
+| `finalName` | The final name of the repackaged JAR (without extension) | `${project.build.finalName}` | No |
+| `backupSource` | Whether to backup the source JAR | `true` | No |
+
+## How it works
+
+1. The plugin takes your regular JAR file (containing just your application classes)
+2. Uses Spring Boot's `Repackager` class to create the nested structure
+3. Includes all compile and runtime dependencies as separate JARs in `BOOT-INF/lib/`
+4. Adds Spring Boot's loader classes to handle the nested JAR loading
+5. Sets up the manifest to use `JarLauncher` as the main class and your class as `Start-Class`
+
+## Comparison with maven-shade-plugin
+
+| Aspect | maven-shade-plugin | camel-repackager |
+|--------|-------------------|------------------------------|
+| JAR Structure | Single flat JAR with all classes | Nested JARs with Spring Boot loader |
+| Startup Time | Slower (all classes loaded) | Faster (on-demand loading) |
+| Memory Usage | Higher (all classes in memory) | Lower (only used classes loaded) |
+| Classpath Conflicts | Possible (overlapping files) | Avoided (separate JARs) |
+| Debugging | Harder (merged classes) | Easier (original JAR structure) |
+| File Size | Smaller (no duplication) | Slightly larger (loader overhead) |
+
+## Example
+
+See the Camel JBang launcher (`dsl/camel-jbang/camel-launcher`) for a real-world example of how this plugin is used.
diff --git a/tooling/maven/camel-repackager-maven-plugin/pom.xml b/tooling/maven/camel-repackager-maven-plugin/pom.xml
new file mode 100644
index 00000000000..7ba5f4df551
--- /dev/null
+++ b/tooling/maven/camel-repackager-maven-plugin/pom.xml
@@ -0,0 +1,107 @@
+<?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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>maven-plugins</artifactId>
+ <version>4.13.0-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+
+ <artifactId>camel-repackager-maven-plugin</artifactId>
+ <packaging>maven-plugin</packaging>
+
+ <name>Camel :: Maven Plugins :: Repackager</name>
+ <description>Maven plugin to create self-executing JARs using Spring Boot loader tools</description>
+
+ <properties>
+ <firstVersion>4.13.0</firstVersion>
+ <label>tooling</label>
+ <supportLevel>Preview</supportLevel>
+ <spring-boot-version>3.5.3</spring-boot-version>
+ </properties>
+
+ <dependencies>
+ <!-- Maven Plugin API -->
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>${maven-version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-core</artifactId>
+ <version>${maven-version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.plugin-tools</groupId>
+ <artifactId>maven-plugin-annotations</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <!-- Spring Boot Loader Tools -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-loader-tools</artifactId>
+ <version>${spring-boot-version}</version>
+ </dependency>
+
+ <!-- Test dependencies -->
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-plugin-plugin</artifactId>
+ <configuration>
+ <goalPrefix>camel-repackager</goalPrefix>
+ </configuration>
+ <executions>
+ <execution>
+ <id>default-descriptor</id>
+ <goals>
+ <goal>descriptor</goal>
+ </goals>
+ <phase>process-classes</phase>
+ </execution>
+ <execution>
+ <id>help-descriptor</id>
+ <goals>
+ <goal>helpmojo</goal>
+ </goals>
+ <phase>process-classes</phase>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/tooling/maven/camel-repackager-maven-plugin/src/main/java/org/apache/camel/maven/RepackageMojo.java b/tooling/maven/camel-repackager-maven-plugin/src/main/java/org/apache/camel/maven/RepackageMojo.java
new file mode 100644
index 00000000000..4dad4468f10
--- /dev/null
+++ b/tooling/maven/camel-repackager-maven-plugin/src/main/java/org/apache/camel/maven/RepackageMojo.java
@@ -0,0 +1,157 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.util.Set;
+
+import org.apache.maven.artifact.Artifact;
+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.plugins.annotations.ResolutionScope;
+import org.apache.maven.project.MavenProject;
+import org.springframework.boot.loader.tools.DefaultLaunchScript;
+import org.springframework.boot.loader.tools.LaunchScript;
+import org.springframework.boot.loader.tools.Library;
+import org.springframework.boot.loader.tools.LibraryCallback;
+import org.springframework.boot.loader.tools.LibraryScope;
+import org.springframework.boot.loader.tools.Repackager;
+
+/**
+ * Maven plugin to repackage a JAR using Spring Boot loader tools to create a self-executing JAR. This creates a JAR
+ * with the Spring Boot nested structure where dependencies are kept as separate JARs in BOOT-INF/lib/ and application
+ * classes are in BOOT-INF/classes/.
+ */
+@Mojo(name = "repackage", defaultPhase = LifecyclePhase.PACKAGE,
+ requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, threadSafe = true)
+public class RepackageMojo extends AbstractMojo {
+
+ /**
+ * The Maven project.
+ */
+ @Parameter(defaultValue = "${project}", readonly = true, required = true)
+ private MavenProject project;
+
+ /**
+ * The source JAR file to repackage. If not specified, uses the project's main artifact.
+ */
+ @Parameter
+ private File sourceJar;
+
+ /**
+ * The main class to use for the executable JAR.
+ */
+ @Parameter(required = true)
+ private String mainClass;
+
+ /**
+ * The output directory for the repackaged JAR.
+ */
+ @Parameter(defaultValue = "${project.build.directory}")
+ private File outputDirectory;
+
+ /**
+ * The final name of the repackaged JAR (without extension).
+ */
+ @Parameter(defaultValue = "${project.build.finalName}")
+ private String finalName;
+
+ /**
+ * Whether to backup the source JAR.
+ */
+ @Parameter(defaultValue = "true")
+ private boolean backupSource;
+
+ @Override
+ public void execute() throws MojoExecutionException, MojoFailureException {
+ try {
+ File sourceJarFile = getSourceJar();
+ if (!sourceJarFile.exists()) {
+ throw new MojoFailureException("Source JAR does not exist: " + sourceJarFile);
+ }
+
+ getLog().info("Repackaging " + sourceJarFile + " using Spring Boot loader tools");
+
+ Repackager repackager = new Repackager(sourceJarFile);
+ repackager.setBackupSource(backupSource);
+ repackager.setMainClass(mainClass);
+
+ LaunchScript launchScript = new DefaultLaunchScript(null, null);
+
+ File targetFile = getTargetFile();
+ repackager.repackage(targetFile, this::getLibraries, launchScript);
+
+ getLog().info("Successfully created self-executing JAR: " + targetFile);
+
+ } catch (IOException e) {
+ throw new MojoExecutionException("Failed to repackage JAR", e);
+ }
+ }
+
+ private File getSourceJar() {
+ if (sourceJar != null) {
+ return sourceJar;
+ }
+ return new File(outputDirectory, finalName + ".jar");
+ }
+
+ private File getTargetFile() {
+ return new File(outputDirectory, finalName + ".jar");
+ }
+
+ private void getLibraries(LibraryCallback callback) throws IOException {
+ // Include all runtime and compile dependencies as separate JARs
+ Set<Artifact> artifacts = project.getArtifacts();
+ for (Artifact artifact : artifacts) {
+ if (includeArtifact(artifact)) {
+ File file = artifact.getFile();
+ if (file != null && file.exists()) {
+ LibraryScope scope = getLibraryScope(artifact);
+ getLog().debug("Including dependency: " + artifact + " with scope: " + scope);
+ callback.library(new Library(file, scope));
+ }
+ }
+ }
+ }
+
+ private boolean includeArtifact(Artifact artifact) {
+ String scope = artifact.getScope();
+ // Include compile and runtime dependencies
+ return Artifact.SCOPE_COMPILE.equals(scope) ||
+ Artifact.SCOPE_RUNTIME.equals(scope) ||
+ (Artifact.SCOPE_PROVIDED.equals(scope) && artifact.getGroupId().startsWith("org.apache.camel"));
+ }
+
+ private LibraryScope getLibraryScope(Artifact artifact) {
+ String scope = artifact.getScope();
+ switch (scope) {
+ case Artifact.SCOPE_COMPILE:
+ return LibraryScope.COMPILE;
+ case Artifact.SCOPE_RUNTIME:
+ return LibraryScope.RUNTIME;
+ case Artifact.SCOPE_PROVIDED:
+ return LibraryScope.PROVIDED;
+ default:
+ return LibraryScope.COMPILE;
+ }
+ }
+}
diff --git a/tooling/maven/camel-repackager-maven-plugin/src/test/java/org/apache/camel/maven/RepackageMojoTest.java b/tooling/maven/camel-repackager-maven-plugin/src/test/java/org/apache/camel/maven/RepackageMojoTest.java
new file mode 100644
index 00000000000..279a8aeab42
--- /dev/null
+++ b/tooling/maven/camel-repackager-maven-plugin/src/test/java/org/apache/camel/maven/RepackageMojoTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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 java.io.File;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * Test for RepackageMojo to verify Spring Boot loader integration.
+ */
+public class RepackageMojoTest {
+
+ @TempDir
+ File tempDir;
+
+ @Test
+ public void testSpringBootLoaderStructure() throws Exception {
+ // This test would verify that the repackaged JAR has the correct Spring Boot structure
+ // For now, it's a placeholder to demonstrate the expected behavior
+
+ // Expected structure after repackaging:
+ // - META-INF/MANIFEST.MF with Main-Class: org.springframework.boot.loader.launch.JarLauncher
+ // - org/springframework/boot/loader/ classes
+ // - BOOT-INF/classes/ with application classes
+ // - BOOT-INF/lib/ with dependency JARs
+
+ assertTrue(true, "Placeholder test - would verify Spring Boot JAR structure");
+ }
+
+ @Test
+ public void testManifestEntries() throws Exception {
+ // This test would verify that the manifest has the correct entries:
+ // Main-Class: org.springframework.boot.loader.launch.JarLauncher
+ // Start-Class: org.apache.camel.dsl.jbang.launcher.CamelLauncher
+
+ assertTrue(true, "Placeholder test - would verify manifest entries");
+ }
+
+ @Test
+ public void testDependencyInclusion() throws Exception {
+ // This test would verify that all compile and runtime dependencies
+ // are included as separate JARs in BOOT-INF/lib/
+
+ assertTrue(true, "Placeholder test - would verify dependency inclusion");
+ }
+}
diff --git a/tooling/maven/pom.xml b/tooling/maven/pom.xml
index 5a64e87d474..cf6b0d862b5 100644
--- a/tooling/maven/pom.xml
+++ b/tooling/maven/pom.xml
@@ -42,6 +42,7 @@
<module>camel-restdsl-openapi-plugin</module>
<module>sync-properties-maven-plugin</module>
<module>camel-component-maven-plugin</module>
+ <module>camel-repackager-maven-plugin</module>
</modules>
<!-- Apply to children. -->