From dd57b08f851bec36a5d492fc27a6982132e63967 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Sun, 14 Feb 2021 17:20:57 +0100 Subject: [PATCH] issue #93 - test for issue verification --- .../AbstractAnnotationProcessorMojo.java | 102 ++++++++---------- test/app/pom.xml | 51 ++++++++- 2 files changed, 90 insertions(+), 63 deletions(-) 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 837c9da..9f7c7b7 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 @@ -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; @@ -667,6 +668,32 @@ private boolean areSourceFilesSameAsPreviousRun(List allSources) } } + private void extractSourcesFromArtifact( Artifact artifact, java.util.List allSources ) { + + final File f = artifact.getFile(); + + try { + + final ZipFile zipFile = new ZipFile(f); + final Enumeration 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) @@ -687,12 +714,10 @@ private void executeWithExceptionsHandled() throws Exception if( addCompileSourceRoots ) { final java.util.List sourceRoots = project.getCompileSourceRoots(); if( sourceRoots != null ) { - for( String s : sourceRoots ) { sourceDirs.add( new File(s) ); } } - } if( additionalSourceDirectories != null && !additionalSourceDirectories.isEmpty() ) { @@ -768,34 +793,10 @@ private void executeWithExceptionsHandled() throws Exception // // add to allSource the files coming out from source archives // - final List allSources = new java.util.ArrayList<>(); + final java.util.List allSources = new java.util.ArrayList<>(); - processSourceArtifacts( artifact -> { - try { - - File f = artifact.getFile(); - - ZipFile zipFile = new ZipFile(f); - Enumeration 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 jdkToolchain = java.util.Collections.emptyMap(); @@ -803,13 +804,9 @@ private void executeWithExceptionsHandled() throws Exception 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 { @@ -836,7 +833,6 @@ private void executeWithExceptionsHandled() throws Exception if( files!=null && !files.isEmpty() ) { for( JavaFileObject f : fileManager.getJavaFileObjectsFromFiles(files) ) { - allSources.add(f); }; @@ -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 options = prepareOptions( compiler ); + final java.util.List options = prepareOptions( compiler ); final CompilationTask task = compiler.getTask( new PrintWriter(System.out), @@ -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 resolveSourceArtifact( Artifact dep ) throws ArtifactResolutionException { if( !matchArtifact(dep) ) { - return null; + return empty(); } final ArtifactTypeRegistry typeReg = repoSession.getArtifactTypeRegistry(); @@ -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 closure ) { @@ -1020,11 +1007,8 @@ private void processSourceArtifacts( Consumer 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); diff --git a/test/app/pom.xml b/test/app/pom.xml index 51df1e8..d4ec654 100644 --- a/test/app/pom.xml +++ b/test/app/pom.xml @@ -50,13 +50,13 @@ PROCESSOR PLUGIN maven-processor-plugin ${project.version} - - process + + test process - process-sources + + + + issue93 + + + + commons-collections + commons-collections + 3.2.1 + sources + + + + + + org.bsc.maven + maven-processor-plugin + ${project.version} + + + issue93 + process + compile + + true + + commons-collections:commons-collections + + true + + org.bsc.maven.plugin.processor.test.TestGenerateSourceProcessor + + + + + + + + + + docker @@ -194,6 +236,7 @@ PROCESSOR PLUGIN + toolchain