Skip to content

Commit

Permalink
issue #93 - test for issue verification
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Feb 14, 2021
1 parent ee1900e commit dd57b08
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
*/
// 3.1.0
import static java.lang.String.format;
import static java.util.Optional.empty;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;

Expand Down Expand Up @@ -667,6 +668,32 @@ 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 All @@ -687,12 +714,10 @@ private void executeWithExceptionsHandled() throws Exception
if( addCompileSourceRoots ) {
final java.util.List<String> sourceRoots = project.getCompileSourceRoots();
if( sourceRoots != null ) {

for( String s : sourceRoots ) {
sourceDirs.add( new File(s) );
}
}

}

if( additionalSourceDirectories != null && !additionalSourceDirectories.isEmpty() ) {
Expand Down Expand Up @@ -768,48 +793,20 @@ private void executeWithExceptionsHandled() throws Exception
//
// add to allSource the files coming out from source archives
//
final List<JavaFileObject> allSources = new java.util.ArrayList<>();
final java.util.List<JavaFileObject> allSources = new java.util.ArrayList<>();

processSourceArtifacts( artifact -> {
try {

File f = artifact.getFile();

ZipFile zipFile = new ZipFile(f);
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]", artifact.getFile().getPath()));
getLog().debug(ex);
}
});
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;
}
if( tc != null ) { fork = true; }

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

//compileLock.lock();
try {
Expand All @@ -836,7 +833,6 @@ private void executeWithExceptionsHandled() throws Exception
if( files!=null && !files.isEmpty() ) {

for( JavaFileObject f : fileManager.getJavaFileObjectsFromFiles(files) ) {

allSources.add(f);
};

Expand All @@ -851,7 +847,7 @@ private void executeWithExceptionsHandled() throws Exception
getLog().info( "no source file(s) change(s) detected! Processor task will be skipped");
return;
}
final List<String> options = prepareOptions( compiler );
final java.util.List<String> options = prepareOptions( compiler );

final CompilationTask task = compiler.getTask(
new PrintWriter(System.out),
Expand Down Expand Up @@ -953,37 +949,28 @@ private boolean matchArtifact( Artifact dep/*, ArtifactFilter filter*/ ) {

for( String a : processSourceArtifacts ) {

if( a == null || a.isEmpty() ) {
continue;
}
if( a == null || a.isEmpty() ) continue;

final String [] token = a.split(":");

final boolean matchGroupId = dep.getGroupId().equals(token[0]);

if( !matchGroupId ) {
continue;
}

if( token.length == 1 ) {
return true;
}
if( !matchGroupId ) continue;

if( token[1].equals("*") ) {
return true;

}
if( token.length == 1 ) return true;

if( token[1].equals("*") ) return true;

return dep.getArtifactId().equals(token[1]);

}
return false;
}

private Artifact resolveSourceArtifact( Artifact dep ) throws ArtifactResolutionException {
private Optional<Artifact> resolveSourceArtifact( Artifact dep ) throws ArtifactResolutionException {

if( !matchArtifact(dep) ) {
return null;
return empty();
}

final ArtifactTypeRegistry typeReg = repoSession.getArtifactTypeRegistry();
Expand All @@ -1004,7 +991,7 @@ private Artifact resolveSourceArtifact( Artifact dep ) throws ArtifactResolution

final ArtifactResult result = repoSystem.resolveArtifact( repoSession, request );

return RepositoryUtils.toArtifact(result.getArtifact());
return ofNullable(RepositoryUtils.toArtifact(result.getArtifact()));
}

private void processSourceArtifacts( Consumer<Artifact> closure ) {
Expand All @@ -1020,11 +1007,8 @@ private void processSourceArtifacts( Consumer<Artifact> closure ) {
}
else {
try {
final Artifact sourcesDep = resolveSourceArtifact(dep);
if( sourcesDep != null ) {
closure.accept(sourcesDep);
}

resolveSourceArtifact(dep).ifPresent(closure::accept);

} catch (ArtifactResolutionException ex) {
getLog().warn( format(" sources for artifact [%s] not found!", dep.toString()));
getLog().debug(ex);
Expand Down
51 changes: 47 additions & 4 deletions test/app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ PROCESSOR PLUGIN
<artifactId>maven-processor-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>

<id>process</id>
<execution>
<id>test</id>
<goals>
<goal>process</goal>
</goals>
<phase>process-sources</phase>
<!--phase>process-sources</phase-->
<configuration>
<!--
<additionalSourceDirectories>
Expand Down Expand Up @@ -102,7 +102,7 @@ PROCESSOR PLUGIN
</execution>
</executions>
</plugin>

<!--
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
Expand Down Expand Up @@ -134,11 +134,53 @@ PROCESSOR PLUGIN
</executions>
</plugin>
-->
</plugins>

</build>

<profiles>

<profile>
<id>issue93</id>
<dependencies>

<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
<classifier>sources</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>issue93</id>
<goals><goal>process</goal></goals>
<phase>compile</phase>
<configuration>
<appendSourceArtifacts>true</appendSourceArtifacts>
<processSourceArtifacts>
<processSourceArtifact>commons-collections:commons-collections</processSourceArtifact>
</processSourceArtifacts>
<failOnError>true</failOnError>
<processors>
<processor>org.bsc.maven.plugin.processor.test.TestGenerateSourceProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>

</plugins>

</build>
</profile>
<profile>
<id>docker</id>
<build>
Expand Down Expand Up @@ -194,6 +236,7 @@ PROCESSOR PLUGIN

</build>
</profile>

<profile>
<id>toolchain</id>
<build>
Expand Down

0 comments on commit dd57b08

Please sign in to comment.