From fff6dab844d6e9a05fe52cf22e2e844f40b11af5 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Sun, 10 Jul 2022 16:39:31 +0200 Subject: [PATCH] Add m2e support (#6) This closes #5 --- pom.xml | 7 ++ .../org/eclipse/sisu/mojos/IndexMojo.java | 65 +++++++++++++++++-- .../org/eclipse/sisu/mojos/MainIndexMojo.java | 10 ++- .../org/eclipse/sisu/mojos/TestIndexMojo.java | 10 ++- .../m2e/lifecycle-mapping-metadata.xml | 25 +++++++ 5 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml diff --git a/pom.xml b/pom.xml index e53cca8d..261da5ef 100644 --- a/pom.xml +++ b/pom.xml @@ -134,6 +134,13 @@ plexus-utils 3.3.0 + + + org.sonatype.plexus + plexus-build-api + 0.0.7 + compile + junit junit diff --git a/src/main/java/org/eclipse/sisu/mojos/IndexMojo.java b/src/main/java/org/eclipse/sisu/mojos/IndexMojo.java index 0094df3b..4a4599b1 100644 --- a/src/main/java/org/eclipse/sisu/mojos/IndexMojo.java +++ b/src/main/java/org/eclipse/sisu/mojos/IndexMojo.java @@ -24,6 +24,7 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; +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.plugins.annotations.ResolutionScope; @@ -36,9 +37,11 @@ import org.apache.maven.shared.artifact.filter.collection.ProjectTransitivityFilter; import org.apache.maven.shared.artifact.filter.collection.ScopeFilter; import org.apache.maven.shared.artifact.filter.collection.TypeFilter; +import org.codehaus.plexus.util.Scanner; import org.codehaus.plexus.util.StringUtils; import org.eclipse.sisu.space.SisuIndex; import org.eclipse.sisu.space.URLClassSpace; +import org.sonatype.plexus.build.incremental.BuildContext; /** * Generates a qualified class index for the current project and its dependencies. @@ -47,6 +50,8 @@ public class IndexMojo extends AbstractMojo { + static final String INDEX_FOLDER = "META-INF/sisu/"; // copied from AbstractSisuIndex as not public + // ---------------------------------------------------------------------- // Configurable parameters // ---------------------------------------------------------------------- @@ -139,6 +144,23 @@ public class IndexMojo @Parameter( property = "project", required = true, readonly = true ) private MavenProject project; + /** + * For m2e incremental build support + */ + @Component + protected BuildContext buildContext; + + public IndexMojo() + { + super(); + } + + public IndexMojo( final BuildContext buildContext ) + { + super(); + this.buildContext = buildContext; + } + // ---------------------------------------------------------------------- // Public methods // ---------------------------------------------------------------------- @@ -183,6 +205,7 @@ protected void warn( final String message ) getLog().warn( message ); } }.index( new URLClassSpace( getProjectClassLoader(), getIndexPath() ) ); + buildContext.refresh( new File( outputDirectory, INDEX_FOLDER ) ); } } @@ -193,11 +216,10 @@ protected void warn( final String message ) private ClassLoader getProjectClassLoader() { final List classPath = new ArrayList(); - appendToClassPath( classPath, outputDirectory ); - appendToClassPath( classPath, new File( project.getBuild().getOutputDirectory() ) ); + appendDirectoryToClassPath( classPath, outputDirectory ); for ( final Object artifact : project.getArtifacts() ) { - appendToClassPath( classPath, ( (Artifact) artifact ).getFile() ); + appendFileToClassPath( classPath, ( (Artifact) artifact ).getFile() ); } if ( getLog().isDebugEnabled() ) { @@ -209,8 +231,8 @@ private ClassLoader getProjectClassLoader() private URL[] getIndexPath() { final List indexPath = new ArrayList(); - appendToClassPath( indexPath, outputDirectory ); - if ( includeDependencies ) + appendDirectoryToClassPath( indexPath, outputDirectory ); + if ( includeDependencies && !buildContext.isIncremental() ) { final FilterArtifacts filter = new FilterArtifacts(); @@ -227,7 +249,7 @@ private URL[] getIndexPath() { for ( final Object artifact : filter.filter( project.getArtifacts() ) ) { - appendToClassPath( indexPath, ( (Artifact) artifact ).getFile() ); + appendFileToClassPath( indexPath, ( (Artifact) artifact ).getFile() ); } } catch ( final ArtifactFilterException e ) @@ -251,7 +273,36 @@ private void dumpEntries( final String name, final List urls ) } } - private void appendToClassPath( final List urls, final File file ) + private void appendDirectoryToClassPath( final List urls, File directory ) + { + if ( directory.isDirectory() ) + { + Scanner scanner = buildContext.newScanner( directory ); + scanner.setIncludes( new String[] {"**/*.class"} ); + scanner.scan(); + String[] includedFiles = scanner.getIncludedFiles(); + if ( includedFiles != null && includedFiles.length > 0 ) + { + getLog().debug("Found at least one class file in " + directory ); + appendFileToClassPath( urls, directory ); + } + else + { + getLog().debug("No class files found in " + directory ); + } + } + else + { + getLog().debug("Path " + directory + " does not exist or is no directory" ); + } + } + + /** + * + * @param urls the list to which to append the URL + * @param file must either be a directory or a JAR file + */ + private void appendFileToClassPath( final List urls, final File file ) { if ( null != file ) { diff --git a/src/main/java/org/eclipse/sisu/mojos/MainIndexMojo.java b/src/main/java/org/eclipse/sisu/mojos/MainIndexMojo.java index 690e73fd..5cd16993 100644 --- a/src/main/java/org/eclipse/sisu/mojos/MainIndexMojo.java +++ b/src/main/java/org/eclipse/sisu/mojos/MainIndexMojo.java @@ -13,11 +13,13 @@ import java.io.File; import org.apache.maven.plugin.AbstractMojo; +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.sonatype.plexus.build.incremental.BuildContext; /** * Generates a qualified class index for classes compiled by the current project. @@ -36,13 +38,19 @@ public class MainIndexMojo @Parameter( property = "project", required = true, readonly = true ) private MavenProject project; + /** + * For m2e incremental build support + */ + @Component + protected BuildContext buildContext; + // ---------------------------------------------------------------------- // Public methods // ---------------------------------------------------------------------- public void execute() { - final IndexMojo mojo = new IndexMojo(); + final IndexMojo mojo = new IndexMojo( buildContext ); mojo.setLog( getLog() ); mojo.setProject( project ); mojo.setOutputDirectory( new File( project.getBuild().getOutputDirectory() ) ); diff --git a/src/main/java/org/eclipse/sisu/mojos/TestIndexMojo.java b/src/main/java/org/eclipse/sisu/mojos/TestIndexMojo.java index 11467288..137bbbe2 100644 --- a/src/main/java/org/eclipse/sisu/mojos/TestIndexMojo.java +++ b/src/main/java/org/eclipse/sisu/mojos/TestIndexMojo.java @@ -13,11 +13,13 @@ import java.io.File; import org.apache.maven.plugin.AbstractMojo; +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.sonatype.plexus.build.incremental.BuildContext; /** * Generates a qualified class index for test classes compiled by the current project. @@ -36,13 +38,19 @@ public class TestIndexMojo @Parameter( property = "project", required = true, readonly = true ) private MavenProject project; + /** + * For m2e incremental build support + */ + @Component + protected BuildContext buildContext; + // ---------------------------------------------------------------------- // Public methods // ---------------------------------------------------------------------- public void execute() { - final IndexMojo mojo = new IndexMojo(); + final IndexMojo mojo = new IndexMojo( buildContext ); mojo.setLog( getLog() ); mojo.setProject( project ); mojo.setOutputDirectory( new File( project.getBuild().getTestOutputDirectory() ) ); diff --git a/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml b/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml new file mode 100644 index 00000000..d3d53eea --- /dev/null +++ b/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml @@ -0,0 +1,25 @@ + + + + + + + index + main-index + test-index + + + + + true + false + + + + + \ No newline at end of file