|
35 | 35 | /** |
36 | 36 | * Generate types from {@link SharedType} annotated classes. |
37 | 37 | * See <a href="https://github.com/SharedType/sharedtype">SharedType</a> for details. |
| 38 | + * |
38 | 39 | * @author Cause Chung |
39 | 40 | */ |
40 | 41 | @Mojo(name = "gen") |
41 | 42 | public final class SharedTypeGenMojo extends AbstractMojo { |
| 43 | + private static final String JAVA8_VERSION = "1.8"; |
42 | 44 | private static final List<String> DEFAULT_COMPILER_OPTIONS = Arrays.asList("-proc:only", "-Asharedtype.enabled=true"); |
43 | 45 | private @Inject RepositorySystem repositorySystem; |
44 | 46 |
|
@@ -84,13 +86,13 @@ public void execute() throws MojoExecutionException, MojoFailureException { |
84 | 86 | Files.createDirectories(outputDirPath); |
85 | 87 | } |
86 | 88 | fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outputDirPath.toFile())); |
87 | | - fileManager.setLocation(StandardLocation.CLASS_PATH, dependencyResolver.getSourceDependencies()); |
| 89 | + fileManager.setLocation(StandardLocation.CLASS_PATH, dependencyResolver.getClasspathDependencies()); |
88 | 90 | } catch (Exception e) { |
89 | 91 | throw new MojoExecutionException(e); |
90 | 92 | } |
91 | 93 | Iterable<? extends JavaFileObject> sources = fileManager.getJavaFileObjectsFromFiles(walkAllSourceFiles()); |
92 | 94 |
|
93 | | - try(SharedTypeLogger logger = new SharedTypeLogger(getLog())) { |
| 95 | + try (SharedTypeLogger logger = new SharedTypeLogger(getLog())) { |
94 | 96 | JavaCompiler.CompilationTask task = compiler.getTask(logger, fileManager, diagnosticListener, getCompilerOptions(), null, sources); |
95 | 97 | SharedTypeAnnotationProcessor annotationProcessor = new SharedTypeAnnotationProcessor(); |
96 | 98 | annotationProcessor.setUserProps(properties); |
@@ -124,7 +126,18 @@ private List<String> getCompilerOptions() throws MojoFailureException { |
124 | 126 | } |
125 | 127 |
|
126 | 128 | private static JavaCompiler getJavaCompiler() throws MojoExecutionException { |
127 | | - JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 129 | + String javaVersion = System.getProperty("java.specification.version"); |
| 130 | + JavaCompiler compiler; |
| 131 | + if (JAVA8_VERSION.equals(javaVersion)) { |
| 132 | + try { |
| 133 | + Class<?> javacToolClass = SharedTypeAnnotationProcessor.class.getClassLoader().loadClass("com.sun.tools.javac.api.JavacTool"); |
| 134 | + compiler = (JavaCompiler) javacToolClass.getConstructor().newInstance(); |
| 135 | + } catch (Exception e) { |
| 136 | + throw new MojoExecutionException("Failed to load JavaCompiler.", e); |
| 137 | + } |
| 138 | + } else { |
| 139 | + compiler = ToolProvider.getSystemJavaCompiler(); |
| 140 | + } |
128 | 141 | if (compiler != null) { |
129 | 142 | return compiler; |
130 | 143 | } |
|
0 commit comments