Skip to content

Commit

Permalink
Merge branch 'feature/issue#80' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Apr 17, 2020
2 parents 8efac77 + 18212a3 commit 74eb57f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
/*
Expand All @@ -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;


/**
Expand All @@ -83,11 +84,7 @@
*/
public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo
{
interface ArtifactClosure {

void execute( Artifact artifact );
}


private static final String SOURCE_CLASSIFIER = "sources";

/**
Expand Down Expand Up @@ -358,6 +355,8 @@ private String buildProcessor()
return result.toString();
}

protected abstract java.util.Set<String> getResourcesElements( java.util.Set<String> result );

protected abstract java.util.Set<String> getClasspathElements( java.util.Set<String> result );

private String buildCompileSourcepath( Consumer<String> onSuccess) {
Expand All @@ -379,7 +378,11 @@ private String buildCompileClasspath()
{

final java.util.Set<String> pathElements = new java.util.LinkedHashSet<>();


getResourcesElements(pathElements);

getClasspathElements(pathElements);

if( pluginArtifacts!=null ) {

for( Artifact a : pluginArtifacts ) {
Expand All @@ -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 ) {
Expand All @@ -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) );
}

/**
*
Expand Down Expand Up @@ -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<File> sourceDirs = getSourceDirectories(new java.util.HashSet<File>( 5 ));
java.util.Set<File> sourceDirs = getSourceDirectories(new java.util.HashSet<>( 5 ));

if( addCompileSourceRoots ) {
final java.util.List<String> sourceRoots = project.getCompileSourceRoots();
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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<Artifact> 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());
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> classpathElements;

/**
* project sourceDirectory
Expand Down Expand Up @@ -94,17 +95,21 @@ public java.util.Set<File> getSourceDirectories( final java.util.Set<File> resul
}

@Override
@SuppressWarnings("unchecked")
protected java.util.Set<String> getClasspathElements( final java.util.Set<String> result) {

List<Resource> resources = project.getResources();
protected Set<String> getResourcesElements(Set<String> result) {
final List<Resource> resources = project.getResources();

if( resources!=null ) {
for( Resource r : resources ) {
result.add(r.getDirectory());
}
}

return result;
}

@Override
protected java.util.Set<String> getClasspathElements( final java.util.Set<String> result) {

result.addAll( classpathElements );

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> classpathElements;


/**
Expand All @@ -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;
}

Expand All @@ -92,10 +90,8 @@ public java.util.Set<File> getSourceDirectories( final java.util.Set<File> resul
return result;
}


@SuppressWarnings("unchecked")
@Override
protected java.util.Set<String> getClasspathElements( final java.util.Set<String> result ) {
protected Set<String> getResourcesElements(Set<String> result) {
final List<Resource> resources = project.getTestResources();

if( resources!=null ) {
Expand All @@ -104,6 +100,12 @@ protected java.util.Set<String> getClasspathElements( final java.util.Set<String
}
}

return result;
}

@Override
protected java.util.Set<String> getClasspathElements( final java.util.Set<String> result ) {

result.addAll( classpathElements );

return result;
Expand Down

0 comments on commit 74eb57f

Please sign in to comment.