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 a4758585b5be74f60c1b98e2d72210f8127b669b Author: Guillaume Nodet <[email protected]> AuthorDate: Fri Jan 17 15:57:06 2020 +0100 [CAMEL-14444] Build speed improvements: use copy of the jandex mojo with stale files detection --- components/pom.xml | 1 + parent/pom.xml | 18 ---- .../camel/maven/packaging/PackageJandexMojo.java | 112 +++++++++++++++++++++ .../camel/maven/packaging/PackageJaxbMojo.java | 17 ++++ .../m2e/lifecycle-mapping-metadata.xml | 0 5 files changed, 130 insertions(+), 18 deletions(-) diff --git a/components/pom.xml b/components/pom.xml index f22066d..9f509c8 100644 --- a/components/pom.xml +++ b/components/pom.xml @@ -378,6 +378,7 @@ <execution> <id>generate</id> <goals> + <goal>jandex</goal> <goal>prepare-components</goal> <goal>generate-components-list</goal> </goals> diff --git a/parent/pom.xml b/parent/pom.xml index d0c6b01..a0abba1 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -431,7 +431,6 @@ <lz4-version>1.6.0</lz4-version> <!-- needed from tooling/archetypes --> <!-- use antrun 1.6 as 1.7 fails with finding tools.jar on java on some platforms --> - <jandex-maven-plugin-version>1.0.7</jandex-maven-plugin-version> <maven-antrun-plugin-version>1.6</maven-antrun-plugin-version> <maven-archetype-plugin-version>3.0.1</maven-archetype-plugin-version> <maven-archetype-packaging-version>2.3</maven-archetype-packaging-version> @@ -4242,19 +4241,6 @@ <artifactId>maven-surefire-plugin</artifactId> <version>${maven-surefire-plugin-version}</version> </plugin> - <plugin> - <groupId>org.jboss.jandex</groupId> - <artifactId>jandex-maven-plugin</artifactId> - <version>${jandex-maven-plugin-version}</version> - <executions> - <execution> - <id>make-index</id> - <goals> - <goal>jandex</goal> - </goals> - </execution> - </executions> - </plugin> </plugins> </pluginManagement> <plugins> @@ -4364,10 +4350,6 @@ </execution> </executions> </plugin> - <plugin> - <groupId>org.jboss.jandex</groupId> - <artifactId>jandex-maven-plugin</artifactId> - </plugin> </plugins> </build> diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJandexMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJandexMojo.java new file mode 100644 index 0000000..4d562aa --- /dev/null +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJandexMojo.java @@ -0,0 +1,112 @@ +/* + * 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.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.jboss.jandex.Index; +import org.jboss.jandex.IndexWriter; +import org.jboss.jandex.Indexer; + +/** + * Generate a Jandex index for classes compiled as part of the current project. + * + * @author jdcasey + */ +@Mojo(name = "jandex", defaultPhase = LifecyclePhase.PROCESS_CLASSES, threadSafe = true) +public class PackageJandexMojo extends AbstractMojo { + + @Parameter(defaultValue = "${project.build.outputDirectory}") + protected File classesDirectory; + + /** + * The name of the index file. Default's to 'target/classes/META-INF/jandex.idx' + */ + @Parameter(defaultValue = "${project.build.directory}/META-INF/jandex.idx") + private File index; + + @Parameter(defaultValue = "${showStaleFiles}") + private boolean showStaleFiles; + + @Override + public void execute() throws MojoExecutionException { + if (!classesDirectory.isDirectory()) { + return; + } + try { + List<Path> inputs = Files.walk(classesDirectory.toPath()) + .filter(f -> f.getFileName().toString().endsWith(".class")) + .collect(Collectors.toList()); + if (index.exists()) { + long lastmod = lastmod(index.toPath()); + String stale = inputs.stream() + .filter(p -> lastmod(p) > lastmod) + .map(Path::toString) + .collect(Collectors.joining(", ")); + if (!stale.isEmpty()) { + getLog().info("Stale files detected, re-generating index."); + if (showStaleFiles) { + getLog().info("Stale files: " + stale); + } else if (getLog().isDebugEnabled()) { + getLog().debug("Stale files: " + stale); + } + } else { + // everything is in order, skip + getLog().info("Skipping index generation, everything is up to date."); + return; + } + } + + getLog().info("Building index..."); + final Indexer indexer = new Indexer(); + for (Path file : inputs) { + try (InputStream fis = Files.newInputStream(file)) { + indexer.index(fis); + } + } + Index idx = indexer.complete(); + index.getParentFile().mkdirs(); + try (OutputStream os = new FileOutputStream(index)) { + new IndexWriter(os).write(idx); + } + } catch (IOException e) { + throw new MojoExecutionException(e.getMessage(), e); + } + } + + private long lastmod(Path p) { + try { + return Files.getLastModifiedTime(p).toMillis(); + } catch (IOException e) { + return 0; + } + } + +} diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJaxbMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJaxbMojo.java index 9a13682..9622133 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJaxbMojo.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJaxbMojo.java @@ -17,6 +17,7 @@ package org.apache.camel.maven.packaging; import java.io.File; +import java.io.FileInputStream; import java.io.IOError; import java.io.IOException; import java.io.InputStream; @@ -48,6 +49,8 @@ import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationTarget; import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; +import org.jboss.jandex.Index; +import org.jboss.jandex.IndexReader; import org.jboss.jandex.IndexView; import org.jboss.jandex.Indexer; @@ -59,6 +62,12 @@ import org.jboss.jandex.Indexer; public class PackageJaxbMojo extends AbstractGeneratorMojo { /** + * The name of the index file. Default's to 'target/classes/META-INF/jandex.idx' + */ + @Parameter(defaultValue = "${project.build.directory}/META-INF/jandex.idx") + private File index; + + /** * The output directory for generated components file */ @Parameter(defaultValue = "${project.build.directory}/generated/camel/jaxb") @@ -114,6 +123,14 @@ public class PackageJaxbMojo extends AbstractGeneratorMojo { } private IndexView createIndex(List<String> locations) throws MojoExecutionException { + if (index.exists()) { + try (InputStream is = new FileInputStream(index)) { + IndexReader r = new IndexReader(is); + return r.read(); + } catch (IOException e) { + throw new MojoExecutionException("Error", e); + } + } try { Indexer indexer = new Indexer(); locations.stream().map(this::asFolder).filter(Files::isDirectory).flatMap(this::walk).filter(Files::isRegularFile) diff --git a/tooling/maven/camel-package-maven-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml b/tooling/maven/camel-package-maven-plugin/src/main/m2e/lifecycle-mapping-metadata.xml similarity index 100% rename from tooling/maven/camel-package-maven-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml rename to tooling/maven/camel-package-maven-plugin/src/main/m2e/lifecycle-mapping-metadata.xml
