forked from java9-modularity/gradle-modules-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
java9-modularity#72: support for "modularity" project extension
via ModularityExtension interface
- Loading branch information
1 parent
00c78ce
commit c5b98dc
Showing
6 changed files
with
208 additions
and
0 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
90 changes: 90 additions & 0 deletions
90
src/main/java/org/javamodularity/moduleplugin/extensions/DefaultModularityExtension.java
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.javamodularity.moduleplugin.extensions; | ||
|
||
import org.gradle.api.JavaVersion; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.plugins.JavaPlugin; | ||
import org.gradle.api.tasks.compile.JavaCompile; | ||
import org.javamodularity.moduleplugin.JavaProjectHelper; | ||
|
||
import java.util.List; | ||
|
||
public class DefaultModularityExtension implements ModularityExtension { | ||
|
||
private final Project project; | ||
|
||
public DefaultModularityExtension(Project project) { | ||
this.project = project; | ||
} | ||
|
||
@Override | ||
public void standardJavaRelease(int mainJavaRelease) { | ||
if (mainJavaRelease < 9) { | ||
throw new IllegalArgumentException(String.format( | ||
"Invalid main --release value: %d. Use 'mixedJavaRelease' instead.", mainJavaRelease | ||
)); | ||
} | ||
project.afterEvaluate(p -> configureStandardJavaRelease(mainJavaRelease)); | ||
} | ||
|
||
private void configureStandardJavaRelease(int mainJavaRelease) { | ||
JavaCompile compileJava = helper().compileJavaTask(JavaPlugin.COMPILE_JAVA_TASK_NAME); | ||
setJavaRelease(compileJava, mainJavaRelease); | ||
} | ||
|
||
@Override | ||
public void mixedJavaRelease(int mainJavaRelease, int moduleInfoJavaRelease) { | ||
validateMixedJavaReleaseArgs(mainJavaRelease, moduleInfoJavaRelease); | ||
|
||
CompileModuleOptions moduleOptions = helper().compileJavaTask(JavaPlugin.COMPILE_JAVA_TASK_NAME) | ||
.getExtensions().getByType(CompileModuleOptions.class); | ||
moduleOptions.setCompileModuleInfoSeparately(true); | ||
|
||
project.afterEvaluate(p -> configureMixedJavaRelease(mainJavaRelease, moduleInfoJavaRelease)); | ||
} | ||
|
||
private static void validateMixedJavaReleaseArgs(int mainJavaRelease, int moduleInfoJavaRelease) { | ||
if (mainJavaRelease < 6) { | ||
throw new IllegalArgumentException("Invalid main --release value: " + mainJavaRelease); | ||
} | ||
if (mainJavaRelease > 8) { | ||
throw new IllegalArgumentException(String.format( | ||
"Invalid main --release value: %d. Use 'standardJavaRelease' instead.", mainJavaRelease | ||
)); | ||
} | ||
if (moduleInfoJavaRelease < 9) { | ||
throw new IllegalArgumentException("Invalid module-info --release value: " + moduleInfoJavaRelease); | ||
} | ||
} | ||
|
||
private void configureMixedJavaRelease(int mainJavaRelease, int moduleInfoJavaRelease) { | ||
var compileJava = helper().compileJavaTask(JavaPlugin.COMPILE_JAVA_TASK_NAME); | ||
setJavaRelease(compileJava, mainJavaRelease); | ||
|
||
var compileModuleInfoJava = helper().compileJavaTask(CompileModuleOptions.COMPILE_MODULE_INFO_TASK_NAME); | ||
setJavaRelease(compileModuleInfoJava, moduleInfoJavaRelease); | ||
} | ||
|
||
// TODO: Remove this method when Gradle supports it natively: https://github.com/gradle/gradle/issues/2510 | ||
private void setJavaRelease(JavaCompile javaCompile, int javaRelease) { | ||
String currentJavaVersion = JavaVersion.current().toString(); | ||
if (!javaCompile.getSourceCompatibility().equals(currentJavaVersion)) { | ||
throw new IllegalStateException("sourceCompatibility should not be set together with --release option"); | ||
} | ||
if (!javaCompile.getTargetCompatibility().equals(currentJavaVersion)) { | ||
throw new IllegalStateException("targetCompatibility should not be set together with --release option"); | ||
} | ||
|
||
List<String> compilerArgs = javaCompile.getOptions().getCompilerArgs(); | ||
if (compilerArgs.contains("--release")) { | ||
throw new IllegalStateException("--release option is already set in compiler args"); | ||
} | ||
|
||
compilerArgs.add("--release"); | ||
compilerArgs.add(String.valueOf(javaRelease)); | ||
} | ||
|
||
private JavaProjectHelper helper() { | ||
return new JavaProjectHelper(project); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/javamodularity/moduleplugin/extensions/ModularityExtension.java
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.javamodularity.moduleplugin.extensions; | ||
|
||
/** | ||
* A project-wide extension that provides the most common modularity-related actions. | ||
* | ||
* @see DefaultModularityExtension | ||
*/ | ||
public interface ModularityExtension { | ||
|
||
/** | ||
* Calling this method results in all Java classes being compiled to Java release 9+ (as given by the | ||
* {@code mainJavaRelease} parameter). | ||
* <p> | ||
* See details about the {@code --release} option | ||
* <a href="https://docs.oracle.com/en/java/javase/11/tools/javac.html">here</a>. | ||
* | ||
* @param mainJavaRelease value for the {@code --release} option of {@code compileJava} task (allowed range: 9+) | ||
*/ | ||
void standardJavaRelease(int mainJavaRelease); | ||
|
||
/** | ||
* Calling this method results in all Java classes being compiled to Java release 6-8 (as given by the | ||
* {@code mainJavaRelease} parameter), with the exception of {@code module-info.java} being compiled to | ||
* Java release 9. | ||
* | ||
* @param mainJavaRelease value for the {@code --release} option of {@code compileJava} task (allowed range: 6-8) | ||
*/ | ||
default void mixedJavaRelease(int mainJavaRelease) { | ||
mixedJavaRelease(mainJavaRelease, 9); | ||
} | ||
|
||
/** | ||
* Calling this method results in all Java classes being compiled to Java release 6-8 (as given by the | ||
* {@code mainJavaRelease} parameter), with the exception of {@code module-info.java} being compiled to | ||
* Java release 9+ (as given by the {@code moduleInfoJavaRelease} parameter). | ||
* <p> | ||
* See details about the {@code --release} option | ||
* <a href="https://docs.oracle.com/en/java/javase/11/tools/javac.html">here</a>. | ||
* | ||
* @param mainJavaRelease value for the {@code --release} option of {@code compileJava} task | ||
* (allowed range: 6-8) | ||
* @param moduleInfoJavaRelease value for the {@code --release} option of {@code compileModuleInfoJava} task | ||
* (allowed range: 9+) | ||
*/ | ||
void mixedJavaRelease(int mainJavaRelease, int moduleInfoJavaRelease); | ||
} |
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 |
---|---|---|
|
@@ -18,6 +18,9 @@ tasks { | |
} | ||
} | ||
} | ||
|
||
modularity { | ||
} | ||
//endregion | ||
|
||
val compileKotlin: KotlinCompile by tasks | ||
|
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 |
---|---|---|
|
@@ -16,4 +16,7 @@ test { | |
runOnClasspath = false | ||
} | ||
} | ||
|
||
modularity { | ||
} | ||
//endregion |