Skip to content

Commit

Permalink
issue #93
Browse files Browse the repository at this point in the history
added UnzipService to extract Source Artifacts to the file system
  • Loading branch information
bsorrentino committed Mar 1, 2021
1 parent 3078349 commit c3651eb
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static java.util.Optional.ofNullable;

class PlexusJavaCompilerWithOutput {

Expand Down Expand Up @@ -245,7 +251,7 @@ public class AnnotationProcessorCompiler implements JavaCompiler {
final MavenProject project;
final MavenSession session;
final CompilerManager plexusCompiler;
final Toolchain toolchain;
final Toolchain _toolchain;

public static JavaCompiler createOutProcess( Toolchain toolchain,
CompilerManager plexusCompiler,
Expand Down Expand Up @@ -286,10 +292,13 @@ private AnnotationProcessorCompiler( Toolchain toolchain,
this.project = project;
this.session = session;
this.plexusCompiler = plexusCompiler;
this.toolchain = toolchain;
this._toolchain = toolchain;

}

public Optional<Toolchain> getToolchain() {
return ofNullable(_toolchain);
}

private boolean execute( final Iterable<String> options,
final Iterable<? extends JavaFileObject> compilationUnits,
Expand Down Expand Up @@ -355,23 +364,26 @@ else if("-encoding".equals(option)) {
javacConf.setWorkingDirectory(project.getBasedir());

final java.util.Set<java.io.File> sourceFiles =
new java.util.HashSet<java.io.File>();
for( JavaFileObject src : compilationUnits ) {
sourceFiles.add( new java.io.File( src.toUri() ) );
}
StreamSupport.stream( compilationUnits.spliterator(), false )
.map( unit -> unit.toUri() )
//.peek( System.out::println )
//.filter( uri -> "file".equalsIgnoreCase(uri.getScheme()))
.map( java.io.File::new )
.collect(Collectors.toSet())
;

javacConf.setSourceFiles(sourceFiles);
javacConf.setDebug(false);
javacConf.setFork(true);
javacConf.setVerbose(false);

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

CompilerResult result;
CompilerResult result;

// USING STANDARD PLEXUS
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@
*/
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.project.MavenProject;
import org.apache.maven.toolchain.Toolchain;
Expand All @@ -33,6 +18,21 @@
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;

import javax.annotation.processing.Processor;
import javax.lang.model.SourceVersion;
import javax.tools.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static java.util.Optional.ofNullable;

class PlexusJavaCompilerWithOutput {

static final PlexusJavaCompilerWithOutput INSTANCE = new PlexusJavaCompilerWithOutput();
Expand Down Expand Up @@ -250,7 +250,7 @@ public class AnnotationProcessorCompiler implements JavaCompiler {
final MavenProject project;
final MavenSession session;
final CompilerManager plexusCompiler;
final Toolchain toolchain;
final Toolchain _toolchain;

public static JavaCompiler createOutProcess( Toolchain toolchain,
CompilerManager plexusCompiler,
Expand Down Expand Up @@ -291,12 +291,15 @@ private AnnotationProcessorCompiler( Toolchain toolchain,
this.project = project;
this.session = session;
this.plexusCompiler = plexusCompiler;
this.toolchain = toolchain;
this._toolchain = toolchain;

}


private boolean execute( final Iterable<String> options,

public Optional<Toolchain> getToolchain() {
return ofNullable(_toolchain);
}

private boolean execute(final Iterable<String> options,
final Iterable<? extends JavaFileObject> compilationUnits,
final Writer w ) throws Exception
{
Expand Down Expand Up @@ -360,21 +363,24 @@ else if("-encoding".equals(option)) {
javacConf.setWorkingDirectory(project.getBasedir());

final java.util.Set<java.io.File> sourceFiles =
new java.util.HashSet<java.io.File>();
for( JavaFileObject src : compilationUnits ) {
sourceFiles.add( new java.io.File( src.toUri() ) );
}
StreamSupport.stream( compilationUnits.spliterator(), false )
.map( unit -> unit.toUri() )
//.peek( System.out::println )
//.filter( uri -> "file".equalsIgnoreCase(uri.getScheme()))
.map( java.io.File::new )
.collect(Collectors.toSet())
;

javacConf.setSourceFiles(sourceFiles);
javacConf.setDebug(false);
javacConf.setFork(true);
javacConf.setVerbose(false);

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

CompilerResult result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,32 +668,6 @@ private boolean areSourceFilesSameAsPreviousRun(List<JavaFileObject> allSources)
}
}

private void extractSourcesFromArtifact( Artifact artifact, java.util.List<JavaFileObject> allSources ) {

final File f = artifact.getFile();

try {

final ZipFile zipFile = new ZipFile(f);
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
int sourceCount = 0;

while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();

if (entry.getName().endsWith(".java")) {
++sourceCount;
allSources.add(ZipFileObject.create(zipFile, entry));
}
}
getLog().debug(format("** Discovered %d java sources in %s", sourceCount, f.getAbsolutePath()));

} catch (Exception ex) {
getLog().warn(format("Problem reading source archive [%s]", f.getPath()));
getLog().debug(ex);
}
}

private void executeWithExceptionsHandled() throws Exception
{
if (outputDirectory == null)
Expand Down Expand Up @@ -790,25 +764,33 @@ private void executeWithExceptionsHandled() throws Exception

}

//
// add to allSource the files coming out from source archives
//
final java.util.List<JavaFileObject> allSources = new java.util.ArrayList<>();

processSourceArtifacts( artifact ->
extractSourcesFromArtifact( artifact, allSources) );

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

final Toolchain tc = getToolchain(jdkToolchain);

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

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


//
// add to allSource the files coming out from source archives
//
final java.util.List<JavaFileObject> allSources = new java.util.ArrayList<>();

final UnzipService unzip = UnzipService.newInstance( getLog() );

if( fork ) {
processSourceArtifacts( artifact -> unzip.extractSourcesFromArtifactToTempDirectory( artifact, allSources ) );
}
else {
processSourceArtifacts( artifact -> unzip.extractSourcesFromArtifact(artifact, allSources) );
}

//compileLock.lock();

try {

final JavaCompiler compiler = (fork) ?
Expand Down

0 comments on commit c3651eb

Please sign in to comment.