Skip to content

Commit 2248255

Browse files
kriegaexcstamas
andauthored
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 <Alexander@Kriegisch.name> * 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 <tamas@cservenak.net>
1 parent 99014e5 commit 2248255

File tree

12 files changed

+81
-6
lines changed

12 files changed

+81
-6
lines changed

plexus-compiler-its/src/main/it/MCOMPILER-346-mre/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
<artifactId>plexus-compiler-api</artifactId>
6565
<version>${plexus.compiler.version}</version>
6666
</dependency>
67+
<dependency>
68+
<groupId>org.codehaus.plexus</groupId>
69+
<artifactId>plexus-compiler-manager</artifactId>
70+
<version>${plexus.compiler.version}</version>
71+
</dependency>
6772
<dependency>
6873
<groupId>org.codehaus.plexus</groupId>
6974
<artifactId>plexus-compiler-javac</artifactId>

plexus-compiler-its/src/main/it/aspectj-compiler/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
<artifactId>plexus-compiler-api</artifactId>
4949
<version>${plexus.compiler.version}</version>
5050
</dependency>
51+
<dependency>
52+
<groupId>org.codehaus.plexus</groupId>
53+
<artifactId>plexus-compiler-manager</artifactId>
54+
<version>${plexus.compiler.version}</version>
55+
</dependency>
5156
<dependency>
5257
<groupId>org.codehaus.plexus</groupId>
5358
<artifactId>plexus-compiler-aspectj</artifactId>

plexus-compiler-its/src/main/it/eclipse-compiler-mapstruct/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@
5959
<artifactId>plexus-compiler-api</artifactId>
6060
<version>${plexus.compiler.version}</version>
6161
</dependency>
62+
<dependency>
63+
<groupId>org.codehaus.plexus</groupId>
64+
<artifactId>plexus-compiler-manager</artifactId>
65+
<version>${plexus.compiler.version}</version>
66+
</dependency>
6267
<dependency>
6368
<groupId>org.codehaus.plexus</groupId>
6469
<artifactId>plexus-compiler-eclipse</artifactId>

plexus-compiler-its/src/main/it/eclipse-compiler-procpath/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
<artifactId>plexus-compiler-api</artifactId>
6767
<version>${plexus.compiler.version}</version>
6868
</dependency>
69+
<dependency>
70+
<groupId>org.codehaus.plexus</groupId>
71+
<artifactId>plexus-compiler-manager</artifactId>
72+
<version>${plexus.compiler.version}</version>
73+
</dependency>
6974
<dependency>
7075
<groupId>org.codehaus.plexus</groupId>
7176
<artifactId>plexus-compiler-eclipse</artifactId>

plexus-compiler-its/src/main/it/error-prone-compiler/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
<artifactId>plexus-compiler-api</artifactId>
8484
<version>${plexus.compiler.version}</version>
8585
</dependency>
86+
<dependency>
87+
<groupId>org.codehaus.plexus</groupId>
88+
<artifactId>plexus-compiler-manager</artifactId>
89+
<version>${plexus.compiler.version}</version>
90+
</dependency>
8691
<dependency>
8792
<groupId>org.codehaus.plexus</groupId>
8893
<artifactId>plexus-compiler-javac-errorprone</artifactId>

plexus-compiler-its/src/main/it/missing-warnings/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
<artifactId>plexus-compiler-api</artifactId>
2626
<version>${plexus.compiler.version}</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>org.codehaus.plexus</groupId>
30+
<artifactId>plexus-compiler-manager</artifactId>
31+
<version>${plexus.compiler.version}</version>
32+
</dependency>
2833
<dependency>
2934
<groupId>org.codehaus.plexus</groupId>
3035
<artifactId>plexus-compiler-javac</artifactId>
@@ -42,4 +47,4 @@
4247
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4348
<plexus.compiler.version>@pom.version@</plexus.compiler.version>
4449
</properties>
45-
</project>
50+
</project>

plexus-compiler-its/src/main/it/simple-eclipse-compiler-fail/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
<artifactId>plexus-compiler-api</artifactId>
6262
<version>${plexus.compiler.version}</version>
6363
</dependency>
64+
<dependency>
65+
<groupId>org.codehaus.plexus</groupId>
66+
<artifactId>plexus-compiler-manager</artifactId>
67+
<version>${plexus.compiler.version}</version>
68+
</dependency>
6469
<dependency>
6570
<groupId>org.codehaus.plexus</groupId>
6671
<artifactId>plexus-compiler-eclipse</artifactId>

