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 2021/12/21 15:35:41 UTC
[camel] 07/08: CAMEL-17194: componentdsl - Prepare for generate only once during project build.
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
commit 2b30163cb2f7f9589637fe86664e42193a505840
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Dec 21 16:10:41 2021 +0100
CAMEL-17194: componentdsl - Prepare for generate only once during project build.
---
dsl/camel-componentdsl/pom.xml | 52 ++++++++++
.../camel/maven/packaging/ComponentDslMojo.java | 110 +++++++--------------
.../camel/maven/packaging/EndpointDslMojo.java | 2 +-
3 files changed, 88 insertions(+), 76 deletions(-)
diff --git a/dsl/camel-componentdsl/pom.xml b/dsl/camel-componentdsl/pom.xml
index 691a8cc..3ee756e 100644
--- a/dsl/camel-componentdsl/pom.xml
+++ b/dsl/camel-componentdsl/pom.xml
@@ -100,4 +100,56 @@
</dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-package-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>generate-component-dsl</id>
+ <goals>
+ <goal>generate-component-dsl</goal>
+ </goals>
+ <phase>generate-sources</phase>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>build-helper-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <phase>initialize</phase>
+ <goals>
+ <goal>add-source</goal>
+ <goal>add-resource</goal>
+ </goals>
+ <configuration>
+ <sources>
+ <source>src/generated/java</source>
+ </sources>
+ <resources>
+ <resource>
+ <directory>src/generated/resources</directory>
+ </resource>
+ </resources>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <!-- The non generated code depends on the generated code,
+ so skip the first compilation phase -->
+ <artifactId>maven-compiler-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>default-compile</id>
+ <phase>none</phase>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
</project>
\ No newline at end of file
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ComponentDslMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ComponentDslMojo.java
index 6c708d6..08a9c8a 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ComponentDslMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ComponentDslMojo.java
@@ -17,13 +17,10 @@
package org.apache.camel.maven.packaging;
import java.io.File;
-import java.io.IOError;
-import java.io.IOException;
import java.io.InputStream;
-import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.ArrayList;
import java.util.Comparator;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -31,15 +28,15 @@ import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
-import java.util.function.Supplier;
import java.util.stream.Collectors;
-import java.util.stream.Stream;
import org.apache.camel.maven.packaging.dsl.component.ComponentDslBuilderFactoryGenerator;
import org.apache.camel.maven.packaging.dsl.component.ComponentsBuilderFactoryGenerator;
import org.apache.camel.maven.packaging.dsl.component.ComponentsDslMetadataRegistry;
+import org.apache.camel.tooling.model.BaseModel;
import org.apache.camel.tooling.model.ComponentModel;
import org.apache.camel.tooling.model.JsonMapper;
+import org.apache.camel.tooling.util.PackageHelper;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -50,6 +47,7 @@ import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.sonatype.plexus.build.incremental.BuildContext;
+import static org.apache.camel.maven.packaging.MojoHelper.getComponentPath;
import static org.apache.camel.tooling.util.PackageHelper.findCamelDirectory;
import static org.apache.camel.tooling.util.PackageHelper.loadText;
@@ -104,6 +102,12 @@ public class ComponentDslMojo extends AbstractGeneratorMojo {
@Parameter(defaultValue = "org.apache.camel.builder.component.dsl")
protected String componentsDslFactoriesPackageName;
+ /**
+ * The components directory where all the Apache Camel components are
+ */
+ @Parameter(defaultValue = "${project.build.directory}/../../../components")
+ protected File componentsDir;
+
@Override
public void execute(MavenProject project, MavenProjectHelper projectHelper, BuildContext buildContext)
throws MojoFailureException, MojoExecutionException {
@@ -132,40 +136,41 @@ public class ComponentDslMojo extends AbstractGeneratorMojo {
componentsMetadata = outputResourcesDir.toPath().resolve("metadata.json").toFile();
}
- Map<File, Supplier<String>> files;
-
- try (Stream<Path> pathStream = Files.find(buildDir.toPath(), Integer.MAX_VALUE, super::isJsonFile)) {
- files = pathStream.collect(Collectors.toMap(Path::toFile, s -> cache(() -> loadJson(s.toFile()))));
- } catch (IOException e) {
- throw new RuntimeException(e.getMessage(), e);
+ List<ComponentModel> models = new ArrayList<>();
+
+ for (File dir : componentsDir.listFiles()) {
+ List<Path> subs = getComponentPath(dir.toPath());
+ for (Path sub : subs) {
+ sub = sub.resolve("src/generated/resources/");
+ PackageHelper.walk(sub).forEach(p -> {
+ String f = p.getFileName().toString();
+ if (f.endsWith(PackageHelper.JSON_SUFIX)) {
+ try {
+ BaseModel<?> model = JsonMapper.generateModel(p);
+ if (model instanceof ComponentModel) {
+ models.add((ComponentModel) model);
+ }
+ } catch (Exception e) {
+ // ignore as its not a camel model
+ }
+ }
+ });
+ }
}
+ models.sort((o1, o2) -> o1.getScheme().compareToIgnoreCase(o2.getScheme()));
Lock lock = LOCKS.computeIfAbsent(root, d -> new ReentrantLock());
lock.lock();
try {
- executeComponent(files);
+ executeComponent(models);
} finally {
lock.unlock();
}
}
- private void executeComponent(Map<File, Supplier<String>> jsonFiles) throws MojoExecutionException, MojoFailureException {
- // find the component names
- Set<String> componentNames = new TreeSet<>();
- findComponentNames(buildDir, componentNames);
-
- // create auto configuration for the components
- if (!componentNames.isEmpty()) {
- getLog().debug("Found " + componentNames.size() + " components");
-
- List<ComponentModel> allModels = new LinkedList<>();
- for (String componentName : componentNames) {
- String json = loadComponentJson(jsonFiles, componentName);
- if (json != null) {
- ComponentModel model = JsonMapper.generateComponentModel(json);
- allModels.add(model);
- }
- }
+ private void executeComponent(List<ComponentModel> allModels) throws MojoExecutionException, MojoFailureException {
+ if (!allModels.isEmpty()) {
+ getLog().debug("Found " + allModels.size() + " components");
// Group the models by implementing classes
Map<String, List<ComponentModel>> grModels
@@ -239,51 +244,6 @@ public class ComponentDslMojo extends AbstractGeneratorMojo {
}
}
- protected static String loadJson(File file) {
- try {
- return loadText(file);
- } catch (IOException e) {
- throw new IOError(e);
- }
- }
-
- protected static String loadComponentJson(Map<File, Supplier<String>> jsonFiles, String componentName) {
- return loadJsonOfType(jsonFiles, componentName, "component");
- }
-
- protected static String loadJsonOfType(Map<File, Supplier<String>> jsonFiles, String modelName, String type) {
- for (Map.Entry<File, Supplier<String>> entry : jsonFiles.entrySet()) {
- if (entry.getKey().getName().equals(modelName + ".json")) {
- String json = entry.getValue().get();
- if (json.contains("\"kind\": \"" + type + "\"")) {
- return json;
- }
- }
- }
- return null;
- }
-
- protected void findComponentNames(File dir, Set<String> componentNames) {
- File f = new File(dir, "classes/META-INF/services/org/apache/camel/component");
-
- if (f.exists() && f.isDirectory()) {
- File[] files = f.listFiles();
- if (files != null) {
- for (File file : files) {
- // skip directories as there may be a sub .resolver
- // directory
- if (file.isDirectory()) {
- continue;
- }
- String name = file.getName();
- if (name.charAt(0) != '.') {
- componentNames.add(name);
- }
- }
- }
- }
- }
-
protected boolean writeSourceIfChanged(String source, String filePath, String fileName, File outputDir)
throws MojoFailureException {
Path target = outputDir.toPath().resolve(filePath).resolve(fileName);
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointDslMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointDslMojo.java
index a82c663..1534970 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointDslMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointDslMojo.java
@@ -172,7 +172,7 @@ public class EndpointDslMojo extends AbstractGeneratorMojo {
});
}
}
- Collections.sort(models, (o1, o2) -> o1.getScheme().compareToIgnoreCase(o2.getScheme()));
+ models.sort((o1, o2) -> o1.getScheme().compareToIgnoreCase(o2.getScheme()));
// generate component endpoint DSL files and write them
Lock lock = LOCKS.computeIfAbsent(root, d -> new ReentrantLock());