Skip to content

Add support for modularity #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ See this upstream Gradle issue about potentially providing a solution to this pr
Gradle core: https://github.com/gradle/gradle/issues/1989. Hopefully an upstream solution to
this will eliminate the need for this plugin.

== Notes on Modularity

This plugin tries to detect if the current project is modularized.
If it is, the `module-path` is adapted instead of the class path.
Since the classpath variable of gradle is cleared after the patch, plugins relying on it stop working.
As a consequence, this plugin cannot be used together with https://github.com/java9-modularity/gradle-modules-plugin[org.javamodularity.moduleplugin].

== Version Compatibility

* Gradle 8.8+ (may work on earlier versions, but untested)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ abstract class AbstractClasspathToFileActionListener: TaskActionListener {
fun classpathToFile(
task: Task,
jvmArgumentProviders: MutableList<CommandLineArgumentProvider>,
classpath: FileCollection
classpath: FileCollection,
isModule: Boolean
) {

cpArgFile = File.createTempFile("classpath-${task.project.name.replace(" ", "_")}", null)
Expand All @@ -39,7 +40,12 @@ abstract class AbstractClasspathToFileActionListener: TaskActionListener {
// TODO this doesn't currently handle wildcards, we should expand wildcards before writing to the arg file
// see https://docs.oracle.com/javase/9/tools/java.htm#JSWOR-GUID-4856361B-8BFD-4964-AE84-121F5F6CF111
OutputStreamWriter(FileOutputStream(cpArgFile), Charsets.UTF_8).use {
it.write("-cp \"\\")
if (isModule) {
it.write("--module-path")
} else {
it.write("-cp")
}
it.write(" \"\\")
it.write(LINE_SEP)
classpath.files.forEachIndexed { i, file ->
if(i > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JavaExecSpecActionListener(private val extension: ClasspathToFilePluginExt
return
}

classpathToFile(task, task.jvmArgumentProviders, task.classpath)
classpathToFile(task, task.jvmArgumentProviders, task.classpath, task.mainModule.isPresent)
task.classpath = EmptyFileCollection
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestActionListener(private val extension: ClasspathToFilePluginExtension):
return
}

classpathToFile(task, task.jvmArgumentProviders, task.classpath)
classpathToFile(task, task.jvmArgumentProviders, task.classpath, false)
task.classpath = EmptyFileCollection
}
}