Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Add m2e support #6

Merged
merged 1 commit into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@
<artifactId>plexus-utils</artifactId>
<version>3.3.0</version>
</dependency>
<!-- incremental build support (http://www.eclipse.org/m2e/documentation/m2e-making-maven-plugins-compat.html) -->
<dependency>
<groupId>org.sonatype.plexus</groupId>
<artifactId>plexus-build-api</artifactId>
<version>0.0.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
65 changes: 58 additions & 7 deletions src/main/java/org/eclipse/sisu/mojos/IndexMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -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
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -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 ) );
}
}

Expand All @@ -193,11 +216,10 @@ protected void warn( final String message )
private ClassLoader getProjectClassLoader()
{
final List<URL> classPath = new ArrayList<URL>();
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() )
{
Expand All @@ -209,8 +231,8 @@ private ClassLoader getProjectClassLoader()
private URL[] getIndexPath()
{
final List<URL> indexPath = new ArrayList<URL>();
appendToClassPath( indexPath, outputDirectory );
if ( includeDependencies )
appendDirectoryToClassPath( indexPath, outputDirectory );
if ( includeDependencies && !buildContext.isIncremental() )
{
final FilterArtifacts filter = new FilterArtifacts();

Expand All @@ -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 )
Expand All @@ -251,7 +273,36 @@ private void dumpEntries( final String name, final List<URL> urls )
}
}

private void appendToClassPath( final List<URL> urls, final File file )
private void appendDirectoryToClassPath( final List<URL> 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<URL> urls, final File file )
{
if ( null != file )
{
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/eclipse/sisu/mojos/MainIndexMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() ) );
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/eclipse/sisu/mojos/TestIndexMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() ) );
Expand Down
25 changes: 25 additions & 0 deletions src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!--
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
-->
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<goals>
<goal>index</goal>
<goal>main-index</goal>
<goal>test-index</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>true</runOnIncremental>
<runOnConfiguration>false</runOnConfiguration>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>