diff --git a/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java b/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java index 56f0a72..1b99d56 100644 --- a/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java +++ b/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java @@ -19,26 +19,6 @@ package org.bsc.maven.plugin.processor; -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; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.List; -import java.util.Map; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; -import java.util.function.Consumer; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import javax.tools.JavaCompiler.CompilationTask; -import javax.tools.*; -import javax.tools.Diagnostic.Kind; import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; @@ -52,6 +32,35 @@ import org.codehaus.plexus.compiler.manager.CompilerManager; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.StringUtils; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.ArtifactTypeRegistry; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.repository.RemoteRepository; +import org.eclipse.aether.resolution.ArtifactRequest; +import org.eclipse.aether.resolution.ArtifactResolutionException; +import org.eclipse.aether.resolution.ArtifactResult; + +import javax.tools.Diagnostic.Kind; +import javax.tools.DiagnosticListener; +import javax.tools.JavaCompiler; +import javax.tools.JavaCompiler.CompilationTask; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +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; +import java.util.*; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; // 3.0.5 /* @@ -64,16 +73,8 @@ import org.sonatype.aether.resolution.ArtifactResult; import org.sonatype.aether.util.artifact.DefaultArtifact; */ - // 3.1.0 -import org.eclipse.aether.RepositorySystem; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.artifact.ArtifactTypeRegistry; -import org.eclipse.aether.artifact.DefaultArtifact; -import org.eclipse.aether.repository.RemoteRepository; -import org.eclipse.aether.resolution.ArtifactRequest; -import org.eclipse.aether.resolution.ArtifactResolutionException; -import org.eclipse.aether.resolution.ArtifactResult; +import static java.util.Optional.ofNullable; /** @@ -83,11 +84,7 @@ */ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo { - interface ArtifactClosure { - - void execute( Artifact artifact ); - } - + private static final String SOURCE_CLASSIFIER = "sources"; /** @@ -358,6 +355,8 @@ private String buildProcessor() return result.toString(); } + protected abstract java.util.Set getResourcesElements( java.util.Set result ); + protected abstract java.util.Set getClasspathElements( java.util.Set result ); private String buildCompileSourcepath( Consumer onSuccess) { @@ -379,7 +378,11 @@ private String buildCompileClasspath() { final java.util.Set pathElements = new java.util.LinkedHashSet<>(); - + + getResourcesElements(pathElements); + + getClasspathElements(pathElements); + if( pluginArtifacts!=null ) { for( Artifact a : pluginArtifacts ) { @@ -389,13 +392,13 @@ private String buildCompileClasspath() java.io.File f = a.getFile(); if( f!=null ) pathElements.add( a.getFile().getAbsolutePath() ); + } } } - - getClasspathElements(pathElements); - + + final StringBuilder result = new StringBuilder(); for( String elem : pathElements ) { @@ -404,6 +407,13 @@ private String buildCompileClasspath() return result.toString(); } + private String buildModulePath() + { + + return getClasspathElements(new java.util.LinkedHashSet<>()) + .stream() + .collect(Collectors.joining( File.pathSeparator) ); + } /** * @@ -505,7 +515,7 @@ private void executeWithExceptionsHandled() throws Exception final String includesString = ( includes==null || includes.length==0) ? "**/*.java" : StringUtils.join(includes, ","); final String excludesString = ( excludes==null || excludes.length==0) ? null : StringUtils.join(excludes, ","); - java.util.Set sourceDirs = getSourceDirectories(new java.util.HashSet( 5 )); + java.util.Set sourceDirs = getSourceDirectories(new java.util.HashSet<>( 5 )); if( addCompileSourceRoots ) { final java.util.List sourceRoots = project.getCompileSourceRoots(); @@ -557,7 +567,10 @@ private void executeWithExceptionsHandled() throws Exception options.add("-cp"); options.add(compileClassPath); - + + options.add("--module-path"); + options.add( buildModulePath() ); + buildCompileSourcepath( sourcepath -> { options.add("-sourcepath"); options.add(sourcepath); @@ -581,11 +594,22 @@ private void executeWithExceptionsHandled() throws Exception options.add("-s"); options.add(outputDirectory.getPath()); - if( releaseVersion!=null ) { + ofNullable(releaseVersion).ifPresent( release -> { options.add("--release"); - options.add( releaseVersion ); - } + options.add( releaseVersion ); + }); + ofNullable(project.getProperties()).ifPresent( properties -> { + + ofNullable(properties.getProperty( "maven.compiler.source" )).ifPresent( source -> { + options.add("-source"); + options.add(source); + }); + ofNullable(properties.getProperty( "maven.compiler.target" )) .ifPresent( target -> { + options.add("-target"); + options.add(target); + }); + }); if( getLog().isDebugEnabled() ) { for (String option : options) { @@ -886,13 +910,13 @@ private Artifact resolveSourceArtifact( Artifact dep ) throws ArtifactResolution return RepositoryUtils.toArtifact(result.getArtifact()); } - private void processSourceArtifacts( ArtifactClosure closure ) { + private void processSourceArtifacts( Consumer closure ) { for (Artifact dep : this.project.getDependencyArtifacts()) { if (dep.hasClassifier() && SOURCE_CLASSIFIER.equals(dep.getClassifier()) ) { if( appendSourceArtifacts ) { - closure.execute(dep); + closure.accept(dep); } //getLog().debug("Append source artifact to classpath: " + dep.getGroupId() + ":" + dep.getArtifactId()); //this.sourceArtifacts.add(dep.getFile()); @@ -901,7 +925,7 @@ private void processSourceArtifacts( ArtifactClosure closure ) { try { final Artifact sourcesDep = resolveSourceArtifact(dep); if( sourcesDep != null ) { - closure.execute(sourcesDep); + closure.accept(sourcesDep); } } catch (ArtifactResolutionException ex) { diff --git a/processor/src/main/java/org/bsc/maven/plugin/processor/MainAnnotationProcessorMojo.java b/processor/src/main/java/org/bsc/maven/plugin/processor/MainAnnotationProcessorMojo.java index a8ac2b3..a4fa46b 100644 --- a/processor/src/main/java/org/bsc/maven/plugin/processor/MainAnnotationProcessorMojo.java +++ b/processor/src/main/java/org/bsc/maven/plugin/processor/MainAnnotationProcessorMojo.java @@ -20,6 +20,8 @@ import java.io.File; import java.util.List; +import java.util.Set; + import org.apache.maven.model.Resource; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; @@ -39,10 +41,9 @@ public class MainAnnotationProcessorMojo extends AbstractAnnotationProcessorMojo * project classpath * */ - @SuppressWarnings("rawtypes") //@MojoParameter(expression = "${project.compileClasspathElements}", required = true, readonly = true) @Parameter( defaultValue="${project.compileClasspathElements}", required=true, readonly=true) - private List classpathElements; + private List classpathElements; /** * project sourceDirectory @@ -94,10 +95,8 @@ public java.util.Set getSourceDirectories( final java.util.Set resul } @Override - @SuppressWarnings("unchecked") - protected java.util.Set getClasspathElements( final java.util.Set result) { - - List resources = project.getResources(); + protected Set getResourcesElements(Set result) { + final List resources = project.getResources(); if( resources!=null ) { for( Resource r : resources ) { @@ -105,6 +104,12 @@ protected java.util.Set getClasspathElements( final java.util.Set getClasspathElements( final java.util.Set result) { + result.addAll( classpathElements ); return result; diff --git a/processor/src/main/java/org/bsc/maven/plugin/processor/TestAnnotationProcessorMojo.java b/processor/src/main/java/org/bsc/maven/plugin/processor/TestAnnotationProcessorMojo.java index ebdd5cd..a350e1b 100644 --- a/processor/src/main/java/org/bsc/maven/plugin/processor/TestAnnotationProcessorMojo.java +++ b/processor/src/main/java/org/bsc/maven/plugin/processor/TestAnnotationProcessorMojo.java @@ -20,6 +20,8 @@ import java.io.File; import java.util.List; +import java.util.Set; + import org.apache.maven.model.Resource; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; @@ -39,10 +41,9 @@ public class TestAnnotationProcessorMojo extends AbstractAnnotationProcessorMojo * project classpath * */ - @SuppressWarnings("rawtypes") //@MojoParameter(expression = "${project.testClasspathElements}", required = true, readonly = true) @Parameter( defaultValue="${project.testClasspathElements}", required=true, readonly=true) - private List classpathElements; + private List classpathElements; /** @@ -69,19 +70,16 @@ public class TestAnnotationProcessorMojo extends AbstractAnnotationProcessorMojo @Override protected void addCompileSourceRoot(MavenProject project, String dir) { - project.addTestCompileSourceRoot(dir); } @Override public File getDefaultOutputDirectory() { - return defaultOutputDirectory; } @Override protected File getOutputClassDirectory() { - return outputClassDirectory; } @@ -92,10 +90,8 @@ public java.util.Set getSourceDirectories( final java.util.Set resul return result; } - - @SuppressWarnings("unchecked") @Override - protected java.util.Set getClasspathElements( final java.util.Set result ) { + protected Set getResourcesElements(Set result) { final List resources = project.getTestResources(); if( resources!=null ) { @@ -104,6 +100,12 @@ protected java.util.Set getClasspathElements( final java.util.Set getClasspathElements( final java.util.Set result ) { + result.addAll( classpathElements ); return result;