Skip to content

Commit

Permalink
Read module-info.class anywhere in jar file (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
airsquared committed Sep 24, 2023
1 parent 98a5ad1 commit 2c0289b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.gradle.api.Project
import org.gradle.api.file.CopySpec
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.process.internal.ExecException

import java.lang.module.ModuleFinder
import java.lang.module.ModuleReference
Expand Down Expand Up @@ -136,16 +137,20 @@ class CreateMergedModuleTaskImpl extends BaseTaskImpl<CreateMergedModuleTaskData
into(targetDir)
}

project.exec {
commandLine = [
"$td.javaHome/bin/javac",
*versionOpts,
'-p',
"$td.mergedJarsDir$SEP$td.jlinkJarsDirPath",
'-d',
targetDir.path,
"$moduleInfoJavaDir/module-info.java"
]
try {
project.exec {
commandLine = [
"$td.javaHome/bin/javac",
*versionOpts,
'-p',
"$td.mergedJarsDir$SEP$td.jlinkJarsDirPath",
'-d',
targetDir.path,
"$moduleInfoJavaDir/module-info.java"
]
}
} catch (ExecException e) {
throw new GradleException('Unable to compile merged module-info. Tip: If there is a module not found error, try using `addExtraDependencies` in the `jlink {...}` block for the missing module.', e)
}
}

Expand Down
17 changes: 7 additions & 10 deletions src/main/groovy/org/beryx/jlink/util/SourceCodeConstants.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@ class SourceCodeConstants {
static final String GET_MODULE_DESCRIPTOR = '''
private static ModuleDescriptor getModuleDescriptor(File f) {
try {
if(!f.isFile()) throw new IllegalArgumentException(f + " is not a file");
if(f.getName().equals("module-info.class")) {
if (!f.isFile()) throw new IllegalArgumentException(f + " is not a file");
if (f.getName().equals("module-info.class")) {
return ModuleDescriptor.read(new FileInputStream(f));
}
if(!f.getName().endsWith(".jar") && !f.getName().endsWith(".jmod")) throw new IllegalArgumentException("Unsupported file type: " + f);
String prefix = f.getName().endsWith(".jmod") ? "classes/" : "";
ZipFile zipFile = new ZipFile(f);
for (Enumeration<? extends ZipEntry> entries = zipFile.entries(); entries.hasMoreElements();) {
ZipEntry entry = entries.nextElement();
if(entry.getName().equals(prefix + "module-info.class")) {
InputStream entryStream = zipFile.getInputStream(entry);
return ModuleDescriptor.read(entryStream);
}
var x = zipFile.stream().filter(entry -> entry.getName().endsWith("module-info.class")).findFirst();
if (x.isPresent()) {
return ModuleDescriptor.read(zipFile.getInputStream(x.get()));
} else {
return null;
}
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class SuggestMergedModuleInfoSpec extends Specification {
"requires 'java.xml';",
"requires 'java.desktop';",
"requires 'java.datatransfer';",
"requires 'org.apache.logging.log4j';",
"provides 'javax.annotation.processing.Processor' with 'org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor';",
"provides 'javax.imageio.spi.ImageWriterSpi' with 'com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriterSpi', 'com.twelvemonkeys.imageio.plugins.tiff.TIFFImageWriterSpi';",
"provides 'javax.imageio.spi.ImageReaderSpi' with 'com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReaderSpi', 'com.twelvemonkeys.imageio.plugins.tiff.BigTIFFImageReaderSpi', 'com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReaderSpi';",
Expand All @@ -59,6 +60,7 @@ class SuggestMergedModuleInfoSpec extends Specification {
'requires("java.sql");',
'requires("java.xml");',
'requires("java.desktop");',
'requires("org.apache.logging.log4j");',
'provides("javax.annotation.processing.Processor").with("org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor");',
'provides("javax.imageio.spi.ImageReaderSpi").with("com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReaderSpi", "com.twelvemonkeys.imageio.plugins.tiff.BigTIFFImageReaderSpi", "com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReaderSpi");',
'provides("javax.imageio.spi.ImageWriterSpi").with("com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriterSpi", "com.twelvemonkeys.imageio.plugins.tiff.TIFFImageWriterSpi");',
Expand All @@ -80,6 +82,7 @@ class SuggestMergedModuleInfoSpec extends Specification {
"requires java.xml;",
"requires java.datatransfer;",
"requires java.management;",
"requires org.apache.logging.log4j;",
"provides javax.annotation.processing.Processor with org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor;",
"provides javax.imageio.spi.ImageReaderSpi with com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReaderSpi, com.twelvemonkeys.imageio.plugins.tiff.BigTIFFImageReaderSpi, com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReaderSpi;",
"provides javax.imageio.spi.ImageWriterSpi with com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriterSpi, com.twelvemonkeys.imageio.plugins.tiff.TIFFImageWriterSpi;",
Expand Down

0 comments on commit 2c0289b

Please sign in to comment.