Skip to content

Commit 6bb25d1

Browse files
committed
Fix java8 javac loading
1 parent f296518 commit 6bb25d1

4 files changed

Lines changed: 67 additions & 5 deletions

File tree

maven-plugin/it/mvne

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
export MAVEN_OPTS="-Xdebug -Xnoagent -Xint -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 $MAVEN_OPTS"
4+
./mvnw "$@"

maven-plugin/src/main/java/online/sharedtype/maven/DependencyResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class DependencyResolver {
2828
this.project = project;
2929
}
3030

31-
List<File> getSourceDependencies() throws MojoExecutionException {
31+
List<File> getClasspathDependencies() throws MojoExecutionException {
3232
try {
3333
ArtifactTypeRegistry artifactTypeRegistry =
3434
session.getRepositorySession().getArtifactTypeRegistry();

maven-plugin/src/main/java/online/sharedtype/maven/SharedTypeGenMojo.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
/**
3636
* Generate types from {@link SharedType} annotated classes.
3737
* See <a href="https://github.com/SharedType/sharedtype">SharedType</a> for details.
38+
*
3839
* @author Cause Chung
3940
*/
4041
@Mojo(name = "gen")
4142
public final class SharedTypeGenMojo extends AbstractMojo {
43+
private static final String JAVA8_VERSION = "1.8";
4244
private static final List<String> DEFAULT_COMPILER_OPTIONS = Arrays.asList("-proc:only", "-Asharedtype.enabled=true");
4345
private @Inject RepositorySystem repositorySystem;
4446

@@ -84,13 +86,13 @@ public void execute() throws MojoExecutionException, MojoFailureException {
8486
Files.createDirectories(outputDirPath);
8587
}
8688
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());
8890
} catch (Exception e) {
8991
throw new MojoExecutionException(e);
9092
}
9193
Iterable<? extends JavaFileObject> sources = fileManager.getJavaFileObjectsFromFiles(walkAllSourceFiles());
9294

93-
try(SharedTypeLogger logger = new SharedTypeLogger(getLog())) {
95+
try (SharedTypeLogger logger = new SharedTypeLogger(getLog())) {
9496
JavaCompiler.CompilationTask task = compiler.getTask(logger, fileManager, diagnosticListener, getCompilerOptions(), null, sources);
9597
SharedTypeAnnotationProcessor annotationProcessor = new SharedTypeAnnotationProcessor();
9698
annotationProcessor.setUserProps(properties);
@@ -124,7 +126,18 @@ private List<String> getCompilerOptions() throws MojoFailureException {
124126
}
125127

126128
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+
}
128141
if (compiler != null) {
129142
return compiler;
130143
}

processor/pom.xml

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45
<parent>
56
<groupId>online.sharedtype</groupId>
@@ -63,6 +64,50 @@
6364
</dependency>
6465
</dependencies>
6566

67+
<!-- For JDK 8, need to explicit add tools.jar dependency for Tree API -->
68+
<profiles>
69+
<profile>
70+
<id>jdk-tools-jar-env-var</id>
71+
<activation>
72+
<activeByDefault>true</activeByDefault>
73+
<file>
74+
<exists>${JAVA_HOME}/lib/tools.jar</exists>
75+
</file>
76+
</activation>
77+
<properties>
78+
<!--suppress UnresolvedMavenProperty -->
79+
<toolsjar>${JAVA_HOME}/lib/tools.jar</toolsjar>
80+
</properties>
81+
</profile>
82+
<profile>
83+
<id>jdk-tools-jar-sys-prop</id>
84+
<activation>
85+
<activeByDefault>false</activeByDefault>
86+
<file>
87+
<exists>${java.home}/../lib/tools.jar</exists>
88+
</file>
89+
</activation>
90+
<properties>
91+
<toolsjar>${java.home}/../lib/tools.jar</toolsjar>
92+
</properties>
93+
</profile>
94+
<profile>
95+
<id>java8-jdk-tools-jar</id>
96+
<activation>
97+
<jdk>1.8</jdk>
98+
</activation>
99+
<dependencies>
100+
<dependency>
101+
<groupId>jdk.tools</groupId>
102+
<artifactId>jdk.tools</artifactId>
103+
<version>jdk1.8.0</version>
104+
<scope>system</scope>
105+
<systemPath>${toolsjar}</systemPath>
106+
</dependency>
107+
</dependencies>
108+
</profile>
109+
</profiles>
110+
66111
<build>
67112
<plugins>
68113
<plugin>

0 commit comments

Comments
 (0)