-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lazy providers and better error reporting (#361)
* ITs need plugin dependency to plexus-compiler-manager While testing #347, changes in compiler manager were not pulled into ITs, because Maven Compiler has a dependency on it, which must be overridden in all ITs or in projects using Plexus Compiler generally, if they need to override the version predefined by Maven Compiler. * Lazy providers and better error reporting If scanning, injection or construction fails, log a comprehensive error message on top of throwing a NoSuchCompilerException. Fixes #347. Co-authored-by: Alexander Kriegisch <[email protected]> * Code review: throw exception with cause In order to be able to do that at all, I had to add a constructor taking a throwable first. Now, even though a cause is propagated, at the time of writing this Maven Compiler will just catch the NoSuchCompilerException we throw, ignore its message and root cause and throw a new MojoExecutionException instead. :-/ Relates to #347. * Code review: improve DefaultCompilerManager.ERROR_MESSAGE Add more detail concerning possible user errors like misspelling the compiler ID or missing dependencies for a compiler. Relates to #347. --------- Co-authored-by: Tamas Cservenak <[email protected]>
- Loading branch information
Showing
12 changed files
with
81 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,30 +25,52 @@ | |
*/ | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Provider; | ||
|
||
import java.util.Map; | ||
|
||
import org.codehaus.plexus.compiler.Compiler; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Trygve Laugstøl</a> | ||
*/ | ||
@Named | ||
public class DefaultCompilerManager implements CompilerManager { | ||
private static final String ERROR_MESSAGE = "Compiler '{}' could not be instantiated or injected properly. " | ||
+ "If you spelled the compiler ID correctly and all necessary dependencies are on the classpath, " | ||
+ "then next you can try running the build with -Dsisu.debug, looking for exceptions."; | ||
private static final String ERROR_MESSAGE_DETAIL = "TypeNotPresentException caused by UnsupportedClassVersionError " | ||
+ "might indicate, that the compiler needs a more recent Java runtime. " | ||
+ "IllegalArgumentException in ClassReader.<init> might mean, that you need to upgrade Maven."; | ||
|
||
@Inject | ||
private Map<String, Compiler> compilers; | ||
private Map<String, Provider<Compiler>> compilers; | ||
|
||
private final Logger log = LoggerFactory.getLogger(getClass()); | ||
|
||
// ---------------------------------------------------------------------- | ||
// CompilerManager Implementation | ||
// ---------------------------------------------------------------------- | ||
|
||
public Compiler getCompiler(String compilerId) throws NoSuchCompilerException { | ||
Compiler compiler = compilers.get(compilerId); | ||
// Provider<Class> is lazy -> presence of provider means compiler is present, but not yet constructed | ||
Provider<Compiler> compilerProvider = compilers.get(compilerId); | ||
|
||
if (compiler == null) { | ||
if (compilerProvider == null) { | ||
// Compiler could not be injected for some reason | ||
log.error(ERROR_MESSAGE + " " + ERROR_MESSAGE_DETAIL, compilerId); | ||
throw new NoSuchCompilerException(compilerId); | ||
} | ||
|
||
return compiler; | ||
// Provider exists, but compiler was not created yet | ||
try { | ||
return compilerProvider.get(); | ||
} catch (Exception e) { | ||
// DI could not construct compiler | ||
log.error(ERROR_MESSAGE, compilerId); | ||
throw new NoSuchCompilerException(compilerId, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters