mocobeta commented on a change in pull request #470: URL: https://github.com/apache/lucene/pull/470#discussion_r762592215
########## File path: gradle/java/modules.gradle ########## @@ -22,7 +22,97 @@ import java.util.stream.Collectors allprojects { plugins.withType(JavaPlugin) { - // Configure the version attribute. + // + // We declare separate configurations that explicitly provide modular + // dependencies. The "normal" java plugin configurations extend from these modular + // configurations (api extends from apiModule, etc.) so all dependencies + // end up on classpath by default for backward compatibility but we can + // manipulate and separate classpath vs. module path for javac and other tasks that need it. + // + // At the same time, we also know which dependencies + // should be treated as modules: this opens up the possibility of automatically + // validating whether gradle dependency configuration is consistent with the information in + // the module-info descriptor (don't know if it's useful), for example: + // + // apiModules - these would correspond to 'requires transitive' directive in module-info, + // implementationModules - 'requires', + // compileOnly - 'requires static'. + // + configurations { + apiModule + api.extendsFrom(apiModule) + + implementationModule + implementation.extendsFrom(implementationModule) + + // This is for internal needs, a combination of the above. + compileModulePath + compileModulePath.extendsFrom(apiModule, implementationModule) + + // All these configurations are for resolution only and have a preference to see 'classes' + // folder instead of JARs for inter-project references. + [apiModule, implementationModule, compileModulePath].each { conf -> + conf.canBeConsumed(false) + conf.canBeResolved(true) + conf.attributes { + attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements, LibraryElements.CLASSES)) + } + } + } + + // TODO, NOCOMMIT: Experiment on the morfologik subproject only, for now. + if (project.path == ":lucene:analysis:morfologik") { + // We won't be using gradle's built-in automatic module finder (because there is no way + // to put non-modular JARs on classpath). + java { + modularity.inferModulePath.set(false) + } + + // Configure a custom hook in JavaCompile which will separate modules and non-modules prior + // to the task execution. Note that this would have to be done + // for each convention task separately because their configuration is different. + tasks.named("compileJava").configure { JavaCompile task -> + def modulePath = task.project.configurations.compileModulePath + task.dependsOn modulePath + + // Add module path to compiler arguments. + task.options.compilerArgumentProviders.add(new CommandLineArgumentProvider() { + @Override + Iterable<String> asArguments() { + return ["--module-path", modulePath.files.join(File.pathSeparator)] + } + }) + + // Modify the default classpath: remove anything already placed on module path. We use + // a lazy file collection for this (maybe it's terribly inefficient, didn't check). + task.classpath = files({ + sourceSets.main.compileClasspath.filter { file -> !modulePath.contains(file) } + }) Review comment: Have a question: What is the intention here...? Compilation fails if I remove "module-info.java" from morfologik project since the classpath is modified and the compiler doesn't care about the module path. Do we completely discard/forbid class-path mode? ``` lucene $ git status On branch jms Changes not staged for commit: deleted: lucene/analysis/morfologik/src/java-module/module-info.java lucene $ ./gradlew -p lucene/analysis/morfologik/ compileJava -Duser.language=en Starting a Gradle Daemon, 2 incompatible Daemons could not be reused, use --status for details > Task :errorProneSkipped WARNING: errorprone disabled (skipped on non-nightly runs) > Task :lucene:analysis:morfologik:compileJava FAILED /mnt/hdd/repo/lucene/lucene/analysis/morfologik/src/java/org/apache/lucene/analysis/uk/UkrainianMorfologikAnalyzer.java:23: error: package morfologik.stemming is not visible import morfologik.stemming.Dictionary; ^ (package morfologik.stemming is declared in module org.carrot2.morfologik.stemming, which is not in the module graph) /mnt/hdd/repo/lucene/lucene/analysis/morfologik/src/java/org/apache/lucene/analysis/uk/UkrainianMorfologikAnalyzer.java:24: error: cannot find symbol import org.apache.lucene.analysis.Analyzer; ... FAILURE: Build failed with an exception. ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org