This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit a78c904d5f98bae95a4c1ff3badd693928372752 Author: Guillaume Nodet <gno...@gmail.com> AuthorDate: Fri Dec 20 18:06:00 2019 +0100 Remove spring-boot specific mojos --- .../packaging/PrepareCatalogSpringBootMojo.java | 717 --------------------- .../camel/maven/packaging/SpringBootHelper.java | 74 --- ...pdateSpringBootAutoConfigurationReadmeMojo.java | 400 ------------ 3 files changed, 1191 deletions(-) diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCatalogSpringBootMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCatalogSpringBootMojo.java deleted file mode 100644 index 6b0c33f..0000000 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCatalogSpringBootMojo.java +++ /dev/null @@ -1,717 +0,0 @@ -/* - * 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.packaging; - -import java.io.File; -import java.io.FileFilter; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.channels.FileChannel; -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; -import java.util.regex.Pattern; - -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.Component; -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 static org.apache.camel.maven.packaging.PackageHelper.loadText; - -/** - * Prepares the Spring Boot provider camel catalog to include component it supports - */ -@Mojo(name = "prepare-catalog-springboot", threadSafe = true) -public class PrepareCatalogSpringBootMojo extends AbstractMojo { - - public static final int BUFFER_SIZE = 128 * 1024; - - private static final Pattern ARTIFACT_PATTERN = Pattern.compile("\"artifactId\": \"camel-(.*)\""); - - /** - * The maven project. - */ - @Parameter(property = "project", required = true, readonly = true) - protected MavenProject project; - - /** - * The output directory for components catalog - */ - @Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/springboot/components") - protected File componentsOutDir; - - /** - * The output directory for dataformats catalog - */ - @Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/springboot/dataformats") - protected File dataFormatsOutDir; - - /** - * The output directory for languages catalog - */ - @Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/springboot/languages") - protected File languagesOutDir; - - /** - * The output directory for others catalog - * - */ - @Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/springboot/others") - protected File othersOutDir; - - /** - * The directory where all spring-boot starters are - * - */ - @Parameter(defaultValue = "${project.build.directory}/../../../platforms/spring-boot/components-starter") - protected File componentsStarterDir; - - /** - * The components directory where all the Apache Camel components are - * - */ - @Parameter(defaultValue = "${project.build.directory}/../../../components") - protected File componentsDir; - - /** - * The camel-core directory - */ - @Parameter(defaultValue = "${project.build.directory}/../../../core/camel-core-engine") - protected File coreDir; - - /** - * The camel-base directory - */ - @Parameter(defaultValue = "${project.build.directory}/../../../core/camel-base") - protected File baseDir; - - /** - * Maven ProjectHelper. - */ - @Component - private MavenProjectHelper projectHelper; - - /** - * Execute goal. - * - * @throws MojoExecutionException execution of the main class or one of the - * threads it generated failed. - * @throws MojoFailureException something bad happened... - */ - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - Set<String> starters = findSpringBootStarters(); - executeComponents(starters); - executeDataFormats(starters); - executeLanguages(starters); - executeOthers(starters); - } - - protected void executeComponents(Set<String> starters) throws MojoExecutionException, MojoFailureException { - getLog().info("Copying all Camel component json descriptors"); - - // lets use sorted set/maps - Set<File> jsonFiles = new TreeSet<>(); - Set<File> componentFiles = new TreeSet<>(); - - // find all json files in components and camel-core - if (componentsDir != null && componentsDir.isDirectory()) { - File[] components = componentsDir.listFiles(); - if (components != null) { - for (File dir : components) { - if (dir.isDirectory() && !"target".equals(dir.getName())) { - File target = new File(dir, "target/classes"); - - // the directory must be in the list of known features - if (!starters.contains(dir.getName())) { - continue; - } - - // special for some which is in a sub dir - if ("camel-as2".equals(dir.getName())) { - target = new File(dir, "camel-as2-component/target/classes"); - } else if ("camel-box".equals(dir.getName())) { - target = new File(dir, "camel-box-component/target/classes"); - } else if ("camel-salesforce".equals(dir.getName())) { - target = new File(dir, "camel-salesforce-component/target/classes"); - } else if ("camel-servicenow".equals(dir.getName())) { - target = new File(dir, "camel-servicenow-component/target/classes"); - } else { - // this module must be active with a source folder - File src = new File(dir, "src"); - boolean active = src.isDirectory() && src.exists(); - if (!active) { - continue; - } - } - - - findComponentFilesRecursive(target, jsonFiles, componentFiles, new CamelComponentsFileFilter()); - } - } - } - } - if (coreDir != null && coreDir.isDirectory()) { - File target = new File(coreDir, "target/classes"); - findComponentFilesRecursive(target, jsonFiles, componentFiles, new CamelComponentsFileFilter()); - } - - getLog().info("Found " + componentFiles.size() + " component.properties files"); - getLog().info("Found " + jsonFiles.size() + " component json files"); - - // make sure to create out dir - componentsOutDir.mkdirs(); - - for (File file : jsonFiles) { - // for spring-boot we need to amend the json file to use -starter as the artifact-id - try { - String text = loadText(new FileInputStream(file)); - - text = ARTIFACT_PATTERN.matcher(text).replaceFirst("\"artifactId\": \"camel-$1-starter\""); - - // write new json file - File to = new File(componentsOutDir, file.getName()); - FileOutputStream fos = new FileOutputStream(to, false); - - fos.write(text.getBytes()); - - fos.close(); - - } catch (IOException e) { - throw new MojoFailureException("Cannot write json file " + file, e); - } - } - - File all = new File(componentsOutDir, "../components.properties"); - try { - FileOutputStream fos = new FileOutputStream(all, false); - - String[] names = componentsOutDir.list(); - List<String> components = new ArrayList<>(); - // sort the names - for (String name : names) { - if (name.endsWith(".json")) { - // strip out .json from the name - String componentName = name.substring(0, name.length() - 5); - components.add(componentName); - } - } - - Collections.sort(components); - for (String name : components) { - fos.write(name.getBytes()); - fos.write("\n".getBytes()); - } - - fos.close(); - - } catch (IOException e) { - throw new MojoFailureException("Error writing to file " + all); - } - } - - protected void executeDataFormats(Set<String> starters) throws MojoExecutionException, MojoFailureException { - getLog().info("Copying all Camel dataformat json descriptors"); - - // lets use sorted set/maps - Set<File> jsonFiles = new TreeSet<>(); - Set<File> dataFormatFiles = new TreeSet<>(); - - // find all data formats from the components directory - if (componentsDir != null && componentsDir.isDirectory()) { - File[] dataFormats = componentsDir.listFiles(); - if (dataFormats != null) { - for (File dir : dataFormats) { - if (dir.isDirectory() && !"target".equals(dir.getName())) { - // the directory must be in the list of known starters - if (!starters.contains(dir.getName())) { - continue; - } - // this module must be active with a source folder - File src = new File(dir, "src"); - boolean active = src.isDirectory() && src.exists(); - if (!active) { - continue; - } - File target = new File(dir, "target/classes"); - findDataFormatFilesRecursive(target, jsonFiles, dataFormatFiles, new CamelDataFormatsFileFilter()); - } - } - } - } - if (coreDir != null && coreDir.isDirectory()) { - File target = new File(coreDir, "target/classes"); - findDataFormatFilesRecursive(target, jsonFiles, dataFormatFiles, new CamelDataFormatsFileFilter()); - } - - getLog().info("Found " + dataFormatFiles.size() + " dataformat.properties files"); - getLog().info("Found " + jsonFiles.size() + " dataformat json files"); - - // make sure to create out dir - dataFormatsOutDir.mkdirs(); - - for (File file : jsonFiles) { - // for spring-boot we need to amend the json file to use -starter as the artifact-id - try { - String text = loadText(new FileInputStream(file)); - - text = ARTIFACT_PATTERN.matcher(text).replaceFirst("\"artifactId\": \"camel-$1-starter\""); - - // write new json file - File to = new File(dataFormatsOutDir, file.getName()); - FileOutputStream fos = new FileOutputStream(to, false); - - fos.write(text.getBytes()); - - fos.close(); - - } catch (IOException e) { - throw new MojoFailureException("Cannot write json file " + file, e); - } - } - - File all = new File(dataFormatsOutDir, "../dataformats.properties"); - try { - FileOutputStream fos = new FileOutputStream(all, false); - - String[] names = dataFormatsOutDir.list(); - List<String> dataFormats = new ArrayList<>(); - // sort the names - for (String name : names) { - if (name.endsWith(".json")) { - // strip out .json from the name - String dataFormatName = name.substring(0, name.length() - 5); - dataFormats.add(dataFormatName); - } - } - - Collections.sort(dataFormats); - for (String name : dataFormats) { - fos.write(name.getBytes()); - fos.write("\n".getBytes()); - } - - fos.close(); - - } catch (IOException e) { - throw new MojoFailureException("Error writing to file " + all); - } - } - - protected void executeLanguages(Set<String> starters) throws MojoExecutionException, MojoFailureException { - getLog().info("Copying all Camel language json descriptors"); - - // lets use sorted set/maps - Set<File> jsonFiles = new TreeSet<>(); - Set<File> languageFiles = new TreeSet<>(); - - // find all languages from the components directory - if (componentsDir != null && componentsDir.isDirectory()) { - File[] languages = componentsDir.listFiles(); - if (languages != null) { - for (File dir : languages) { - // the directory must be in the list of known starters - if (!starters.contains(dir.getName())) { - continue; - } - // this module must be active with a source folder - File src = new File(dir, "src"); - boolean active = src.isDirectory() && src.exists(); - if (!active) { - continue; - } - if (dir.isDirectory() && !"target".equals(dir.getName())) { - File target = new File(dir, "target/classes"); - findLanguageFilesRecursive(target, jsonFiles, languageFiles, new CamelLanguagesFileFilter()); - } - } - } - } - if (baseDir != null && baseDir.isDirectory()) { - File target = new File(baseDir, "target/classes"); - findLanguageFilesRecursive(target, jsonFiles, languageFiles, new CamelLanguagesFileFilter()); - // also look in camel-jaxp - target = new File(coreDir, "../camel-jaxp/target/classes"); - findLanguageFilesRecursive(target, jsonFiles, languageFiles, new CamelLanguagesFileFilter()); - } - - getLog().info("Found " + languageFiles.size() + " language.properties files"); - getLog().info("Found " + jsonFiles.size() + " language json files"); - - // make sure to create out dir - languagesOutDir.mkdirs(); - - for (File file : jsonFiles) { - // for spring-boot we need to amend the json file to use -starter as the artifact-id - try { - String text = loadText(new FileInputStream(file)); - - text = ARTIFACT_PATTERN.matcher(text).replaceFirst("\"artifactId\": \"camel-$1-starter\""); - - // write new json file - File to = new File(languagesOutDir, file.getName()); - FileOutputStream fos = new FileOutputStream(to, false); - - fos.write(text.getBytes()); - - fos.close(); - - } catch (IOException e) { - throw new MojoFailureException("Cannot write json file " + file, e); - } - } - - File all = new File(languagesOutDir, "../languages.properties"); - try { - FileOutputStream fos = new FileOutputStream(all, false); - - String[] names = languagesOutDir.list(); - List<String> languages = new ArrayList<>(); - // sort the names - for (String name : names) { - if (name.endsWith(".json")) { - // strip out .json from the name - String languageName = name.substring(0, name.length() - 5); - languages.add(languageName); - } - } - - Collections.sort(languages); - for (String name : languages) { - fos.write(name.getBytes()); - fos.write("\n".getBytes()); - } - - fos.close(); - - } catch (IOException e) { - throw new MojoFailureException("Error writing to file " + all); - } - } - - protected void executeOthers(Set<String> starters) throws MojoExecutionException, MojoFailureException { - getLog().info("Copying all Camel other json descriptors"); - - // lets use sorted set/maps - Set<File> jsonFiles = new TreeSet<>(); - Set<File> otherFiles = new TreeSet<>(); - - // find all other from the components directory - if (componentsDir != null && componentsDir.isDirectory()) { - File[] others = componentsDir.listFiles(); - if (others != null) { - for (File dir : others) { - // the directory must be in the list of known starters - if (!starters.contains(dir.getName())) { - continue; - } - - // skip these special cases - boolean special = "camel-core-osgi".equals(dir.getName()) - || "camel-core-xml".equals(dir.getName()) - || "camel-http-base".equals(dir.getName()) - || "camel-http-common".equals(dir.getName()) - || "camel-jetty-common".equals(dir.getName()); - boolean special2 = "camel-as2".equals(dir.getName()) - || "camel-box".equals(dir.getName()) - || "camel-olingo2".equals(dir.getName()) - || "camel-olingo4".equals(dir.getName()) - || "camel-servicenow".equals(dir.getName()) - || "camel-salesforce".equals(dir.getName()); - boolean special3 = "camel-debezium-common".equals(dir.getName()); - if (special || special2 || special3) { - continue; - } - - // this module must be active with a source folder - File src = new File(dir, "src"); - boolean active = src.isDirectory() && src.exists(); - if (!active) { - continue; - } - - if (dir.isDirectory() && !"target".equals(dir.getName())) { - File target = new File(dir, "target/classes"); - findOtherFilesRecursive(target, jsonFiles, otherFiles, new CamelOthersFileFilter()); - } - } - } - } - - getLog().info("Found " + otherFiles.size() + " other.properties files"); - getLog().info("Found " + jsonFiles.size() + " other json files"); - - // make sure to create out dir - othersOutDir.mkdirs(); - - for (File file : jsonFiles) { - // for spring-boot we need to amend the json file to use -starter as the artifact-id - try { - String text = loadText(new FileInputStream(file)); - - text = ARTIFACT_PATTERN.matcher(text).replaceFirst("\"artifactId\": \"camel-$1-starter\""); - - // write new json file - File to = new File(othersOutDir, file.getName()); - FileOutputStream fos = new FileOutputStream(to, false); - - fos.write(text.getBytes()); - - fos.close(); - - } catch (IOException e) { - throw new MojoFailureException("Cannot write json file " + file, e); - } - } - - File all = new File(othersOutDir, "../others.properties"); - try { - FileOutputStream fos = new FileOutputStream(all, false); - - String[] names = othersOutDir.list(); - List<String> others = new ArrayList<>(); - // sort the names - for (String name : names) { - if (name.endsWith(".json")) { - // strip out .json from the name - String otherName = name.substring(0, name.length() - 5); - others.add(otherName); - } - } - - Collections.sort(others); - for (String name : others) { - fos.write(name.getBytes()); - fos.write("\n".getBytes()); - } - - fos.close(); - - } catch (IOException e) { - throw new MojoFailureException("Error writing to file " + all); - } - } - - private void findComponentFilesRecursive(File dir, Set<File> found, Set<File> components, FileFilter filter) { - File[] files = dir.listFiles(filter); - if (files != null) { - for (File file : files) { - // skip files in root dirs as Camel does not store information there but others may do - boolean rootDir = "classes".equals(dir.getName()) || "META-INF".equals(dir.getName()); - boolean jsonFile = !rootDir && file.isFile() && file.getName().endsWith(".json"); - boolean componentFile = !rootDir && file.isFile() && file.getName().equals("component.properties"); - if (jsonFile) { - found.add(file); - } else if (componentFile) { - components.add(file); - } else if (file.isDirectory()) { - findComponentFilesRecursive(file, found, components, filter); - } - } - } - } - - private void findDataFormatFilesRecursive(File dir, Set<File> found, Set<File> dataFormats, FileFilter filter) { - File[] files = dir.listFiles(filter); - if (files != null) { - for (File file : files) { - // skip files in root dirs as Camel does not store information there but others may do - boolean rootDir = "classes".equals(dir.getName()) || "META-INF".equals(dir.getName()); - boolean jsonFile = !rootDir && file.isFile() && file.getName().endsWith(".json"); - boolean dataFormatFile = !rootDir && file.isFile() && file.getName().equals("dataformat.properties"); - if (jsonFile) { - found.add(file); - } else if (dataFormatFile) { - dataFormats.add(file); - } else if (file.isDirectory()) { - findDataFormatFilesRecursive(file, found, dataFormats, filter); - } - } - } - } - - private void findLanguageFilesRecursive(File dir, Set<File> found, Set<File> languages, FileFilter filter) { - File[] files = dir.listFiles(filter); - if (files != null) { - for (File file : files) { - // skip files in root dirs as Camel does not store information there but others may do - boolean rootDir = "classes".equals(dir.getName()) || "META-INF".equals(dir.getName()); - boolean jsonFile = !rootDir && file.isFile() && file.getName().endsWith(".json"); - boolean languageFile = !rootDir && file.isFile() && file.getName().equals("language.properties"); - if (jsonFile) { - found.add(file); - } else if (languageFile) { - languages.add(file); - } else if (file.isDirectory()) { - findLanguageFilesRecursive(file, found, languages, filter); - } - } - } - } - - private void findOtherFilesRecursive(File dir, Set<File> found, Set<File> others, FileFilter filter) { - File[] files = dir.listFiles(filter); - if (files != null) { - for (File file : files) { - // skip files in root dirs as Camel does not store information there but others may do - boolean rootDir = "classes".equals(dir.getName()) || "META-INF".equals(dir.getName()); - boolean jsonFile = rootDir && file.isFile() && file.getName().endsWith(".json"); - boolean otherFile = !rootDir && file.isFile() && file.getName().equals("other.properties"); - if (jsonFile) { - found.add(file); - } else if (otherFile) { - others.add(file); - } else if (file.isDirectory()) { - findOtherFilesRecursive(file, found, others, filter); - } - } - } - } - - private class CamelComponentsFileFilter implements FileFilter { - - @Override - public boolean accept(File pathname) { - if (pathname.isDirectory() && pathname.getName().equals("model")) { - // do not check the camel-core model packages as there is no components there - return false; - } - if (pathname.isFile() && pathname.getName().endsWith(".json")) { - // must be a components json file - try { - String json = loadText(new FileInputStream(pathname)); - return json != null && json.contains("\"kind\": \"component\""); - } catch (IOException e) { - // ignore - } - } - return pathname.isDirectory() || (pathname.isFile() && pathname.getName().equals("component.properties")); - } - } - - private class CamelDataFormatsFileFilter implements FileFilter { - - @Override - public boolean accept(File pathname) { - if (pathname.isDirectory() && pathname.getName().equals("model")) { - // do not check the camel-core model packages as there is no components there - return false; - } - if (pathname.isFile() && pathname.getName().endsWith(".json")) { - // must be a dataformat json file - try { - String json = loadText(new FileInputStream(pathname)); - return json != null && json.contains("\"kind\": \"dataformat\""); - } catch (IOException e) { - // ignore - } - } - return pathname.isDirectory() || (pathname.isFile() && pathname.getName().equals("dataformat.properties")); - } - } - - private class CamelLanguagesFileFilter implements FileFilter { - - @Override - public boolean accept(File pathname) { - if (pathname.isDirectory() && pathname.getName().equals("model")) { - // do not check the camel-core model packages as there is no components there - return false; - } - if (pathname.isFile() && pathname.getName().endsWith(".json")) { - // must be a language json file - try { - String json = loadText(new FileInputStream(pathname)); - return json != null && json.contains("\"kind\": \"language\""); - } catch (IOException e) { - // ignore - } - } - return pathname.isDirectory() || (pathname.isFile() && pathname.getName().equals("language.properties")); - } - } - - private class CamelOthersFileFilter implements FileFilter { - - @Override - public boolean accept(File pathname) { - if (pathname.isFile() && pathname.getName().endsWith(".json")) { - // must be a language json file - try { - String json = loadText(new FileInputStream(pathname)); - return json != null && json.contains("\"kind\": \"other\""); - } catch (IOException e) { - // ignore - } - } - return pathname.isDirectory() || (pathname.isFile() && pathname.getName().equals("other.properties")); - } - } - - public static void copyFile(File from, File to) throws IOException { - FileChannel in = null; - FileChannel out = null; - try (FileInputStream fis = new FileInputStream(from); FileOutputStream fos = new FileOutputStream(to)) { - try { - in = fis.getChannel(); - out = fos.getChannel(); - - long size = in.size(); - long position = 0; - while (position < size) { - position += in.transferTo(position, BUFFER_SIZE, out); - } - } finally { - if (in != null) { - in.close(); - } - if (out != null) { - out.close(); - } - } - } - } - - private Set<String> findSpringBootStarters() { - Set<String> answer = new LinkedHashSet<>(); - - String[] names = componentsStarterDir.list(); - if (names != null) { - for (String name : names) { - if (name.startsWith("camel-") && name.endsWith("-starter")) { - // remove ending -starter - String regular = name.substring(0, name.length() - 8); - answer.add(regular); - } - } - } - - getLog().info("Found " + answer.size() + " Camel Spring Boot starters from: " + componentsStarterDir); - - return answer; - } - -} diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootHelper.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootHelper.java deleted file mode 100644 index ac65159..0000000 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootHelper.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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.packaging; - -import java.io.File; -import java.io.IOException; - -/** - * Useful methods for spring-boot mojos. - */ -public final class SpringBootHelper { - - public static final String STARTER_SUFFIX = "-starter"; - - private SpringBootHelper() { - } - - public static File starterSrcDir(File baseDir, String artifactId) { - return new File(starterDir(baseDir, artifactId), "src/main/java"); - } - - public static File starterResourceDir(File baseDir, String artifactId) { - return new File(starterDir(baseDir, artifactId), "src/main/resources"); - } - - public static File starterDir(File baseDir, String artifactId) { - String starterName = artifactId + STARTER_SUFFIX; - - File allStartersDir = allStartersDir(baseDir); - File starterDir = new File(allStartersDir, starterName); - return starterDir; - } - - public static File allStartersDir(File baseDir) { - File allStartersDir = new File(camelProjectRoot(baseDir, "platforms"), "platforms/spring-boot/components-starter"); - return allStartersDir; - } - - public static File camelProjectRoot(File baseDir, String expectedDirName) { - // another solution could be to look for pom.xml file and see if that pom.xml is the camel root pom - // however looking for a dir named components-starter should be fine also (there is only 1 with such name) - try { - File root = baseDir.getCanonicalFile(); - while (root != null) { - File[] names = root.listFiles(pathname -> pathname.getName().equals(expectedDirName)); - if (names != null && names.length == 1) { - break; - } - root = root.getParentFile(); - } - - if (root == null) { - throw new IllegalStateException("Cannot find Apache Camel project root directory"); - } - return root; - } catch (IOException e) { - throw new IllegalStateException("Error while getting directory", e); - } - } -} diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/UpdateSpringBootAutoConfigurationReadmeMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/UpdateSpringBootAutoConfigurationReadmeMojo.java deleted file mode 100644 index 7c5dc0f..0000000 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/UpdateSpringBootAutoConfigurationReadmeMojo.java +++ /dev/null @@ -1,400 +0,0 @@ -/* - * 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.packaging; - -import java.io.File; -import java.io.FileFilter; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import java.util.stream.Collectors; - -import org.apache.camel.maven.packaging.model.SpringBootAutoConfigureOptionModel; -import org.apache.camel.maven.packaging.model.SpringBootModel; -import org.apache.camel.util.json.DeserializationException; -import org.apache.camel.util.json.JsonArray; -import org.apache.camel.util.json.JsonObject; -import org.apache.camel.util.json.Jsoner; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.Component; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; -import org.mvel2.templates.TemplateRuntime; -import org.sonatype.plexus.build.incremental.BuildContext; - -import static org.apache.camel.maven.packaging.PackageHelper.loadText; -import static org.apache.camel.maven.packaging.PackageHelper.writeText; - -/** - * For all the Camel components that has Spring Boot starter JAR, their documentation - * .adoc files in their component directory is updated to include spring boot auto configuration options. - */ -@Mojo(name = "update-spring-boot-auto-configuration-readme", threadSafe = true) -public class UpdateSpringBootAutoConfigurationReadmeMojo extends AbstractMojo { - - /** - * The maven project. - */ - @Parameter(property = "project", required = true, readonly = true) - protected MavenProject project; - - /** - * The project build directory - * - */ - @Parameter(defaultValue = "${project.build.directory}") - protected File buildDir; - - /** - * The documentation directory - * - */ - @Parameter(defaultValue = "${basedir}/../../../../components/") - protected File componentsDir; - - /** - * Whether to fail the build fast if any Warnings was detected. - */ - @Parameter - protected Boolean failFast; - - /** - * Whether to fail if an option has no documentation. - */ - @Parameter - protected Boolean failOnMissingDescription; - - /** - * build context to check changed files and mark them for refresh (used for - * m2e compatibility) - */ - @Component - private BuildContext buildContext; - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - try { - executeStarter(project.getBasedir()); - } catch (Exception e) { - throw new MojoFailureException("Error processing spring-configuration-metadata.json", e); - } - } - - private void executeStarter(File starter) throws Exception { - File jsonFile = new File(buildDir, "classes/META-INF/spring-configuration-metadata.json"); - - // only if there is components we should update the documentation files - if (jsonFile.exists()) { - getLog().debug("Processing Spring Boot auto-configuration file: " + jsonFile); - Object js = Jsoner.deserialize(new FileReader(jsonFile)); - if (js != null) { - String name = starter.getName(); - - if (!isValidStarter(name)) { - return; - } - - File compDir = getComponentsDir(name); - - File[] docFiles; - File docFolder; - String componentName; - if ("camel-spring-boot".equals(name)) { - // special for camel-spring-boot where we also want to auto-generate the options in the adoc file - componentName = "spring-boot"; - docFolder = new File(compDir, "/src/main/docs/"); - docFiles = docFolder.listFiles(new ComponentDocFilter(componentName)); - } else if ("camel-univocity-parsers-starter".equals(name)) { - // special for univocity-parsers - componentName = "univocity"; - docFolder = new File(compDir, "camel-univocity-parsers/src/main/docs/"); - docFiles = docFolder.listFiles(new ComponentDocFilter(componentName)); - } else { - // skip camel- and -starter in the end - componentName = name.substring(6, name.length() - 8); - getLog().debug("Camel component: " + componentName); - docFolder = new File(compDir, "camel-" + componentName + "/src/main/docs/"); - docFiles = docFolder.listFiles(new ComponentDocFilter(componentName)); - - // maybe its one of those component that has subfolders with -api and -component - if (docFiles == null || docFiles.length == 0) { - docFolder = new File(compDir, "camel-" + componentName + "/camel-" + componentName + "-component/src/main/docs/"); - docFiles = docFolder.listFiles(new ComponentDocFilter(componentName)); - } - } - - if (docFiles != null && docFiles.length > 0) { - List<File> files = Arrays.asList(docFiles); - - // find out if the JAR has a Camel component, dataformat, or language - boolean hasComponentDataFormatOrLanguage = files.stream().anyMatch( - f -> f.getName().endsWith("-component.adoc") || f.getName().endsWith("-dataformat.adoc") || f.getName().endsWith("-language.adoc")); - - // if so then skip the root adoc file as its just a introduction to the others - if (hasComponentDataFormatOrLanguage) { - files = Arrays.stream(docFiles).filter(f -> !f.getName().equals(componentName + ".adoc")).collect(Collectors.toList()); - } - - if (files.size() == 1) { - List<SpringBootAutoConfigureOptionModel> models = parseSpringBootAutoConfigureModels(jsonFile, null); - - // special for other kind of JARs that is not a regular Camel component,dataformat,language - boolean onlyOther = files.size() == 1 && !hasComponentDataFormatOrLanguage; - if (models.isEmpty() && onlyOther) { - // there are no spring-boot auto configuration for this other kind of JAR so lets just ignore this - return; - } - File docFile = files.get(0); - - // check for missing description on options - boolean noDescription = false; - for (SpringBootAutoConfigureOptionModel o : models) { - if (StringHelper.isEmpty(o.getDescription())) { - noDescription = true; - getLog().warn("Option " + o.getName() + " has no description"); - } - } - if (noDescription && isFailOnNoDescription()) { - throw new MojoExecutionException("Failed build due failOnMissingDescription=true"); - } - - String changed = templateAutoConfigurationOptions(models, componentName); - boolean updated = updateAutoConfigureOptions(docFile, changed); - if (updated) { - getLog().info("Updated doc file: " + docFile); - } else { - getLog().debug("No changes to doc file: " + docFile); - } - } else if (files.size() > 1) { - // when we have 2 or more files we need to filter the model options accordingly - for (File docFile : files) { - String docName = docFile.getName(); - int pos = docName.lastIndexOf("-"); - // spring-boot use lower cased keys - String prefix = pos > 0 ? docName.substring(0, pos).toLowerCase(Locale.US) : null; - - List<SpringBootAutoConfigureOptionModel> models = parseSpringBootAutoConfigureModels(jsonFile, prefix); - - // check for missing description on options - boolean noDescription = false; - for (SpringBootAutoConfigureOptionModel o : models) { - if (StringHelper.isEmpty(o.getDescription())) { - noDescription = true; - getLog().warn("Option " + o.getName() + " has no description"); - } - } - if (noDescription && isFailOnNoDescription()) { - throw new MojoExecutionException("Failed build due failOnMissingDescription=true"); - } - - String changed = templateAutoConfigurationOptions(models, componentName); - boolean updated = updateAutoConfigureOptions(docFile, changed); - if (updated) { - getLog().info("Updated doc file: " + docFile); - } else { - getLog().debug("No changes to doc file: " + docFile); - } - } - } - } else { - getLog().warn("No component docs found in folder: " + docFolder); - if (isFailFast()) { - throw new MojoExecutionException("Failed build due failFast=true"); - } - } - } - } - } - - private File getComponentsDir(String name) { - if ("camel-spring-boot".equals(name)) { - // special for camel-spring-boot - return project.getBasedir(); - } else { - return componentsDir; - } - } - - private static final class ComponentDocFilter implements FileFilter { - - private final String componentName; - - public ComponentDocFilter(String componentName) { - this.componentName = asComponentName(componentName); - } - - @Override - public boolean accept(File pathname) { - String name = pathname.getName(); - return name.startsWith(componentName) && name.endsWith(".adoc"); - } - } - - private static String asComponentName(String componentName) { - if ("fastjson".equals(componentName)) { - return "json-fastjson"; - } else if ("gson".equals(componentName)) { - return "json-gson"; - } else if ("jackson".equals(componentName)) { - return "json-jackson"; - } else if ("johnzon".equals(componentName)) { - return "json-johnzon"; - } else if ("snakeyaml".equals(componentName)) { - return "yaml-snakeyaml"; - } else if ("cassandraql".equals(componentName)) { - return "cql"; - } else if ("josql".equals(componentName)) { - return "sql"; - } else if ("juel".equals(componentName)) { - return "el"; - } else if ("jsch".equals(componentName)) { - return "scp"; - } else if ("printer".equals(componentName)) { - return "lpr"; - } else if ("saxon".equals(componentName)) { - return "xquery"; - } else if ("stringtemplate".equals(componentName)) { - return "string-template"; - } else if ("tagsoup".equals(componentName)) { - return "tidyMarkup"; - } - return componentName; - } - - private static boolean isValidStarter(String name) { - // skip these - if ("camel-core-starter".equals(name)) { - return false; - } - return true; - } - - private List<SpringBootAutoConfigureOptionModel> parseSpringBootAutoConfigureModels(File file, String include) throws IOException, DeserializationException { - getLog().debug("Parsing Spring Boot AutoConfigureModel using include: " + include); - List<SpringBootAutoConfigureOptionModel> answer = new ArrayList<>(); - - JsonObject obj = (JsonObject) Jsoner.deserialize(new FileReader(file)); - - JsonArray arr = obj.getCollection("properties"); - if (arr != null && !arr.isEmpty()) { - arr.forEach(e -> { - JsonObject row = (JsonObject) e; - String name = row.getString("name"); - String javaType = row.getString("type"); - String desc = row.getStringOrDefault("description", ""); - String defaultValue = row.getStringOrDefault("defaultValue", ""); - - // is the option deprecated then include that as well in the description - String deprecated = row.getStringOrDefault("deprecated", ""); - String deprecationNote = row.getStringOrDefault("deprecationNote", ""); - if ("true".equals(deprecated)) { - desc = "*Deprecated* " + desc; - if (!StringHelper.isEmpty(deprecationNote)) { - if (!desc.endsWith(".")) { - desc = desc + ". Deprecation note: " + deprecationNote; - } else { - desc = desc + " Deprecation note: " + deprecationNote; - } - } - } - - // skip this special option and also if not matching the filter - boolean skip = name.endsWith("customizer.enabled") || include != null && !name.contains("." + include + "."); - if (!skip) { - SpringBootAutoConfigureOptionModel model = new SpringBootAutoConfigureOptionModel(); - model.setName(name); - model.setJavaType(javaType); - model.setDefaultValue(defaultValue); - model.setDescription(desc); - answer.add(model); - } - }); - } - - return answer; - } - - private boolean updateAutoConfigureOptions(File file, String changed) throws MojoExecutionException { - if (!file.exists()) { - return false; - } - - try { - String text = loadText(new FileInputStream(file)); - - String existing = StringHelper.between(text, "// spring-boot-auto-configure options: START", "// spring-boot-auto-configure options: END"); - if (existing != null) { - // remove leading line breaks etc - existing = existing.trim(); - changed = changed.trim(); - if (existing.equals(changed)) { - return false; - } else { - String before = StringHelper.before(text, "// spring-boot-auto-configure options: START"); - String after = StringHelper.after(text, "// spring-boot-auto-configure options: END"); - text = before + "// spring-boot-auto-configure options: START\n" + changed + "\n// spring-boot-auto-configure options: END" + after; - writeText(file, text); - return true; - } - } else { - getLog().warn("Cannot find markers in file " + file); - getLog().warn("Add the following markers"); - getLog().warn("\t// spring-boot-auto-configure options: START"); - getLog().warn("\t// spring-boot-auto-configure options: END"); - if (isFailFast()) { - throw new MojoExecutionException("Failed build due failFast=true"); - } - return false; - } - } catch (Exception e) { - throw new MojoExecutionException("Error reading file " + file + " Reason: " + e, e); - } - } - - private String templateAutoConfigurationOptions(List<SpringBootAutoConfigureOptionModel> options, String componentName) throws MojoExecutionException { - SpringBootModel model = new SpringBootModel(); - model.setGroupId(project.getGroupId()); - model.setArtifactId("camel-" + componentName + "-starter"); - model.setVersion(project.getVersion()); - model.setOptions(options); - - try { - String template = loadText(UpdateSpringBootAutoConfigurationReadmeMojo.class.getClassLoader().getResourceAsStream("spring-boot-auto-configure-options.mvel")); - String out = (String) TemplateRuntime.eval(template, model, Collections.singletonMap("util", MvelHelper.INSTANCE)); - return out; - } catch (Exception e) { - throw new MojoExecutionException("Error processing mvel template. Reason: " + e, e); - } - } - - private boolean isFailFast() { - return failFast != null && failFast; - } - - private boolean isFailOnNoDescription() { - return failOnMissingDescription != null && failOnMissingDescription; - } - -}