in tooling/camel-kafka-connector-generator-maven-plugin/src/main/java/org/apache/camel/kafkaconnector/maven/GenerateCamelKafkaConnectorsMojo.java [175:381]
protected void executeAll() throws MojoExecutionException, IOException, ResourceNotFoundException, FileResourceCreationException {
// load some project version properties
final Properties properties = new Properties();
try (InputStream stream = new FileInputStream(rm.getResourceAsFile("project.properties"))) {
properties.load(stream);
}
Map<String, String> kameletsResources = new HashMap<>();
Set<String> camelComponentsUsedInKamelets = new HashSet<>();
List<String> resourceNames;
try (ScanResult scanResult = new ClassGraph().acceptPaths("/" + KAMELETS_DIR + "/").scan()) {
resourceNames = scanResult.getAllResources().getPaths();
}
for (String fileName: resourceNames) {
String pathInJar = "/" + fileName;
String kamelet = new BufferedReader(
new InputStreamReader(KameletsCatalog.class.getResourceAsStream(pathInJar), StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n"));
KameletModel kameletModel = YamlKameletMapper.parseKameletYaml(kamelet);
// filter all kamelets with type not in {source,sink} and not excluded
if (("source".equals(kameletModel.getType()) || "sink".equals(kameletModel.getType())) && !excludedKamelets.contains(kameletModel.getName())) {
kameletsResources.put(kameletModel.getName(), kamelet);
camelComponentsUsedInKamelets.addAll(
kameletModel.getDependencies().stream()
.filter(d -> d.startsWith("camel:"))
.map(d -> d.replaceFirst("camel:", ""))
.collect(Collectors.toSet())
);
}
//TODO: add include (filter)
}
getLog().info("Kamelets found to be used to generate/update a kafka connector: " + kameletsResources.keySet());
for (String kamelet : kameletsResources.keySet()) {
executeMojo(
plugin(
groupId(properties.getProperty("groupId")),
artifactId(properties.getProperty("artifactId")),
version(properties.getProperty("version"))
),
goal("camel-kafka-connector-kamelet-create"),
configuration(
element(name("name"), kamelet),
element(name("initialKameletPomTemplate"), initialKameletPomTemplate),
element(name("noticeTemplate"), noticeTemplate),
element(name("licenseTemplate"), licenseTemplate),
element(name("fixKameletDependenciesProperties"), fixKameletDependenciesProperties),
element(name("packageFileTemplate"), packageFileTemplate),
element(name("overridePomFile"), overridePomFile.toString()),
element(name("connectorsProjectName"), connectorsProjectName)
),
executionEnvironment(
project,
session,
pluginManager
)
);
executeMojo(
plugin(
groupId(properties.getProperty("groupId")),
artifactId(properties.getProperty("artifactId")),
version(properties.getProperty("version"))
),
goal("camel-kafka-connector-kamelet-update"),
configuration(
element(name("additionalDependencies"), additionalComponentDependencies),
element(name("name"), kamelet),
element(name("kameletYaml"), kameletsResources.get(kamelet)),
element(name("initialKameletPomTemplate"), initialKameletPomTemplate),
element(name("noticeTemplate"), noticeTemplate),
element(name("licenseTemplate"), licenseTemplate),
element(name("fixKameletDependenciesProperties"), fixKameletDependenciesProperties),
element(name("packageFileTemplate"), packageFileTemplate),
element(name("connectorsProjectName"), connectorsProjectName)
),
executionEnvironment(
project,
session,
pluginManager
)
);
}
CamelCatalog cc = new DefaultCamelCatalog();
List<String> components;
List<String> filteredComponents;
if (filter == null || filter.isEmpty()) {
components = cc.findComponentNames();
} else {
Set<String> filterComponentNames = new HashSet<>(Arrays.asList(filter.split(",")));
components = cc.findComponentNames().stream().filter(componentName -> filterComponentNames.contains(componentName)).collect(Collectors.toList());
}
// exclude all components used in a kamelet
camelComponentsUsedInKamelets.addAll(excludedComponents);
excludedComponents = camelComponentsUsedInKamelets.stream().collect(Collectors.toList());
if (excludedComponents == null || excludedComponents.isEmpty()) {
filteredComponents = components;
} else {
filteredComponents = components.stream().filter(component -> !excludedComponents.contains(component)).collect(Collectors.toList());
}
if (filter != null && !filter.isEmpty()) {
getLog().info("Filtered Components that will be used to generate a kafka connector: " + filter);
}
if (excludedComponents != null && !excludedComponents.isEmpty()) {
getLog().info("Excluded Components that won't be used to generate a kafka connector: " + excludedComponents);
}
getLog().info("Components found to be used to generate/update a kafka connector: " + filteredComponents);
for (String component : filteredComponents) {
String cJson = cc.componentJSonSchema(component);
executeMojo(
plugin(
groupId(properties.getProperty("groupId")),
artifactId(properties.getProperty("artifactId")),
version(properties.getProperty("version"))
),
goal("camel-kafka-connector-create"),
configuration(
element(name("name"), component),
element(name("componentJson"), cJson),
element(name("initialPomTemplate"), initialPomTemplate),
element(name("noticeTemplate"), noticeTemplate),
element(name("licenseTemplate"), licenseTemplate),
element(name("fixDependenciesProperties"), fixDependenciesProperties),
element(name("packageFileTemplate"), packageFileTemplate),
element(name("overridePomFile"), overridePomFile.toString()),
element(name("connectorsProjectName"), connectorsProjectName)
),
executionEnvironment(
project,
session,
pluginManager
)
);
executeMojo(
plugin(
groupId(properties.getProperty("groupId")),
artifactId(properties.getProperty("artifactId")),
version(properties.getProperty("version"))
),
goal("camel-kafka-connector-update"),
configuration(
element(name("additionalDependencies"), additionalComponentDependencies),
element(name("name"), component),
element(name("componentJson"), cJson),
element(name("initialPomTemplate"), initialPomTemplate),
element(name("noticeTemplate"), noticeTemplate),
element(name("licenseTemplate"), licenseTemplate),
element(name("fixDependenciesProperties"), fixDependenciesProperties),
element(name("packageFileTemplate"), packageFileTemplate),
element(name("connectorsProjectName"), connectorsProjectName)
),
executionEnvironment(
project,
session,
pluginManager
)
);
}
if (removeMissingComponents) {
if (projectDir != null && projectDir.isDirectory()) {
// sanitize names, as there are some camel components with + signal which are sanitized when creating the kafka connector
List<String> sanitizedGeneratedFromComponentsConnectorsNames = filteredComponents.stream().map(MavenUtils::sanitizeMavenArtifactId).collect(Collectors.toList());
List<String> sanitizedGeneratedFromKameletsConnectorsNames = kameletsResources.keySet().stream().map(MavenUtils::sanitizeMavenArtifactId).collect(Collectors.toList());
// retrieve the list of existing camel kafka connectors
String[] existingConnectorNames = projectDir.list((dir, filename) -> filename.endsWith(KAFKA_CONNECTORS_SUFFIX));
if (existingConnectorNames != null) {
List<String> connectorsToRemove = Stream.of(existingConnectorNames).sorted().filter(filename -> {
String componentName = extractComponentName(filename);
// set to remove connectors that are not generated from camel components or a kamelet and are not excluded in excludedConnectorsFromDeletion
return !sanitizedGeneratedFromComponentsConnectorsNames.contains(componentName)
&& !sanitizedGeneratedFromKameletsConnectorsNames.contains(componentName)
&& !excludedConnectorsFromDeletion.contains(componentName);
}).collect(Collectors.toList());
getLog().info("Connectors previously generated found to be removed: " + connectorsToRemove);
for (String connectorToBeRemoved: connectorsToRemove) {
executeMojo(
plugin(
groupId(properties.getProperty("groupId")),
artifactId(properties.getProperty("artifactId")),
version(properties.getProperty("version"))
),
goal("camel-kafka-connector-delete"),
configuration(
element(name("name"), connectorToBeRemoved),
element(name("connectorsProjectName"), connectorsProjectName)
),
executionEnvironment(
project,
session,
pluginManager
)
);
}
}
}
}
}