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 e095aaf8c3cf Add apiSrcDir option to maven tooling that generates code
fromApis that should load source code from another folder than its own
classpath. (#21298)
e095aaf8c3cf is described below
commit e095aaf8c3cff9fb64425bfef19e006ab144e124
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Feb 6 16:37:14 2026 +0100
Add apiSrcDir option to maven tooling that generates code fromApis that
should load source code from another folder than its own classpath. (#21298)
---
components/camel-fhir/camel-fhir-component/pom.xml | 1 +
.../camel/maven/ApiComponentGeneratorMojo.java | 8 ++
.../java/org/apache/camel/maven/HashHelper.java | 3 +
.../maven/JavaSourceApiMethodGeneratorMojo.java | 103 ++++++++++++---------
4 files changed, 71 insertions(+), 44 deletions(-)
diff --git a/components/camel-fhir/camel-fhir-component/pom.xml
b/components/camel-fhir/camel-fhir-component/pom.xml
index 3e25aaac6d38..659102791fc0 100644
--- a/components/camel-fhir/camel-fhir-component/pom.xml
+++ b/components/camel-fhir/camel-fhir-component/pom.xml
@@ -191,6 +191,7 @@
<goal>fromApis</goal>
</goals>
<configuration>
+
<apiSrcDir>../camel-fhir-api/src/main/java</apiSrcDir>
<apis>
<api>
<apiName>capabilities</apiName>
diff --git
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
index 45d2eaa24521..5cc69df433d0 100644
---
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
+++
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
@@ -82,6 +82,12 @@ public class ApiComponentGeneratorMojo extends
AbstractApiMethodBaseMojo {
@Parameter(defaultValue = "${project.build.directory}")
private File cachedir;
+ /**
+ * To use a file system for the api source code location, instead of
attempting to load from classpath -source JARs
+ */
+ @Parameter
+ protected File apiSrcDir;
+
/**
* Names of options that can be set to null value if not specified.
*/
@@ -108,6 +114,7 @@ public class ApiComponentGeneratorMojo extends
AbstractApiMethodBaseMojo {
String newHash = new HashHelper()
.hash("ApiComponentGeneratorMojo")
.hash("apis", apis)
+ .hash("apiSrcDir", apiSrcDir)
.hash("fromJavasource", fromJavasource)
.hash("nullableOptions", nullableOptions)
.hash("aliases", aliases)
@@ -278,6 +285,7 @@ public class ApiComponentGeneratorMojo extends
AbstractApiMethodBaseMojo {
? apiFromJavasource.getIncludeSetters() :
fromJavasource.getIncludeSetters();
mojo.aliases = api.getAliases().isEmpty() ? aliases :
api.getAliases();
mojo.nullableOptions = api.getNullableOptions() != null ?
api.getNullableOptions() : nullableOptions;
+ mojo.apiSrcDir = apiSrcDir;
apiMethodGenerator = mojo;
}
diff --git
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/HashHelper.java
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/HashHelper.java
index 4a966543376f..39e044b22a03 100644
---
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/HashHelper.java
+++
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/HashHelper.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.maven;
+import java.io.File;
import java.util.Arrays;
import com.google.common.hash.Hasher;
@@ -39,6 +40,8 @@ public class HashHelper {
hasher.putUnencodedChars((String) f);
} else if (f instanceof Boolean) {
hasher.putBoolean((Boolean) f);
+ } else if (f instanceof File file) {
+ hasher.putUnencodedChars(file.getAbsolutePath());
} else if (f instanceof Iterable) {
for (Object a : (Iterable<?>) f) {
hash(a);
diff --git
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceApiMethodGeneratorMojo.java
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceApiMethodGeneratorMojo.java
index 2a42fd7472dd..4caaaf7c0ed8 100644
---
a/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceApiMethodGeneratorMojo.java
+++
b/tooling/maven/camel-api-component-maven-plugin/src/main/java/org/apache/camel/maven/JavaSourceApiMethodGeneratorMojo.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.maven;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -23,6 +25,7 @@ import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
+import org.apache.camel.util.IOHelper;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
@@ -61,6 +64,9 @@ public class JavaSourceApiMethodGeneratorMojo extends
AbstractApiMethodGenerator
@Parameter(property = PREFIX + "includeSetters")
protected Boolean includeSetters;
+ @Parameter
+ protected File apiSrcDir;
+
@Override
public List<SignatureModel> getSignatureList() throws
MojoExecutionException {
// signatures as map from signature with no arg names to arg names
from JavadocParser
@@ -89,60 +95,69 @@ public class JavaSourceApiMethodGeneratorMojo extends
AbstractApiMethodGenerator
// read source java text for class
log.debug("Loading source: {}", sourcePath);
- JavaSourceParser parser;
- try (InputStream inputStream =
getProjectClassLoader().getResourceAsStream(sourcePath)) {
- if (inputStream == null) {
- log.debug("Java source not found on classpath for {}",
aClass.getName());
- break;
- }
+ JavaSourceParser parser = null;
- parser = new JavaSourceParser(projectClassLoader);
- parser.parse(inputStream, nestedClass, includeSetters != null
&& includeSetters);
+ InputStream inputStream =
getProjectClassLoader().getResourceAsStream(sourcePath);
+ if (inputStream == null) {
+ log.debug("Java source not found on classpath for {}",
aClass.getName());
+ }
+ try {
+ if (inputStream == null && apiSrcDir != null) {
+ log.debug("Using file location for source: {}", apiSrcDir);
+ inputStream = new FileInputStream(new File(apiSrcDir,
sourcePath));
+ }
+ if (inputStream != null) {
+ parser = new JavaSourceParser(projectClassLoader);
+ parser.parse(inputStream, nestedClass, includeSetters !=
null && includeSetters);
+ }
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
+ } finally {
+ IOHelper.close(inputStream);
}
- // look for parse errors
- final String parseError = parser.getErrorMessage();
- if (parseError != null) {
- throw new MojoExecutionException(parseError);
- }
+ if (parser != null) {
+ // look for parse errors
+ final String parseError = parser.getErrorMessage();
+ if (parseError != null) {
+ throw new MojoExecutionException(parseError);
+ }
- // get public method signature
- for (String method : parser.getMethodSignatures()) {
- if (!result.containsKey(method)
- && (includeMethodPatterns == null ||
includeMethodPatterns.matcher(method).find())
- && (excludeMethodPatterns == null ||
!excludeMethodPatterns.matcher(method).find())) {
-
- String signature = method;
- method = method.replace("public ", "");
- int whitespace = method.indexOf(' ');
- int leftBracket = method.indexOf('(');
- String name = method.substring(whitespace + 1,
leftBracket);
-
- SignatureModel model = new SignatureModel();
- model.setSignature(method);
- model.setApiDescription(parser.getClassDoc());
-
model.setMethodDescription(parser.getMethodDocs().get(name));
-
model.setParameterDescriptions(parser.getParameterDocs().get(name));
-
model.setParameterTypes(parser.getParameterTypes().get(signature));
-
- // include getter/setters
- if (includeSetters != null && includeSetters &&
!parser.getSetterMethods().isEmpty()) {
- var m = parser.getSetterMethods().get(signature);
- var d = parser.getSetterDocs().get(signature);
- if (getLog().isDebugEnabled()) {
- getLog().debug("There are " + m.size() + "
optional properties as setters for: " + name);
- getLog().debug("" + m);
+ // get public method signature
+ for (String method : parser.getMethodSignatures()) {
+ if (!result.containsKey(method)
+ && (includeMethodPatterns == null ||
includeMethodPatterns.matcher(method).find())
+ && (excludeMethodPatterns == null ||
!excludeMethodPatterns.matcher(method).find())) {
+
+ String signature = method;
+ method = method.replace("public ", "");
+ int whitespace = method.indexOf(' ');
+ int leftBracket = method.indexOf('(');
+ String name = method.substring(whitespace + 1,
leftBracket);
+
+ SignatureModel model = new SignatureModel();
+ model.setSignature(method);
+ model.setApiDescription(parser.getClassDoc());
+
model.setMethodDescription(parser.getMethodDocs().get(name));
+
model.setParameterDescriptions(parser.getParameterDocs().get(name));
+
model.setParameterTypes(parser.getParameterTypes().get(signature));
+
+ // include getter/setters
+ if (includeSetters != null && includeSetters &&
!parser.getSetterMethods().isEmpty()) {
+ var m = parser.getSetterMethods().get(signature);
+ var d = parser.getSetterDocs().get(signature);
+ if (getLog().isDebugEnabled()) {
+ getLog().debug("There are " + m.size() + "
optional properties as setters for: " + name);
+ getLog().debug("" + m);
+ }
+ model.setSetterTypes(m);
+ model.setSetterDescriptions(d);
}
- model.setSetterTypes(m);
- model.setSetterDescriptions(d);
- }
- result.put(method, model);
+ result.put(method, model);
+ }
}
}
-
}
if (result.isEmpty()) {