From 75059592db5173e7832ac85204cce655e25ad99c Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Thu, 9 Mar 2017 17:39:07 +0100 Subject: [PATCH 01/14] move to next develop version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 16cdf4f..1fb5eb2 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.bsc.maven maven-processor-plugin maven-plugin - 3.2.0 + 3.2.1-SNAPSHOT MAVEN PROCESSOR PLUGIN - ${project.version} A maven plugin to process annotation for jdk6 at compile time From 91128bea35178d180b6aebe0416573d2f01eeaff Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Tue, 21 Mar 2017 18:14:50 +0100 Subject: [PATCH 02/14] add support of plexus compiler --- pom.xml | 34 ++- .../AbstractAnnotationProcessorMojo.java | 28 +- .../AnnotationProcessorCompiler.java | 241 ++++++++++++++++++ test/pom.xml | 2 +- 4 files changed, 302 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java diff --git a/pom.xml b/pom.xml index 1fb5eb2..cab0605 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,26 @@ This plugin could be considered the 'alter ego' of maven apt plugin http://mojo. + + + + org.codehaus.plexus + plexus-compiler-api + 2.8.1 + + + org.codehaus.plexus + plexus-compiler-manager + 2.8.1 + + + org.codehaus.plexus + plexus-compiler-javac + 2.8.1 + runtime + + + org.apache.maven @@ -104,7 +124,19 @@ This plugin could be considered the 'alter ego' of maven apt plugin http://mojo. plexus-utils 3.0.15 - + + org.codehaus.plexus + plexus-compiler-api + + + org.codehaus.plexus + plexus-compiler-manager + + + org.codehaus.plexus + plexus-compiler-javac + runtime + junit diff --git a/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java b/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java index fabfdc8..7d6d854 100644 --- a/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java +++ b/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java @@ -39,12 +39,15 @@ import javax.tools.Diagnostic.Kind; import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; +import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.BuildPluginManager; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import org.bsc.function.Consumer; +import org.codehaus.plexus.compiler.manager.CompilerManager; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.StringUtils; @@ -256,6 +259,26 @@ interface ArtifactClosure { @Parameter(defaultValue = "false", property = "skipAnnotationProcessing") protected boolean skip; + /** + * + * @since 3.2.1 + */ + @Component + protected MavenSession session; + + /** + * + * @since 3.2.1 + */ + @Component + protected BuildPluginManager pluginManager; + + /** + * Plexus compiler manager. + */ + @Component + protected CompilerManager compilerManager; + /** * for execution synchronization */ @@ -580,7 +603,10 @@ public void execute(Artifact artifact) { //compileLock.lock(); try { - final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + final JavaCompiler compiler = + new AnnotationProcessorCompiler( compilerManager, project, session, pluginManager); + //ToolProvider.getSystemJavaCompiler(); + if( compiler==null ) { getLog().error("JVM is not suitable for processing annotation! ToolProvider.getSystemJavaCompiler() is null."); diff --git a/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java b/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java new file mode 100644 index 0000000..e8328b2 --- /dev/null +++ b/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java @@ -0,0 +1,241 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.bsc.maven.plugin.processor; + +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Writer; +import java.nio.charset.Charset; +import java.util.Locale; +import java.util.Set; +import javax.annotation.processing.Processor; +import javax.lang.model.SourceVersion; +import javax.tools.Diagnostic; +import javax.tools.DiagnosticListener; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileManager; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.BuildPluginManager; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.compiler.CompilerConfiguration; +import org.codehaus.plexus.compiler.manager.CompilerManager; + +/** + * + * @author softphone + */ +public class AnnotationProcessorCompiler implements JavaCompiler { + + final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + + final MavenProject project; + final MavenSession session; + final BuildPluginManager pluginManager; + final CompilerManager plexusCompiler; + + AnnotationProcessorCompiler( CompilerManager plexusCompiler, MavenProject project, MavenSession session, BuildPluginManager pluginManager ) { + + this.project = project; + this.session = session; + this.pluginManager = pluginManager; + this.plexusCompiler = plexusCompiler; + } + +/* + private String find( final Iterable options, String key, String def ) { + + for( String option : options ) { + if( option.equals(key)) { + return + } + } + + } +*/ + private void executePlugin(final Iterable options) throws Exception { + + final CompilerConfiguration javacConf = new CompilerConfiguration(); + + final java.util.Iterator ii = options.iterator(); + + while( ii.hasNext() ) { + + final String option = ii.next(); + + if( "-cp".equals(option)) { + javacConf.addClasspathEntry(ii.next()); + } + else if( "-sourcepath".equals(option) ) { + String [] sourceLocations = ii.next().split(":"); + for( String path : sourceLocations ) { + + final java.io.File dir = new java.io.File( path ); + if( dir.exists() ) + javacConf.addSourceLocation(path); + + } + //javacConf.addCompilerCustomArgument(option, ii.next()); + } + else if( "-proc:only".equals(option) ) { + javacConf.setProc("only"); + } + else if( "-processor".equals(option) ) { + + final String processors[] = ii.next().split(","); + + javacConf.setAnnotationProcessors( processors ); + } + else if( "-d".equals(option) ) { + javacConf.setOutputLocation(ii.next()); + } + else if( "-s".equals(option) ) { + javacConf.setGeneratedSourcesDirectory( new java.io.File(ii.next())); + } + + javacConf.setSourceVersion("1.6"); + javacConf.setTargetVersion("1.6"); + + } + + javacConf.setFork(false); + + final org.codehaus.plexus.compiler.Compiler javac = plexusCompiler.getCompiler("javac"); + + String[] cli = javac.createCommandLine(javacConf); + + for( String c : cli ) { + System.out.printf( "CLI: [%s]\n", c); + } + javac.performCompile( javacConf ); + + } + + @Override + public CompilationTask getTask( + final Writer out, + final JavaFileManager fileManager, + final DiagnosticListener diagnosticListener, + final Iterable options, + final Iterable classes, + final Iterable compilationUnits) + { + + return new CompilationTask() { + @Override + public void setProcessors(Iterable processors) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void setLocale(Locale locale) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Boolean call() { + System.out.println("OPTIONS"); + for( String option : options ) { + System.out.printf("OPTION:\t[%s]\n", option); + } + System.out.println("CLASSES"); + if( classes != null ) { + for( String clazz : classes ) { + System.out.printf("CLASS:\t[%s]\n", clazz); + } + } + System.out.println("COMPILATION UNITS"); + if( compilationUnits != null ) { + for( JavaFileObject cu : compilationUnits ) { + System.out.printf("CUNIT:\t[%s]\n", cu); + } + } + + try { + executePlugin(options); + return true; + } catch (final Exception ex) { + diagnosticListener.report( new Diagnostic() { + @Override + public Diagnostic.Kind getKind() { + return Diagnostic.Kind.ERROR; + } + + @Override + public JavaFileObject getSource() { + return null; + } + + @Override + public long getPosition() { + return -1; + } + + @Override + public long getStartPosition() { + return -1; + } + + @Override + public long getEndPosition() { + return -1; + } + + @Override + public long getLineNumber() { + return -1; + } + + @Override + public long getColumnNumber() { + return -1; + } + + @Override + public String getCode() { + return null; + } + + @Override + public String getMessage(Locale locale) { + return ex.getLocalizedMessage(); + } + + public String toString() { + return ex.getLocalizedMessage(); + } + + }); + return false; + } + } + }; + } + + @Override + public StandardJavaFileManager getStandardFileManager(DiagnosticListener diagnosticListener, Locale locale, Charset charset) { + return compiler.getStandardFileManager(diagnosticListener, locale, charset); + } + + @Override + public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Set getSourceVersions() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public int isSupportedOption(String option) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + +} diff --git a/test/pom.xml b/test/pom.xml index 51da027..318df5f 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -4,7 +4,7 @@ org.bsc.maven maven-processor-plugin-test - 3.2.0-SNAPSHOT + 3.2.1-SNAPSHOT jar MAVEN PROCESSOR PLUGIN TEST- ${project.version} From 384cc666c61d4cee543576a2fe820d27c8b5ce49 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Tue, 28 Mar 2017 00:45:50 +0200 Subject: [PATCH 03/14] plexus compiler refinements --- .../AnnotationProcessorCompiler.java | 52 ++++++++++++++----- test/pom.xml | 19 ++++--- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java b/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java index e8328b2..4855eab 100644 --- a/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java +++ b/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java @@ -22,9 +22,11 @@ import javax.tools.ToolProvider; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.BuildPluginManager; -import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.compiler.CompilerConfiguration; +import org.codehaus.plexus.compiler.CompilerException; +import org.codehaus.plexus.compiler.CompilerMessage; +import org.codehaus.plexus.compiler.CompilerResult; import org.codehaus.plexus.compiler.manager.CompilerManager; /** @@ -59,7 +61,19 @@ private String find( final Iterable options, String key, String def ) { } */ - private void executePlugin(final Iterable options) throws Exception { + + private void printCommand( final org.codehaus.plexus.compiler.Compiler javac, final CompilerConfiguration javacConf ) throws CompilerException { + System.out.println(); + System.out.println(); + System.out.println( "javac \\"); + for( String c : javac.createCommandLine(javacConf) ) + System.out.printf( "%s \\\n", c); + System.out.println(); + System.out.println(); + System.out.println(); + + } + private void executePlugin(final Iterable options, final Iterable compilationUnits) throws Exception { final CompilerConfiguration javacConf = new CompilerConfiguration(); @@ -99,22 +113,32 @@ else if( "-s".equals(option) ) { javacConf.setGeneratedSourcesDirectory( new java.io.File(ii.next())); } - javacConf.setSourceVersion("1.6"); - javacConf.setTargetVersion("1.6"); + javacConf.setSourceVersion("1.7"); + javacConf.setTargetVersion("1.7"); + javacConf.setWorkingDirectory(project.getBasedir()); + final java.util.Set sourceFiles = + new java.util.HashSet(); + for( JavaFileObject src : compilationUnits ) { + sourceFiles.add( new java.io.File( src.toUri() ) ); + } + + javacConf.setSourceFiles(sourceFiles); } - - javacConf.setFork(false); - + javacConf.setDebug(true); + javacConf.setFork(true); + javacConf.setVerbose(true); + //javacConf.setExecutable("javac"); final org.codehaus.plexus.compiler.Compiler javac = plexusCompiler.getCompiler("javac"); - String[] cli = javac.createCommandLine(javacConf); - - for( String c : cli ) { - System.out.printf( "CLI: [%s]\n", c); - } - javac.performCompile( javacConf ); + //printCommand(javac, javacConf); + final CompilerResult result = javac.performCompile( javacConf ); + + for( CompilerMessage m : result.getCompilerMessages()) + System.out.printf( "message [%s]\n", m.getMessage() ); + + System.out.printf( "Compiler success [%b]\n", result.isSuccess()); } @Override @@ -158,7 +182,7 @@ public Boolean call() { } try { - executePlugin(options); + executePlugin(options, compilationUnits); return true; } catch (final Exception ex) { diagnosticListener.report( new Diagnostic() { diff --git a/test/pom.xml b/test/pom.xml index 318df5f..96bcfb0 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -36,10 +36,11 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.2 + 3.6.1 1.7 1.7 + none @@ -62,7 +63,7 @@ PROCESSOR PLUGIN - + org.bsc.maven.plugin.processor.test.TESTWikiProcessor org.bsc.jaxrs.JAXRSWikiProcessor @@ -91,6 +91,7 @@ PROCESSOR PLUGIN +--> process @@ -102,14 +103,18 @@ PROCESSOR PLUGIN ${basedir}/src/site - ${basedir}/src/main/java + ${basedir}/src/main/java + false - + org.bsc.maven.plugin.processor.test.TESTWikiProcessor + @@ -140,7 +145,7 @@ PROCESSOR PLUGIN add-test-source - generate-sources + generate-test-sources add-test-source From 8ce007e4f1f97d0dd631f5efa932ddba40356b16 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Tue, 28 Mar 2017 13:08:12 +0200 Subject: [PATCH 04/14] Customize Plexus Compiler to capture output --- .gitignore | 6 +- pom.xml | 3 +- .../AnnotationProcessorCompiler.java | 255 +++++++++++++++--- 3 files changed, 219 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index 339bf9d..229f04e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,7 @@ .classpath .project .settings/ -branch.sh -release.sh -sign-and-deploy.sh +*.sh target/ issue47.txt -pom.xml.versionsBackup +maven-compiler-plugin/ \ No newline at end of file diff --git a/pom.xml b/pom.xml index cab0605..1ce6b79 100644 --- a/pom.xml +++ b/pom.xml @@ -96,7 +96,7 @@ This plugin could be considered the 'alter ego' of maven apt plugin http://mojo. org.codehaus.plexus plexus-compiler-javac 2.8.1 - runtime + compile @@ -135,7 +135,6 @@ This plugin could be considered the 'alter ego' of maven apt plugin http://mojo. org.codehaus.plexus plexus-compiler-javac - runtime diff --git a/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java b/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java index 4855eab..ff9c30d 100644 --- a/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java +++ b/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java @@ -28,7 +28,205 @@ import org.codehaus.plexus.compiler.CompilerMessage; import org.codehaus.plexus.compiler.CompilerResult; import org.codehaus.plexus.compiler.manager.CompilerManager; +import org.codehaus.plexus.util.Os; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.cli.CommandLineUtils; +import org.codehaus.plexus.util.cli.Commandline; +class PlexusJavaCompilerWithOutput { + + static final PlexusJavaCompilerWithOutput INSTANCE = new PlexusJavaCompilerWithOutput(); + + private PlexusJavaCompilerWithOutput() { + } + + private String getJavacExecutable(CompilerConfiguration config ) + throws java.io.IOException + { + if( !StringUtils.isEmpty(config.getExecutable())) { + return config.getExecutable(); + } + + final String javacCommand = "javac" + ( Os.isFamily( Os.FAMILY_WINDOWS ) ? ".exe" : "" ); + + String javaHome = System.getProperty( "java.home" ); + java.io.File javacExe; + if ( Os.isName( "AIX" ) ) + { + javacExe = new java.io.File( javaHome + java.io.File.separator + ".." + java.io.File.separator + "sh", javacCommand ); + } + else if ( Os.isName( "Mac OS X" ) ) + { + javacExe = new java.io.File( javaHome + java.io.File.separator + "bin", javacCommand ); + } + else + { + javacExe = new java.io.File( javaHome + java.io.File.separator + ".." + java.io.File.separator + "bin", javacCommand ); + } + + // ---------------------------------------------------------------------- + // Try to find javacExe from JAVA_HOME environment variable + // ---------------------------------------------------------------------- + if ( !javacExe.isFile() ) + { + java.util.Properties env = CommandLineUtils.getSystemEnvVars(); + javaHome = env.getProperty( "JAVA_HOME" ); + if ( StringUtils.isEmpty( javaHome ) ) + { + throw new java.io.IOException( "The environment variable JAVA_HOME is not correctly set." ); + } + if ( !new java.io.File( javaHome ).isDirectory() ) + { + throw new java.io.IOException( + "The environment variable JAVA_HOME=" + javaHome + " doesn't exist or is not a valid directory." ); + } + + javacExe = new java.io.File( env.getProperty( "JAVA_HOME" ) + java.io.File.separator + "bin", javacCommand ); + } + + if ( !javacExe.isFile() ) + { + throw new java.io.IOException( "The javadoc executable '" + javacExe + + "' doesn't exist or is not a file. Verify the JAVA_HOME environment variable." ); + } + + return javacExe.getAbsolutePath(); + } + /** + * + * @param args + * @return + * @throws java.io.IOException + */ + private java.io.File createFileWithArguments( String[] args, String outputDirectory ) + throws java.io.IOException + { + java.io.PrintWriter writer = null; + try + { + final java.io.File tempFile; + { + tempFile = java.io.File.createTempFile( org.codehaus.plexus.compiler.javac.JavacCompiler.class.getName(), "arguments" ); + tempFile.deleteOnExit(); + } + + writer = new java.io.PrintWriter( new java.io.FileWriter( tempFile ) ); + + for ( String arg : args ) + { + String argValue = arg.replace( java.io.File.separatorChar, '/' ); + + writer.write( "\"" + argValue + "\"" ); + + writer.println(); + } + + writer.flush(); + + return tempFile; + + } + finally + { + if ( writer != null ) + { + writer.close(); + } + } + } + + private CompilerResult compileOutOfProcess( CompilerConfiguration config, String executable, String[] args ) + throws CompilerException + { + Commandline cli = new Commandline(); + + cli.setWorkingDirectory( config.getWorkingDirectory().getAbsolutePath() ); + + cli.setExecutable( executable ); + + try + { + + final java.io.File argumentsFile = createFileWithArguments( args, config.getOutputLocation() ); + cli.addArguments( + new String[]{ "@" + argumentsFile.getCanonicalPath().replace( java.io.File.separatorChar, '/' ) } ); + + if ( !StringUtils.isEmpty( config.getMaxmem() ) ) + { + cli.addArguments( new String[]{ "-J-Xmx" + config.getMaxmem() } ); + } + + if ( !StringUtils.isEmpty( config.getMeminitial() ) ) + { + cli.addArguments( new String[]{ "-J-Xms" + config.getMeminitial() } ); + } + + for ( String key : config.getCustomCompilerArgumentsAsMap().keySet() ) + { + if ( StringUtils.isNotEmpty( key ) && key.startsWith( "-J" ) ) + { + cli.addArguments( new String[]{ key } ); + } + } + } + catch ( java.io.IOException e ) + { + throw new CompilerException( "Error creating file with javac arguments", e ); + } + + final CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer(); + + int returnCode; + + final java.util.List messages = java.util.Collections.emptyList(); + + try + { + returnCode = CommandLineUtils.executeCommandLine( cli, out, out ); + } + catch ( Exception e ) + { + throw new CompilerException( "Error while executing the external compiler.", e ); + } + + boolean success = returnCode == 0; + return new CompilerResult( success, messages ) { + @Override + public String toString() { + return out.getOutput(); + } + }; + } + + private String[] getSourceFiles( CompilerConfiguration config ) throws java.io.IOException { + + final java.util.Set sourceFiles = + new java.util.HashSet(); + for( java.io.File src : config.getSourceFiles() ) { + sourceFiles.add( src.getCanonicalPath() ); + } + + return sourceFiles.toArray( new String[sourceFiles.size()] ); + } + /** + * + * @param config + * @return + */ + protected CompilerResult performCompile( CompilerConfiguration config ) throws CompilerException, java.io.IOException { + + final java.util.Set sourceFiles = + new java.util.HashSet(); + for( java.io.File src : config.getSourceFiles() ) { + sourceFiles.add( src.getCanonicalPath() ); + } + + final String[] compilerArguments = + org.codehaus.plexus.compiler.javac.JavacCompiler.buildCompilerArguments(config, getSourceFiles(config) ); + return compileOutOfProcess( config, getJavacExecutable(config), compilerArguments ); + + } +} /** * * @author softphone @@ -49,18 +247,6 @@ public class AnnotationProcessorCompiler implements JavaCompiler { this.pluginManager = pluginManager; this.plexusCompiler = plexusCompiler; } - -/* - private String find( final Iterable options, String key, String def ) { - - for( String option : options ) { - if( option.equals(key)) { - return - } - } - - } -*/ private void printCommand( final org.codehaus.plexus.compiler.Compiler javac, final CompilerConfiguration javacConf ) throws CompilerException { System.out.println(); @@ -121,24 +307,32 @@ else if( "-s".equals(option) ) { new java.util.HashSet(); for( JavaFileObject src : compilationUnits ) { sourceFiles.add( new java.io.File( src.toUri() ) ); + } javacConf.setSourceFiles(sourceFiles); } - javacConf.setDebug(true); + javacConf.setDebug(false); javacConf.setFork(true); - javacConf.setVerbose(true); + javacConf.setVerbose(false); //javacConf.setExecutable("javac"); - final org.codehaus.plexus.compiler.Compiler javac = plexusCompiler.getCompiler("javac"); + + CompilerResult result; + // USING STANDARD PLEXUS + /* + final org.codehaus.plexus.compiler.Compiler javac = plexusCompiler.getCompiler("javac"); //printCommand(javac, javacConf); - - final CompilerResult result = javac.performCompile( javacConf ); - + result = javac.performCompile( javacConf ); for( CompilerMessage m : result.getCompilerMessages()) - System.out.printf( "message [%s]\n", m.getMessage() ); - - System.out.printf( "Compiler success [%b]\n", result.isSuccess()); + System.out.println( m.getMessage() ); + */ + + // USING CUSTOM PLEXUS + + result = PlexusJavaCompilerWithOutput.INSTANCE.performCompile(javacConf); + + System.out.println( result.toString() ); } @Override @@ -164,23 +358,6 @@ public void setLocale(Locale locale) { @Override public Boolean call() { - System.out.println("OPTIONS"); - for( String option : options ) { - System.out.printf("OPTION:\t[%s]\n", option); - } - System.out.println("CLASSES"); - if( classes != null ) { - for( String clazz : classes ) { - System.out.printf("CLASS:\t[%s]\n", clazz); - } - } - System.out.println("COMPILATION UNITS"); - if( compilationUnits != null ) { - for( JavaFileObject cu : compilationUnits ) { - System.out.printf("CUNIT:\t[%s]\n", cu); - } - } - try { executePlugin(options, compilationUnits); return true; @@ -261,5 +438,5 @@ public Set getSourceVersions() { public int isSupportedOption(String option) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } - + } From 40a6e2ec2ab46864afe33bfd8798c293307fa5e1 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Tue, 28 Mar 2017 18:14:31 +0200 Subject: [PATCH 05/14] update --- pom.xml | 2 +- .../AbstractAnnotationProcessorMojo.java | 63 +++++++++++-------- .../AnnotationProcessorCompiler.java | 61 +++++++++++------- test/pom.xml | 16 ++--- 4 files changed, 81 insertions(+), 61 deletions(-) diff --git a/pom.xml b/pom.xml index 1ce6b79..548c4da 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.bsc.maven maven-processor-plugin maven-plugin - 3.2.1-SNAPSHOT + 3.3-SNAPSHOT MAVEN PROCESSOR PLUGIN - ${project.version} A maven plugin to process annotation for jdk6 at compile time diff --git a/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java b/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java index 7d6d854..1b86a56 100644 --- a/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java +++ b/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java @@ -41,7 +41,6 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.BuildPluginManager; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; @@ -260,21 +259,26 @@ interface ArtifactClosure { protected boolean skip; /** + * Allows running the compiler in a separate process. + * If false it uses the built in compiler, while if true it will use an executable. * - * @since 3.2.1 + * @since 3.3 */ - @Component - protected MavenSession session; - + @Parameter(defaultValue = "false", property = "fork") + protected boolean fork; + /** + * Maven Session * - * @since 3.2.1 + * @since 3.3 */ @Component - protected BuildPluginManager pluginManager; - + protected MavenSession session; + /** * Plexus compiler manager. + * + * @since 3.3 */ @Component protected CompilerManager compilerManager; @@ -525,6 +529,7 @@ public void accept(String sourcepath) { final DiagnosticListener dl = new DiagnosticListener() { + @Override public void report(Diagnostic< ? extends JavaFileObject> diagnostic) { if (!outputDiagnostics) { @@ -533,22 +538,23 @@ public void report(Diagnostic< ? extends JavaFileObject> diagnostic) { final Kind kind = diagnostic.getKind(); - if (Kind.ERROR == kind) { - - getLog().error(String.format("diagnostic: %s", diagnostic)); - - } else if (Kind.MANDATORY_WARNING == kind || Kind.WARNING == kind) { - - getLog().warn(String.format("diagnostic: %s", diagnostic)); - - } else if (Kind.NOTE == kind) { - - getLog().info(String.format("diagnostic: %s", diagnostic)); - - } else if (Kind.OTHER == kind) { - - getLog().info(String.format("diagnostic: %s", diagnostic)); - + if (null != kind) + switch (kind) { + case ERROR: + getLog().error(String.format("diagnostic: %s", diagnostic)); + break; + case MANDATORY_WARNING: + case WARNING: + getLog().warn(String.format("diagnostic: %s", diagnostic)); + break; + case NOTE: + getLog().info(String.format("diagnostic: %s", diagnostic)); + break; + case OTHER: + getLog().info(String.format("diagnostic: %s", diagnostic)); + break; + default: + break; } } @@ -573,6 +579,7 @@ public void report(Diagnostic< ? extends JavaFileObject> diagnostic) { processSourceArtifacts( new ArtifactClosure() { + @Override public void execute(Artifact artifact) { try { @@ -603,9 +610,11 @@ public void execute(Artifact artifact) { //compileLock.lock(); try { - final JavaCompiler compiler = - new AnnotationProcessorCompiler( compilerManager, project, session, pluginManager); - //ToolProvider.getSystemJavaCompiler(); + + + final JavaCompiler compiler = (fork) ? + AnnotationProcessorCompiler.createOutProcess(compilerManager, project, session ) : + AnnotationProcessorCompiler.createInProcess(); if( compiler==null ) { diff --git a/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java b/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java index ff9c30d..816b799 100644 --- a/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java +++ b/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java @@ -233,33 +233,52 @@ protected CompilerResult performCompile( CompilerConfiguration config ) throws C */ public class AnnotationProcessorCompiler implements JavaCompiler { - final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + final JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler(); final MavenProject project; final MavenSession session; - final BuildPluginManager pluginManager; final CompilerManager plexusCompiler; - AnnotationProcessorCompiler( CompilerManager plexusCompiler, MavenProject project, MavenSession session, BuildPluginManager pluginManager ) { + public static JavaCompiler createOutProcess( CompilerManager plexusCompiler, MavenProject project, MavenSession session ) { + + return new AnnotationProcessorCompiler( plexusCompiler, project, session); + } + + public static JavaCompiler createInProcess() { + + return ToolProvider.getSystemJavaCompiler(); + } + + private AnnotationProcessorCompiler( CompilerManager plexusCompiler, MavenProject project, MavenSession session ) { this.project = project; this.session = session; - this.pluginManager = pluginManager; this.plexusCompiler = plexusCompiler; + } - private void printCommand( final org.codehaus.plexus.compiler.Compiler javac, final CompilerConfiguration javacConf ) throws CompilerException { - System.out.println(); - System.out.println(); - System.out.println( "javac \\"); + private void printCommand( final org.codehaus.plexus.compiler.Compiler javac, + final CompilerConfiguration javacConf, + final java.io.PrintWriter out ) throws CompilerException + { + out.println(); + out.println(); + out.println( "javac \\"); for( String c : javac.createCommandLine(javacConf) ) - System.out.printf( "%s \\\n", c); - System.out.println(); - System.out.println(); - System.out.println(); + out.printf( "%s \\\n", c); + out.println(); + out.println(); + out.println(); } - private void executePlugin(final Iterable options, final Iterable compilationUnits) throws Exception { + + + private void execute( final Iterable options, + final Iterable compilationUnits, + final Writer w ) throws Exception + { + final java.io.PrintWriter out = ((w instanceof java.io.PrintWriter) ? + ((java.io.PrintWriter)w) : new java.io.PrintWriter(w)); final CompilerConfiguration javacConf = new CompilerConfiguration(); @@ -299,8 +318,8 @@ else if( "-s".equals(option) ) { javacConf.setGeneratedSourcesDirectory( new java.io.File(ii.next())); } - javacConf.setSourceVersion("1.7"); - javacConf.setTargetVersion("1.7"); + javacConf.setSourceVersion( project.getProperties().getProperty("maven.compiler.source", "1.7") ); + javacConf.setTargetVersion( project.getProperties().getProperty("maven.compiler.target", "1.7")); javacConf.setWorkingDirectory(project.getBasedir()); final java.util.Set sourceFiles = @@ -321,18 +340,18 @@ else if( "-s".equals(option) ) { // USING STANDARD PLEXUS /* - final org.codehaus.plexus.compiler.Compiler javac = plexusCompiler.getCompiler("javac"); - //printCommand(javac, javacConf); + final org.codehaus.plexus.systemJavaCompiler.Compiler javac = plexusCompiler.getCompiler("javac"); + //printCommand(javac, javacConf, out ); result = javac.performCompile( javacConf ); for( CompilerMessage m : result.getCompilerMessages()) - System.out.println( m.getMessage() ); + out.println( m.getMessage() ); */ // USING CUSTOM PLEXUS result = PlexusJavaCompilerWithOutput.INSTANCE.performCompile(javacConf); - System.out.println( result.toString() ); + out.println( result.toString() ); out.flush(); } @Override @@ -359,7 +378,7 @@ public void setLocale(Locale locale) { @Override public Boolean call() { try { - executePlugin(options, compilationUnits); + execute(options, compilationUnits, out); return true; } catch (final Exception ex) { diagnosticListener.report( new Diagnostic() { @@ -421,7 +440,7 @@ public String toString() { @Override public StandardJavaFileManager getStandardFileManager(DiagnosticListener diagnosticListener, Locale locale, Charset charset) { - return compiler.getStandardFileManager(diagnosticListener, locale, charset); + return systemJavaCompiler.getStandardFileManager(diagnosticListener, locale, charset); } @Override diff --git a/test/pom.xml b/test/pom.xml index 96bcfb0..7785155 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -4,12 +4,14 @@ org.bsc.maven maven-processor-plugin-test - 3.2.1-SNAPSHOT + 3.3-SNAPSHOT jar MAVEN PROCESSOR PLUGIN TEST- ${project.version} + 1.7 + 1.7 @@ -33,17 +35,6 @@ - - org.apache.maven.plugins - maven-compiler-plugin - 3.6.1 - - 1.7 - 1.7 - none - - - + + + + ---> + process - process + process process-classes - ${basedir}/src/site - - - ${basedir}/src/main/java - - - - false - - org.bsc.maven.plugin.processor.test.TESTWikiProcessor - - - - - - true + ${basedir}/src/site + + + ${basedir}/src/main/java + + + + false + + org.bsc.maven.plugin.processor.test.TESTWikiProcessor + + + + + + true - + - - org.codehaus.mojo - build-helper-maven-plugin - 1.7 - - - add-source - generate-sources - - add-source - - - - ${basedir}/src-generated/src - - - - - add-test-source - generate-test-sources - - add-test-source - - - - ${basedir}/src-generated/test - - - - - - + + org.codehaus.mojo + build-helper-maven-plugin + + + add-source + generate-sources + + add-source + + + + ${basedir}/src-generated/src + + + + + add-test-source + generate-test-sources + + add-test-source + + + + ${basedir}/src-generated/test + + + + + + + + + toolchain + + + + + org.apache.maven.plugins + maven-toolchains-plugin + + + + toolchain + + + + + + + 1.6 + sun + + + + + + + + + + + + From 4e81968ea5ee53848772cab13b69383b116e2371 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Sun, 9 Apr 2017 13:10:32 +0200 Subject: [PATCH 08/14] add support of env vars : maven.processor.source & maven.processor.target --- .../processor/AnnotationProcessorCompiler.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java b/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java index 9a9c025..c5e3fd1 100644 --- a/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java +++ b/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java @@ -239,6 +239,14 @@ protected CompilerResult performCompile( CompilerConfiguration config ) throws C * @author softphone */ public class AnnotationProcessorCompiler implements JavaCompiler { + private static final String COMPILER_TARGET = "maven.compiler.target"; + private static final String COMPILER_SOURCE = "maven.compiler.source"; + + private static final String PROCESSOR_TARGET = "maven.processor.target"; + private static final String PROCESSOR_SOURCE = "maven.processor.source"; + + private static final String DEFAULT_SOURCE_VERSION = "1.7"; + private static final String DEFAULT_TARGET_VERSION = DEFAULT_SOURCE_VERSION ; final JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler(); @@ -379,8 +387,13 @@ else if( "-s".equals(option) ) { javacConf.setGeneratedSourcesDirectory( new java.io.File(ii.next())); } - javacConf.setSourceVersion( project.getProperties().getProperty("maven.compiler.source", "1.7") ); - javacConf.setTargetVersion( project.getProperties().getProperty("maven.compiler.target", "1.7")); + final java.util.Properties props = project.getProperties(); + + final String sourceVersion = props.getProperty(PROCESSOR_SOURCE,props.getProperty(COMPILER_SOURCE, DEFAULT_SOURCE_VERSION)); + final String targetVersion = props.getProperty(PROCESSOR_TARGET,props.getProperty(COMPILER_TARGET, DEFAULT_TARGET_VERSION)); + + javacConf.setSourceVersion(sourceVersion ); + javacConf.setTargetVersion(targetVersion); javacConf.setWorkingDirectory(project.getBasedir()); final java.util.Set sourceFiles = From fc0f4631c9e0a087503ec55ff282e5cd26eab8e9 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Sun, 9 Apr 2017 19:33:55 +0200 Subject: [PATCH 09/14] prepare for release --- pom.xml | 2 +- test/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 548c4da..aa5038a 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.bsc.maven maven-processor-plugin maven-plugin - 3.3-SNAPSHOT + 3.3 MAVEN PROCESSOR PLUGIN - ${project.version} A maven plugin to process annotation for jdk6 at compile time diff --git a/test/pom.xml b/test/pom.xml index bbeb609..9a23748 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -4,7 +4,7 @@ org.bsc.maven maven-processor-plugin-test - 3.3-SNAPSHOT + 3.3 jar MAVEN PROCESSOR PLUGIN TEST- ${project.version} From 83635f07abf09eb5b475717369c044cf5184c3ad Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Sun, 9 Apr 2017 19:44:03 +0200 Subject: [PATCH 10/14] update readme --- README.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 36bec2d..bff5f99 100644 --- a/README.md +++ b/README.md @@ -5,20 +5,28 @@     -This plugin helps to use from maven the new annotation processing integrated in java compiler provided from JDK6 +This plugin helps to use from maven the new annotation processing integrated in java compiler provided from JDK6 This plugin could be considered the 'alter ego' of maven apt plugin http://mojo.codehaus.org/apt-maven-plugin/ -### [Documentation - v3](http://bsorrentino.github.io/maven-annotation-plugin/index.html) +### Documentation -### [Old documentation - v2](http://bsorrentino.github.io/maven-annotation-plugin/site2/index.html) +* [Version 3.X](http://bsorrentino.github.io/maven-annotation-plugin/index.html) + +* [Version 2 (old)](http://bsorrentino.github.io/maven-annotation-plugin/site2/index.html) ## Related plugin ## -| [m2e-apt](https://github.com/jbosstools/m2e-apt) | eclipse plugin from Jboss | -|:-------------------------------------------------|:--------------------------| -| [m2e-annotations](https://github.com/ilx/m2e-annotations) | eclipse plugin | + [m2e-apt](https://github.com/jbosstools/m2e-apt) | eclipse plugin from Jboss +----|---- + [m2e-annotations](https://github.com/ilx/m2e-annotations) | eclipse plugin ## Releases ## +Apr ,2017 | **Release 3.3**. | Available on **[MAVEN CENTRAL REPO](http://search.maven.org/#artifactdetails%7Corg.bsc.maven%7Cmaven-processor-plugin%7C3.3%7Cmaven-plugin)** | +----|----|---- + +* [Issue 64](https://github.com/bsorrentino/maven-annotation-plugin/issues/64) - Add option to `fork` for JDK9 support +* [Issue 65](https://github.com/bsorrentino/maven-annotation-plugin/issues/65) - Add support for `maven-toolchains-plugin` + |Oct 7,2016 | **Release 3.2.0**. | Available on **[MAVEN CENTRAL REPO](http://search.maven.org/#artifactdetails%7Corg.bsc.maven%7Cmaven-processor-plugin%7C3.2.0%7Cmaven-plugin)** | |:-----------|:-------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------| From b36c0435ecb71e21e34c59c0ef2afe8d94ff50cb Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Sun, 9 Apr 2017 19:59:47 +0200 Subject: [PATCH 11/14] update README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bff5f99..5f3ccb9 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,14 @@ This plugin could be considered the 'alter ego' of maven apt plugin http://mojo. ### Documentation -* [Version 3.X](http://bsorrentino.github.io/maven-annotation-plugin/index.html) +* [Version 3](http://bsorrentino.github.io/maven-annotation-plugin/index.html) * [Version 2 (old)](http://bsorrentino.github.io/maven-annotation-plugin/site2/index.html) ## Related plugin ## - [m2e-apt](https://github.com/jbosstools/m2e-apt) | eclipse plugin from Jboss +[m2e-apt](https://github.com/jbosstools/m2e-apt) | eclipse plugin from Jboss ----|---- - [m2e-annotations](https://github.com/ilx/m2e-annotations) | eclipse plugin +[m2e-annotations](https://github.com/ilx/m2e-annotations) | eclipse plugin ## Releases ## Apr ,2017 | **Release 3.3**. | Available on **[MAVEN CENTRAL REPO](http://search.maven.org/#artifactdetails%7Corg.bsc.maven%7Cmaven-processor-plugin%7C3.3%7Cmaven-plugin)** | From 165ad0853b3ed37a03e6356a54f02b5770d6edd0 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Mon, 10 Apr 2017 15:18:45 +0200 Subject: [PATCH 12/14] update site doc --- src/site/apt/usage.apt | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/site/apt/usage.apt b/src/site/apt/usage.apt index 3db60a5..967fce1 100644 --- a/src/site/apt/usage.apt +++ b/src/site/apt/usage.apt @@ -8,16 +8,7 @@ Repository - - - + sonatype-repo From fa257b153e9cb95cc632cffe50764fab1f10d10a Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Mon, 10 Apr 2017 15:20:19 +0200 Subject: [PATCH 13/14] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5f3ccb9..f67bf10 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ This plugin could be considered the 'alter ego' of maven apt plugin http://mojo. [m2e-annotations](https://github.com/ilx/m2e-annotations) | eclipse plugin ## Releases ## -Apr ,2017 | **Release 3.3**. | Available on **[MAVEN CENTRAL REPO](http://search.maven.org/#artifactdetails%7Corg.bsc.maven%7Cmaven-processor-plugin%7C3.3%7Cmaven-plugin)** | +Apr 10,2017 | **Release 3.3**. | Available on **[MAVEN CENTRAL REPO](http://search.maven.org/#artifactdetails%7Corg.bsc.maven%7Cmaven-processor-plugin%7C3.3%7Cmaven-plugin)** | ----|----|---- * [Issue 64](https://github.com/bsorrentino/maven-annotation-plugin/issues/64) - Add option to `fork` for JDK9 support From 063f735be3a422793512c43cad1dcdba9368be90 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Mon, 10 Apr 2017 19:34:50 +0200 Subject: [PATCH 14/14] update comments --- pom.xml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index aa5038a..3523f13 100644 --- a/pom.xml +++ b/pom.xml @@ -243,9 +243,9 @@ This plugin could be considered the 'alter ego' of maven apt plugin http://mojo. org.apache.maven.plugins @@ -287,6 +287,7 @@ This plugin could be considered the 'alter ego' of maven apt plugin http://mojo.