This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new fb410bb CAMEL-14503: Added generate-component for 3rd party Camel component developers fb410bb is described below commit fb410bb27247d6a0a5c1b650fd9d86bbaad70458 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Feb 6 12:45:23 2020 +0100 CAMEL-14503: Added generate-component for 3rd party Camel component developers --- ...GenerateMojo.java => AbstractGenerateMojo.java} | 45 +----- .../maven/packaging/GenerateComponentMojo.java | 47 ++++++ .../apache/camel/maven/packaging/GenerateMojo.java | 160 +-------------------- 3 files changed, 53 insertions(+), 199 deletions(-) diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGenerateMojo.java similarity index 80% copy from tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateMojo.java copy to tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGenerateMojo.java index b47d7e7..5e8c48d 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateMojo.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGenerateMojo.java @@ -35,20 +35,12 @@ 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.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.apache.maven.project.MavenProjectHelper; import org.sonatype.plexus.build.incremental.BuildContext; -/** - * - */ -@Mojo(name = "generate", threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, - defaultPhase = LifecyclePhase.PROCESS_CLASSES) -public class GenerateMojo extends AbstractMojo { +public abstract class AbstractGenerateMojo extends AbstractMojo { @Parameter(property = "project", required = true, readonly = true) protected MavenProject project; @@ -73,40 +65,9 @@ public class GenerateMojo extends AbstractMojo { } } - protected void doExecute() throws MojoFailureException, MojoExecutionException { - // jandex - invoke(PackageJandexMojo.class); - // generate-schema - invoke(SchemaGeneratorMojo.class); - // generate-type-converter-loader - invoke(TypeConverterLoaderGeneratorMojo.class); - // generate-spi - invoke(SpiGeneratorMojo.class); - // generate-jaxb-list - invoke(PackageJaxbMojo.class); - // generate-eips-list - invoke(PackageModelMojo.class); - // generate-endpoint-schema - invoke(EndpointSchemaGeneratorMojo.class); - // prepare-components - invoke(PrepareComponentMojo.class); - // prepare-main - invoke(PrepareCamelMainMojo.class); - // generate-xml-parser - invoke(ModelXmlParserGeneratorMojo.class); - // generate-legal - invoke(PackageLegalMojo.class); - // validate-components - invoke(ValidateComponentMojo.class); - // generate-endpoint-dsl - invoke(EndpointDslMojo.class); - // generate-component-dsl - invoke(ComponentDslMojo.class); - // update-readme - invoke(UpdateReadmeMojo.class); - } + protected abstract void doExecute() throws MojoFailureException, MojoExecutionException; - private void invoke(Class<? extends AbstractMojo> mojoClass) throws MojoExecutionException, MojoFailureException { + protected void invoke(Class<? extends AbstractMojo> mojoClass) throws MojoExecutionException, MojoFailureException { try { AbstractMojo mojo = mojoClass.newInstance(); mojo.setLog(getLog()); diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateComponentMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateComponentMojo.java new file mode 100644 index 0000000..fc9ffc8 --- /dev/null +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateComponentMojo.java @@ -0,0 +1,47 @@ +/* + * 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 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.ResolutionScope; + +/** + * To be used by 3rd party Camel component developers to generate metadata. + */ +@Mojo(name = "generate-component", threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, + defaultPhase = LifecyclePhase.PROCESS_CLASSES) +public class GenerateComponentMojo extends AbstractGenerateMojo { + + protected void doExecute() throws MojoFailureException, MojoExecutionException { + // jandex + invoke(PackageJandexMojo.class); + // generate-type-converter-loader + invoke(TypeConverterLoaderGeneratorMojo.class); + // generate-spi + invoke(SpiGeneratorMojo.class); + // generate-endpoint-schema + invoke(EndpointSchemaGeneratorMojo.class); + // prepare-components + invoke(PrepareComponentMojo.class); + // validate-components + invoke(ValidateComponentMojo.class); + } + +} diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateMojo.java index b47d7e7..dba8ac7 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateMojo.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateMojo.java @@ -16,62 +16,18 @@ */ package org.apache.camel.maven.packaging; -import java.io.File; -import java.io.IOError; -import java.io.IOException; -import java.io.Writer; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import java.util.zip.ZipFile; - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.execution.MavenSession; -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.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.apache.maven.project.MavenProjectHelper; -import org.sonatype.plexus.build.incremental.BuildContext; /** - * + * Used by Apache Camel project itself - do NOT use as end user. */ -@Mojo(name = "generate", threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, +@Mojo(name = "generate", threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, defaultPhase = LifecyclePhase.PROCESS_CLASSES) -public class GenerateMojo extends AbstractMojo { - - @Parameter(property = "project", required = true, readonly = true) - protected MavenProject project; - @Component - protected MavenProjectHelper projectHelper; - @Component - protected BuildContext buildContext; - @Component - private MavenSession session; - @Parameter(defaultValue = "${showStaleFiles}") - private boolean showStaleFiles; - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - try { - if (!isUpToDate(project)) { - doExecute(); - writeIncrementalInfo(project); - } - } catch (Exception e) { - throw new MojoFailureException("Error generating data " + e.toString(), e); - } - } +public class GenerateMojo extends AbstractGenerateMojo { protected void doExecute() throws MojoFailureException, MojoExecutionException { // jandex @@ -106,114 +62,4 @@ public class GenerateMojo extends AbstractMojo { invoke(UpdateReadmeMojo.class); } - private void invoke(Class<? extends AbstractMojo> mojoClass) throws MojoExecutionException, MojoFailureException { - try { - AbstractMojo mojo = mojoClass.newInstance(); - mojo.setLog(getLog()); - mojo.setPluginContext(getPluginContext()); - ((AbstractGeneratorMojo) mojo).execute(project, projectHelper, buildContext); - - } catch (MojoExecutionException|MojoFailureException e) { - throw e; - } catch (Exception e) { - throw new MojoFailureException("Unable to create mojo", e); - } - } - - private void writeIncrementalInfo(MavenProject project) throws MojoExecutionException { - try { - Path cacheData = getIncrementalDataPath(project); - String curdata = getIncrementalData(); - Files.createDirectories(cacheData.getParent()); - try (Writer w = Files.newBufferedWriter(cacheData)) { - w.append(curdata); - } - } catch (IOException e) { - throw new MojoExecutionException("Error checking manifest uptodate status", e); - } - } - - private boolean isUpToDate(MavenProject project) throws MojoExecutionException { - try { - Path cacheData = getIncrementalDataPath(project); - String prvdata; - if (Files.isRegularFile(cacheData)) { - prvdata = new String(Files.readAllBytes(cacheData), StandardCharsets.UTF_8); - } else { - prvdata = null; - } - String curdata = getIncrementalData(); - if (curdata.equals(prvdata)) { - long lastmod = Files.getLastModifiedTime(cacheData).toMillis(); - Set<String> stale = Stream.concat(Stream.concat( - project.getCompileSourceRoots().stream().map(File::new), - Stream.of(new File(project.getBuild().getOutputDirectory()))), - project.getArtifacts().stream().map(Artifact::getFile)) - .flatMap(f -> newer(lastmod, f)).collect(Collectors.toSet()); - if (!stale.isEmpty()) { - getLog().info("Stale files detected, re-generating."); - if (showStaleFiles) { - getLog().info("Stale files: " + stale.stream().collect(Collectors.joining(", "))); - } else if (getLog().isDebugEnabled()) { - getLog().debug("Stale files: " + stale.stream().collect(Collectors.joining(", "))); - } - } else { - // everything is in order, skip - getLog().info("Skipping generation, everything is up to date."); - return true; - } - } else { - if (prvdata == null) { - getLog().info("No previous run data found, generating files."); - } else { - getLog().info("Configuration changed, re-generating files."); - } - } - } catch (IOException e) { - throw new MojoExecutionException("Error checking uptodate status", e); - } - return false; - } - - private String getIncrementalData() { - return ""; - } - - private Path getIncrementalDataPath(MavenProject project) { - return Paths.get(project.getBuild().getDirectory(), "camel-package-maven-plugin", "org.apache.camel_camel-package-maven-plugin_info_xx"); - } - - private long lastmod(Path p) { - try { - return Files.getLastModifiedTime(p).toMillis(); - } catch (IOException e) { - return 0; - } - } - - private Stream<String> newer(long lastmod, File file) { - try { - if (file.isDirectory()) { - return Files.walk(file.toPath()).filter(Files::isRegularFile).filter(p -> lastmod(p) > lastmod).map(Path::toString); - } else if (file.isFile()) { - if (lastmod(file.toPath()) > lastmod) { - if (file.getName().endsWith(".jar")) { - try (ZipFile zf = new ZipFile(file)) { - return zf.stream().filter(ze -> !ze.isDirectory()).filter(ze -> ze.getLastModifiedTime().toMillis() > lastmod) - .map(ze -> file.toString() + "!" + ze.getName()).collect(Collectors.toList()).stream(); - } - } else { - return Stream.of(file.toString()); - } - } else { - return Stream.empty(); - } - } else { - return Stream.empty(); - } - } catch (IOException e) { - throw new IOError(e); - } - } - }