plexus-compiler-its/src/main/it/simple-eclipse-compiler/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
<artifactId>plexus-compiler-api</artifactId>
6262
<version>${plexus.compiler.version}</version>
6363
</dependency>
64+
<dependency>
65+
<groupId>org.codehaus.plexus</groupId>
66+
<artifactId>plexus-compiler-manager</artifactId>
67+
<version>${plexus.compiler.version}</version>
68+
</dependency>
6469
<dependency>
6570
<groupId>org.codehaus.plexus</groupId>
6671
<artifactId>plexus-compiler-eclipse</artifactId>

plexus-compiler-its/src/main/it/simple-javac-fork/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
<artifactId>plexus-compiler-api</artifactId>
6464
<version>${plexus.compiler.version}</version>
6565
</dependency>
66+
<dependency>
67+
<groupId>org.codehaus.plexus</groupId>
68+
<artifactId>plexus-compiler-manager</artifactId>
69+
<version>${plexus.compiler.version}</version>
70+
</dependency>
6671
<dependency>
6772
<groupId>org.codehaus.plexus</groupId>
6873
<artifactId>plexus-compiler-javac</artifactId>

plexus-compiler-its/src/main/it/simple-javac/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
<artifactId>plexus-compiler-api</artifactId>
6464
<version>${plexus.compiler.version}</version>
6565
</dependency>
66+
<dependency>
67+
<groupId>org.codehaus.plexus</groupId>
68+
<artifactId>plexus-compiler-manager</artifactId>
69+
<version>${plexus.compiler.version}</version>
70+
</dependency>
6671
<dependency>
6772
<groupId>org.codehaus.plexus</groupId>
6873
<artifactId>plexus-compiler-javac</artifactId>

plexus-compiler-manager/src/main/java/org/codehaus/plexus/compiler/manager/DefaultCompilerManager.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,52 @@
2525
*/
2626
import javax.inject.Inject;
2727
import javax.inject.Named;
28+
import javax.inject.Provider;
2829

2930
import java.util.Map;
3031

3132
import org.codehaus.plexus.compiler.Compiler;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
3235

3336
/**
3437
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
3538
*/
3639
@Named
3740
public class DefaultCompilerManager implements CompilerManager {
41+
private static final String ERROR_MESSAGE = "Compiler '{}' could not be instantiated or injected properly. "
42+
+ "If you spelled the compiler ID correctly and all necessary dependencies are on the classpath, "
43+
+ "then next you can try running the build with -Dsisu.debug, looking for exceptions.";
44+
private static final String ERROR_MESSAGE_DETAIL = "TypeNotPresentException caused by UnsupportedClassVersionError "
45+
+ "might indicate, that the compiler needs a more recent Java runtime. "
46+
+ "IllegalArgumentException in ClassReader.<init> might mean, that you need to upgrade Maven.";
47+
3848
@Inject
39-
private Map<String, Compiler> compilers;
49+
private Map<String, Provider<Compiler>> compilers;
50+
51+
private final Logger log = LoggerFactory.getLogger(getClass());
4052

4153
// ----------------------------------------------------------------------
4254
// CompilerManager Implementation
4355
// ----------------------------------------------------------------------
4456

4557
public Compiler getCompiler(String compilerId) throws NoSuchCompilerException {
46-
Compiler compiler = compilers.get(compilerId);
58+
// Provider<Class> is lazy -> presence of provider means compiler is present, but not yet constructed
59+
Provider<Compiler> compilerProvider = compilers.get(compilerId);
4760

48-
if (compiler == null) {
61+
if (compilerProvider == null) {
62+
// Compiler could not be injected for some reason
63+
log.error(ERROR_MESSAGE + " " + ERROR_MESSAGE_DETAIL, compilerId);
4964
throw new NoSuchCompilerException(compilerId);
5065
}
5166

52-
return compiler;
67+
// Provider exists, but compiler was not created yet
68+
try {
69+
return compilerProvider.get();
70+
} catch (Exception e) {
71+
// DI could not construct compiler
72+
log.error(ERROR_MESSAGE, compilerId);
73+
throw new NoSuchCompilerException(compilerId, e);
74+
}
5375
}
5476
}

plexus-compiler-manager/src/main/java/org/codehaus/plexus/compiler/manager/NoSuchCompilerException.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ public class NoSuchCompilerException extends Exception {
3131
private final String compilerId;
3232

3333
public NoSuchCompilerException(String compilerId) {
34-
super("No such compiler '" + compilerId + "'.");
34+
this(compilerId, null);
35+
}
3536

37+
public NoSuchCompilerException(String compilerId, Throwable cause) {
38+
super("No such compiler '" + compilerId + "'", cause);
3639
this.compilerId = compilerId;
3740
}
3841

0 commit comments

Comments
 (0)