Skip to content

Commit

Permalink
Run jdeps to list dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolai Parlog authored and gunnarmorling committed May 25, 2021
1 parent c206bde commit 3de7ce0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
56 changes: 52 additions & 4 deletions core/src/main/java/org/moditect/commands/GenerateModuleList.java
Expand Up @@ -16,22 +16,70 @@
package org.moditect.commands;

import org.moditect.model.Version;
import org.moditect.spi.log.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.spi.ToolProvider;
import java.util.stream.Collectors;

public class GenerateModuleList {

private final Version jvmVersion;
private final Path projectJar;
private final Set<Path> dependencies;
private final Version jvmVersion;
private final Log log;

public GenerateModuleList(Version jvmVersion, Set<Path> dependencies) {
this.jvmVersion = jvmVersion;
private final ToolProvider jdeps;

public GenerateModuleList(Path projectJar, Set<Path> dependencies, Version jvmVersion, Log log) {
this.projectJar = projectJar;
this.dependencies = dependencies;
this.jvmVersion = jvmVersion;
this.log = log;

this.jdeps = ToolProvider
.findFirst( "jdeps" )
.orElseThrow(() -> new RuntimeException("jdeps tool not found"));
}

public void run() {
System.out.println("Creating module list for " + target + " and " + dependencies);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
PrintStream out = new PrintStream(outStream);
jdeps.run(out , System.err, "--version");
out.close();
int jdepsVersion = Runtime.Version
.parse(outStream.toString().strip())
.feature();
if (jdepsVersion < 12) {
log.error("The jdeps option this plugin uses to list JDK modules only works flawlessly on JDK 12+, so please use that to run this goal.");
return;
}

List<String> command = new ArrayList<>();
command.add("--print-module-deps");
command.add("--ignore-missing-deps");
command.add("--multi-release");
command.add(String.valueOf(jvmVersion.feature()));
command.add("--class-path");
String classPath = dependencies.stream()
.map(Path::toAbsolutePath)
.map(Path::toString)
.collect(Collectors.joining(File.pathSeparator));
command.add(classPath);
command.add(projectJar.toAbsolutePath().toString());

log.debug( "Running jdeps " + String.join( " ", command ) );

int result = jdeps.run( System.out, System.err, command.toArray( new String[0] ) );
if (result != 0) {
throw new IllegalStateException("Invocation of jdeps failed: jdeps " + String.join( " ", command ) );
}
}

}
Expand Up @@ -17,16 +17,18 @@

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.moditect.commands.GenerateModuleList;
import org.moditect.mavenplugin.util.DependencyHelper;
import org.moditect.mavenplugin.util.MojoLog;
import org.moditect.model.Version;

import java.io.File;

@Mojo(name = "list-application-image-modules",
defaultPhase = LifecyclePhase.PACKAGE,
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME
Expand All @@ -42,9 +44,11 @@ public class GenerateModuleListMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException {
GenerateModuleList generateModuleList = new GenerateModuleList(
new File(project.getBuild().getOutputDirectory()).toPath(),
// project.getArtifact().getFile().toPath(),
DependencyHelper.getDirectAndTransitiveDependencies(project),
determineJvmVersion(),
DependencyHelper.getDirectAndTransitiveDependencies(project)
);
new MojoLog(getLog()));
try {
generateModuleList.run();
} catch (RuntimeException ex) {
Expand Down

0 comments on commit 3de7ce0

Please sign in to comment.