Skip to content

Commit

Permalink
[moditect#53] Add option to specify JVM version for add-module-info. F…
Browse files Browse the repository at this point in the history
…ixes moditect#53
  • Loading branch information
beikov committed Apr 24, 2018
1 parent 8e18788 commit bb21834
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 26 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ the _add-module-info_ goal as follows:
<goal>add-module-info</goal>
</goals>
<configuration>
<jvmVersion>9</jvmVersion>
<module>
<moduleInfo>
<name>com.example</name>
Expand All @@ -203,6 +204,9 @@ the _add-module-info_ goal as follows:
...
```

The optional `jvmVersion` element allows to define for which JVM version the module descriptor should be created.
When defined, the module descriptor will be put into `META-INF/versions/${jvmVersion}`, otherwise it will be put into the root of the final JAR.

The following configuration options exist for the `<module>` configuration element:

* `moduleInfoSource`: Inline representation of a module-info.java descriptor
Expand Down
9 changes: 8 additions & 1 deletion core/src/main/java/org/moditect/Moditect.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void main(String[] args) throws Exception {
CliArgs cliArgs = new CliArgs();
new JCommander( cliArgs, args );

new AddModuleInfo( null, null, null, null, cliArgs.outputDirecory, cliArgs.overwriteExistingFiles ).run();
new AddModuleInfo( null, null, null, null, cliArgs.outputDirecory, cliArgs.jvmVersion, cliArgs.overwriteExistingFiles ).run();
}

@Parameters(separators = "=")
Expand All @@ -53,6 +53,13 @@ private static class CliArgs {
)
private Path outputDirecory;

@Parameter(
names = "--jvm-version",
required = false,
description = "The JVM version for which to add the module-info.java descriptor"
)
private Integer jvmVersion;

@Parameter(
names = "--overwrite-existing-files",
required = false,
Expand Down
33 changes: 30 additions & 3 deletions core/src/main/java/org/moditect/commands/AddModuleInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.moditect.commands;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
Expand All @@ -24,6 +25,8 @@
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.moditect.internal.compiler.ModuleInfoCompiler;

Expand All @@ -41,14 +44,16 @@ public class AddModuleInfo {
private final String version;
private final Path inputJar;
private final Path outputDirectory;
private final Integer jvmVersion;
private final boolean overwriteExistingFiles;

public AddModuleInfo(String moduleInfoSource, String mainClass, String version, Path inputJar, Path outputDirectory, boolean overwriteExistingFiles) {
public AddModuleInfo(String moduleInfoSource, String mainClass, String version, Path inputJar, Path outputDirectory, Integer jvmVersion, boolean overwriteExistingFiles) {
this.moduleInfoSource = moduleInfoSource;
this.mainClass = mainClass;
this.version = version;
this.inputJar = inputJar;
this.outputDirectory = outputDirectory;
this.jvmVersion = jvmVersion;
this.overwriteExistingFiles = overwriteExistingFiles;
}

Expand Down Expand Up @@ -83,8 +88,30 @@ public void run() {
URI uri = URI.create( "jar:" + outputJar.toUri().toString() );

try (FileSystem zipfs = FileSystems.newFileSystem( uri, env ) ) {
Files.write( zipfs.getPath( "module-info.class" ), clazz );
}
if (jvmVersion == null) {
Files.write( zipfs.getPath( "module-info.class" ), clazz );
}
else {
Path path = zipfs.getPath( "META-INF/versions", jvmVersion.toString(), "module-info.class" );
Files.createDirectories( path.getParent() );
Files.write( path, clazz );

Path manifestPath = zipfs.getPath( "META-INF/MANIFEST.MF" );
Manifest manifest;
if ( Files.exists( manifestPath ) ) {
manifest = new Manifest( Files.newInputStream( manifestPath ) );
}
else {
manifest = new Manifest();
manifest.getMainAttributes().put( Attributes.Name.MANIFEST_VERSION, "1.0" );
}

manifest.getMainAttributes().put( Attributes.Name.MULTI_RELEASE, "true" );
try (OutputStream manifestOs = Files.newOutputStream( manifestPath )) {
manifest.write( manifestOs );
}
}
}
catch(IOException e) {
throw new RuntimeException( "Couldn't add module-info.class to JAR", e );
}
Expand Down
89 changes: 67 additions & 22 deletions core/src/test/java/org/moditect/test/AddModuleInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
*/
package org.moditect.test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ProcessBuilder.Redirect;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Optional;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -49,15 +54,71 @@ public class AddModuleInfoTest {

@Before
public void prepareDirectories() throws Exception {
Files.deleteIfExists( GENERATED_TEST_RESOURCES );
Files.createDirectory( GENERATED_TEST_RESOURCES );
truncateFolder( GENERATED_TEST_RESOURCES );
truncateFolder( GENERATED_TEST_MODULES );
}

Files.deleteIfExists( GENERATED_TEST_MODULES );
Files.createDirectory( GENERATED_TEST_MODULES );
private void truncateFolder(Path folder) throws Exception {
if ( Files.exists( folder ) ) {
Files.walkFileTree(folder, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
Files.deleteIfExists(path);
return super.visitFile(path, attrs);
}
});
Files.deleteIfExists( folder );
}
Files.createDirectory( folder );
}

@Test
public void foo() throws Exception {
public void addJvmVersionModuleInfoAndRunModular() throws Exception {
prepareTestJar();

String javaHome = System.getProperty("java.home");
String javaBin = javaHome +
File.separator + "bin" +
File.separator + "java";

ProcessBuilder builder = new ProcessBuilder(
javaBin, "--module-path", GENERATED_TEST_RESOURCES + File.separator + "example.jar", "--module", "com.example" )
.redirectOutput( Redirect.INHERIT );

Process process = builder.start();
process.waitFor();

if ( process.exitValue() == 0 ) {
throw new AssertionError();
}

new AddModuleInfo(
"module com.example {}",
"com.example.HelloWorld",
"1.42.3",
Paths.get( "target", "generated-test-resources", "example.jar" ),
Paths.get( "target", "generated-test-modules" ),
9,
false
)
.run();

builder = new ProcessBuilder(
javaBin, "--module-path", GENERATED_TEST_MODULES + File.separator + "example.jar", "--module", "com.example" );

process = builder.start();
process.waitFor();

if ( process.exitValue() != 0 ) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
process.getInputStream().transferTo( baos );
process.getErrorStream().transferTo( baos );
throw new AssertionError( baos.toString() );
}
}

@Test
public void addModuleInfoAndRunModular() throws Exception {
prepareTestJar();

String javaHome = System.getProperty("java.home");
Expand All @@ -82,6 +143,7 @@ public void foo() throws Exception {
"1.42.3",
Paths.get( "target", "generated-test-resources", "example.jar" ),
Paths.get( "target", "generated-test-modules" ),
null,
false
)
.run();
Expand Down Expand Up @@ -117,23 +179,6 @@ private void prepareTestJar() throws Exception {

Path exampleJar = GENERATED_TEST_RESOURCES.resolve( "example.jar" );


// Map<String, String> env = new HashMap<>();
// env.put( "create", "true" );
//
// URI uri = URI.create( "jar:" + exampleJar.toUri().toString() );
//
// try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
// Files.write( zipfs.getPath( "com/example/HelloWorld.class" ), ByteStreams.toByteArray( classFile.get().openInputStream() ) );
// }
// catch(IOException e) {
// throw new RuntimeException( "Couldn't add module-info.class to JAR", e );
// }





Manifest manifest = new Manifest();
manifest.getMainAttributes().put( Attributes.Name.MANIFEST_VERSION, "1.0" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public class AddModuleInfoMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.version}", readonly = true, required = true)
private String version;

@Parameter(readonly = true)
private Integer jvmVersion;

@Parameter(readonly = true, defaultValue = "${project.build.directory}/moditect")
private File workingDirectory;

Expand Down Expand Up @@ -126,6 +129,7 @@ project, repoSystem, repoSession, remoteRepos, artifactResolutionHelper, jdepsEx
getVersion( moduleConfiguration ),
inputFile,
outputPath,
jvmVersion,
overwriteExistingFiles
);

Expand All @@ -152,6 +156,7 @@ project, repoSystem, repoSession, remoteRepos, artifactResolutionHelper, jdepsEx
version,
inputJar,
outputPath,
jvmVersion,
overwriteExistingFiles
);
addModuleInfo.run();
Expand Down

0 comments on commit bb21834

Please sign in to comment.