Skip to content

Commit

Permalink
fix issue#66 and issue#67
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Apr 11, 2017
1 parent 6709b3a commit c5dd497
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 94 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>3.4-SNAPSHOT</version>
<version>3.3.1-SNAPSHOT</version>
<name>MAVEN PROCESSOR PLUGIN - ${project.version}</name>
<description>A maven plugin to process annotation for jdk6 at compile time

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
Expand All @@ -45,6 +46,7 @@
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;
import org.bsc.function.Consumer;
import org.codehaus.plexus.compiler.manager.CompilerManager;
Expand Down Expand Up @@ -263,6 +265,11 @@ interface ArtifactClosure {
* Allows running the compiler in a separate process.
* If false it uses the built in compiler, while if true it will use an executable.
*
* to set source and target use
* <pre>
* maven.processor.source
* maven.processor.target
* </pre>
* @since 3.3
*/
@Parameter(defaultValue = "false", property = "fork")
Expand Down Expand Up @@ -428,6 +435,54 @@ public void execute() throws MojoExecutionException

}

/**
* TODO remove the part with ToolchainManager lookup once we depend on
* 3.0.9 (have it as prerequisite). Define as regular component field then.
*
* @param jdkToolchain
*/
private Toolchain getToolchain(final Map<String, String> jdkToolchain)
{
Toolchain tc = null;

if ( jdkToolchain != null && !jdkToolchain.isEmpty())
{
// Maven 3.3.1 has plugin execution scoped Toolchain Support
try
{
final Method getToolchainsMethod =
toolchainManager.getClass().getMethod( "getToolchains",
MavenSession.class,
String.class,
Map.class );

@SuppressWarnings( "unchecked" )
final List<Toolchain> tcs =
(List<Toolchain>) getToolchainsMethod.invoke( toolchainManager,
session,
"jdk",
jdkToolchain );

if ( tcs != null && tcs.size() > 0 )
{
tc = tcs.get( 0 );
}
}
catch ( Exception e )
{
// ignore
}
}

if ( tc == null )
{
tc = toolchainManager.getToolchainFromBuildContext( "jdk", session );
}

return tc;
}


@SuppressWarnings("unchecked")
private void executeWithExceptionsHandled() throws Exception
{
Expand Down Expand Up @@ -615,19 +670,28 @@ public void execute(Artifact artifact) {
}
}
});

final java.util.Map<String,String> jdkToolchain =
java.util.Collections.emptyMap();

final Toolchain tc = getToolchain(jdkToolchain);

// If toolchain is set force fork compilation
if( toolchainManager != null ) {
if( tc != null ) {
fork = true;
}

if( fork ) {
getLog().debug( "PROCESSOR COMPILER FORKED!");
}

//compileLock.lock();
try {


final JavaCompiler compiler = (fork) ?
AnnotationProcessorCompiler.createOutProcess(
toolchainManager,
tc,
compilerManager,
project,
session ) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.Processor;
import javax.lang.model.SourceVersion;
Expand All @@ -26,7 +23,6 @@
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;
import org.codehaus.plexus.compiler.CompilerConfiguration;
import org.codehaus.plexus.compiler.CompilerException;
import org.codehaus.plexus.compiler.CompilerMessage;
Expand Down Expand Up @@ -230,6 +226,7 @@ protected CompilerResult performCompile( CompilerConfiguration config ) throws C

final String[] compilerArguments =
org.codehaus.plexus.compiler.javac.JavacCompiler.buildCompilerArguments(config, getSourceFiles(config) );

return compileOutOfProcess( config, getJavacExecutable(config), compilerArguments );

}
Expand All @@ -253,15 +250,15 @@ public class AnnotationProcessorCompiler implements JavaCompiler {
final MavenProject project;
final MavenSession session;
final CompilerManager plexusCompiler;
final ToolchainManager toolchainManager;
final Toolchain toolchain;

public static JavaCompiler createOutProcess( ToolchainManager toolchainManager,
public static JavaCompiler createOutProcess( Toolchain toolchain,
CompilerManager plexusCompiler,
MavenProject project,
MavenSession session )
{

return new AnnotationProcessorCompiler( toolchainManager, plexusCompiler, project, session);
return new AnnotationProcessorCompiler( toolchain, plexusCompiler, project, session);
}

public static JavaCompiler createInProcess() {
Expand All @@ -285,50 +282,7 @@ private static void printCommand( final org.codehaus.plexus.compiler.Compiler j
}


//TODO remove the part with ToolchainManager lookup once we depend on
//3.0.9 (have it as prerequisite). Define as regular component field then.
private Toolchain getToolchain(final Map<String, String> jdkToolchain)
{
Toolchain tc = null;

if ( jdkToolchain != null && !jdkToolchain.isEmpty())
{
// Maven 3.3.1 has plugin execution scoped Toolchain Support
try
{
final Method getToolchainsMethod =
toolchainManager.getClass().getMethod( "getToolchains",
MavenSession.class,
String.class,
Map.class );

@SuppressWarnings( "unchecked" )
final List<Toolchain> tcs =
(List<Toolchain>) getToolchainsMethod.invoke( toolchainManager,
session,
"jdk",
jdkToolchain );

if ( tcs != null && tcs.size() > 0 )
{
tc = tcs.get( 0 );
}
}
catch ( Exception e )
{
// ignore
}
}

if ( tc == null )
{
tc = toolchainManager.getToolchainFromBuildContext( "jdk", session );
}

return tc;
}

private AnnotationProcessorCompiler( ToolchainManager toolchainManager,
private AnnotationProcessorCompiler( Toolchain toolchain,
CompilerManager plexusCompiler,
MavenProject project,
MavenSession session )
Expand All @@ -337,7 +291,7 @@ private AnnotationProcessorCompiler( ToolchainManager toolchainManager,
this.project = project;
this.session = session;
this.plexusCompiler = plexusCompiler;
this.toolchainManager = toolchainManager;
this.toolchain = toolchain;

}

Expand All @@ -354,7 +308,6 @@ private void execute( final Iterable<String> options,
final java.util.Iterator<String> ii = options.iterator();

while( ii.hasNext() ) {

final String option = ii.next();

if( "-cp".equals(option)) {
Expand Down Expand Up @@ -386,7 +339,10 @@ else if( "-d".equals(option) ) {
else if( "-s".equals(option) ) {
javacConf.setGeneratedSourcesDirectory( new java.io.File(ii.next()));
}

else if( option.startsWith("-A") ) {
javacConf.addCompilerCustomArgument(option, "");

}
final java.util.Properties props = project.getProperties();

final String sourceVersion = props.getProperty(PROCESSOR_SOURCE,props.getProperty(COMPILER_SOURCE, DEFAULT_SOURCE_VERSION));
Expand All @@ -409,19 +365,12 @@ else if( "-s".equals(option) ) {
javacConf.setFork(true);
javacConf.setVerbose(false);

if( toolchainManager != null ) {
final java.util.Map<String,String> jdkToolchain =
java.util.Collections.emptyMap();

final Toolchain tc = getToolchain( jdkToolchain );

if( tc!=null ) {
final String executable = tc.findTool( "javac");
//out.print( "==> TOOLCHAIN EXECUTABLE: "); out.println( executable );
javacConf.setExecutable(executable);
}

if( toolchain != null ) {
final String executable = toolchain.findTool( "javac");
//out.print( "==> TOOLCHAIN EXECUTABLE: "); out.println( executable );
javacConf.setExecutable(executable);
}

CompilerResult result;

// USING STANDARD PLEXUS
Expand Down
Loading

0 comments on commit c5dd497

Please sign in to comment.