Skip to content

Commit 5f5128b

Browse files
committed
Add "fixJava8Classpath" option to support multirelease projects
In certain environments that build multirelease jars, the Maven "classes" directory contains class files with newer Java versions, whereas the Java 8 versions are stored elsewhere. Add a boolean "fixJava8Classpath" option that, once enabled, automatically fixes these classpath entries. It checks for the presence of a sibling directory named "classes-java8". If that's available, then the original classpath entry is replaced.
1 parent fbd82c9 commit 5f5128b

File tree

6 files changed

+47
-0
lines changed

6 files changed

+47
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ Configurable system properties:
131131
Alternative to retrolambda.classpath for avoiding the command line
132132
length limit. The file must list one file per line with UTF-8 encoding.
133133
134+
retrolambda.fixJava8Classpath
135+
Whether to replace occurrences of classpath entries ending in /classes
136+
with entries ending in /classes-java8 if such directory is available.
137+
Disabled by default. Enable by setting to "true"
138+
134139
retrolambda.includedFiles
135140
List of files to process, instead of processing all files.
136141
This is useful for a build tool to support incremental compilation.

retrolambda-api/src/main/java/net/orfjackal/retrolambda/api/RetrolambdaApi.java

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ public class RetrolambdaApi {
1717
public static final String DEFAULT_METHODS = PREFIX + "defaultMethods";
1818
public static final String BYTECODE_VERSION = PREFIX + "bytecodeVersion";
1919
public static final String JAVAC_HACKS = PREFIX + "javacHacks";
20+
public static final String FIX_JAVA8_CLASSPATH = PREFIX + "fixJava8Classpath";
2021
}

retrolambda-maven-plugin/src/main/java/net/orfjackal/retrolambda/maven/ProcessClassesMojo.java

+9
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ abstract class ProcessClassesMojo extends AbstractMojo {
107107
@Parameter(defaultValue = "false")
108108
public boolean fork;
109109

110+
/**
111+
* Whether to replace occurrences of classpath entries ending in {@code /classes}
112+
* with entries ending in {@code /classes-java8} if such directory is available.
113+
* @since 2.5.8
114+
*/
115+
@Parameter(defaultValue = "false", property = "fixJava8Classpath", required = false)
116+
public boolean fixJava8Classpath;
117+
110118
protected abstract File getInputDir();
111119

112120
protected abstract File getOutputDir();
@@ -126,6 +134,7 @@ public void execute() throws MojoExecutionException {
126134
config.setProperty(RetrolambdaApi.OUTPUT_DIR, getOutputDir().getAbsolutePath());
127135
config.setProperty(RetrolambdaApi.CLASSPATH, getClasspath());
128136
config.setProperty(RetrolambdaApi.JAVAC_HACKS, "" + javacHacks);
137+
config.setProperty(RetrolambdaApi.FIX_JAVA8_CLASSPATH, "" + fixJava8Classpath);
129138

130139
if (fork) {
131140
processClassesInForkedProcess(config);

retrolambda/src/main/java/net/orfjackal/retrolambda/Config.java

+2
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ public interface Config {
2424
boolean isJavacHacksEnabled();
2525

2626
boolean isQuiet();
27+
28+
boolean isFixJava8Classpath();
2729
}

retrolambda/src/main/java/net/orfjackal/retrolambda/Retrolambda.java

+16
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ public static void run(Config config) throws Throwable {
3838
} else {
3939
Log.INFO();
4040
}
41+
42+
if (config.isFixJava8Classpath()) {
43+
List<Path> classpathNew = new ArrayList<>();
44+
for(Path p : classpath) {
45+
if (p.toString().endsWith("/classes") && Files.isDirectory(p)) {
46+
Path p2 = p.getParent().resolve("classes-java8");
47+
if (Files.isDirectory(p2)) {
48+
p = p2;
49+
}
50+
}
51+
classpathNew.add(p);
52+
}
53+
classpath = classpathNew;
54+
}
55+
4156
Log.info("Bytecode version: " + bytecodeVersion + " (" + Bytecode.getJavaVersion(bytecodeVersion) + ")");
4257
Log.info("Default methods: " + defaultMethodsEnabled);
4358
Log.info("Input directory: " + inputDir);
@@ -47,6 +62,7 @@ public static void run(Config config) throws Throwable {
4762
Log.info("JVM version: " + System.getProperty("java.version"));
4863
Log.info("Agent enabled: " + Agent.isEnabled());
4964
Log.info("javac hacks: " + isJavacHacksEnabled);
65+
Log.info("Fix classpath: " + config.isFixJava8Classpath());
5066

5167
if (!Files.isDirectory(inputDir)) {
5268
Log.info("Nothing to do; not a directory: " + inputDir);

retrolambda/src/main/java/net/orfjackal/retrolambda/SystemPropertiesConfig.java

+14
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ public Path getOutputDir() {
132132
"length limit. The file must list one file per line with UTF-8 encoding.");
133133
}
134134

135+
136+
// fix Java 8 classpath
137+
138+
static {
139+
optionalParameterHelp(FIX_JAVA8_CLASSPATH,
140+
"Whether to replace occurrences of classpath entries ending in /classes",
141+
"with entries ending in /classes-java8 if such directory is available.",
142+
"Disabled by default. Enable by setting to \"true\"");
143+
}
144+
145+
@Override
146+
public boolean isFixJava8Classpath() {
147+
return Boolean.parseBoolean(p.getProperty(FIX_JAVA8_CLASSPATH, "false"));
148+
}
135149
@Override
136150
public List<Path> getClasspath() {
137151
String classpath = p.getProperty(CLASSPATH);

0 commit comments

Comments
 (0)