Skip to content
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

fix: jbang doesn't store additional deps in JAR Class-Path anymore #708

Merged
merged 1 commit into from
Feb 5, 2021
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
9 changes: 9 additions & 0 deletions src/main/java/dev/jbang/dependencies/DependencyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jboss.shrinkwrap.resolver.api.maven.ConfigurableMavenResolverSystem;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
Expand Down Expand Up @@ -293,4 +294,12 @@ public void apply(ConfigurableMavenResolverSystem resolver) {
return new MavenRepo(repoid, reporef);
}
}

@SafeVarargs
public static List<String> joinClasspaths(List<String>... classpaths) {
return Stream .of(classpaths)
.flatMap(x -> x.stream())
.distinct()
.collect(Collectors.toList());
}
}
21 changes: 15 additions & 6 deletions src/main/java/dev/jbang/dependencies/ModularClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,34 @@
import dev.jbang.util.Util;

public class ModularClassPath {

static final String JAVAFX_PREFIX = "javafx";

private final List<ArtifactInfo> artifacts;

private List<String> classPaths;
private String classPath;
private String manifestPath;
private final List<ArtifactInfo> artifacts;
private Optional<Boolean> javafx = Optional.empty();

public ModularClassPath(List<ArtifactInfo> artifacts) {
this.artifacts = artifacts;
}

public String getClassPath() {
if (classPath == null) {
classPath = artifacts .stream()
public List<String> getClassPaths() {
if (classPaths == null) {
classPaths = artifacts .stream()
.map(it -> it.asFile().getAbsolutePath())
.map(it -> it.contains(" ") ? '"' + it + '"' : it)
.distinct()
.collect(Collectors.joining(CP_SEPARATOR));
.collect(Collectors.toList());
}

return classPaths;
}

public String getClassPath() {
if (classPath == null) {
classPath = String.join(CP_SEPARATOR, getClassPaths());
}

return classPath;
Expand Down
40 changes: 19 additions & 21 deletions src/main/java/dev/jbang/source/RunContext.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package dev.jbang.source;

import static dev.jbang.dependencies.DependencyUtil.joinClasspaths;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import dev.jbang.Settings;
import dev.jbang.catalog.Alias;
import dev.jbang.cli.BaseCommand;
import dev.jbang.cli.ExitException;
import dev.jbang.dependencies.ModularClassPath;

/**
Expand Down Expand Up @@ -43,7 +45,8 @@ public class RunContext {

private Alias alias;

private ModularClassPath classpath;
private ModularClassPath mcp;
private ModularClassPath additionalMcp;

public static RunContext empty() {
return new RunContext();
Expand Down Expand Up @@ -234,41 +237,36 @@ public List<String> collectAllDependenciesFor(Source src) {
if (getProperties() != null) {
p.putAll(getProperties());
}
return getAllDependencies(src, p);
}

private List<String> getAllDependencies(Source src, Properties props) {
return Stream .concat(getAdditionalDependencies().stream(), src.getAllDependencies(props).stream())
.collect(Collectors.toList());
return src.getAllDependencies(p);
}

/**
* Return resolved classpath lazily. resolution will only happen once, any
* consecutive calls return the same classpath.
**/
public String resolveClassPath(Source src, boolean offline) {
if (classpath == null) {
classpath = src.resolveClassPath(collectAllDependenciesFor(src), offline);
if (additionalMcp == null) {
additionalMcp = src.resolveClassPath(getAdditionalDependencies(), offline);
}
StringBuilder cp = new StringBuilder(classpath.getClassPath());
for (String addcp : getAdditionalClasspaths()) {
if (cp.length() > 0) {
cp.append(Settings.CP_SEPARATOR);
}
cp.append(addcp);
if (mcp == null) {
mcp = src.resolveClassPath(collectAllDependenciesFor(src), offline);
}
return cp.toString();
List<String> cp = joinClasspaths(additionalMcp.getClassPaths(), mcp.getClassPaths(), getAdditionalClasspaths());
return String.join(Settings.CP_SEPARATOR, cp);
}

public List<String> getAutoDetectedModuleArguments(Source src, String requestedVersion, boolean offline) {
if (classpath == null) {
if (mcp == null) {
resolveClassPath(src, offline);
}
return classpath.getAutoDectectedModuleArguments(requestedVersion);
return mcp.getAutoDectectedModuleArguments(requestedVersion);
}

public ModularClassPath getClassPath() {
return classpath;
if (mcp == null) {
throw new ExitException(BaseCommand.EXIT_INTERNAL_ERROR, "Classpath must be resolved first");
}
return mcp;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/test/java/dev/jbang/cli/TestRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ void testCreateJar(@TempDir Path rootdir) throws IOException {
RunContext ctx = RunContext.empty();
ctx.setMainClass("wonkabear");

ctx.resolveClassPath(src, true);
BaseBuildCommand.createJarFile(src, ctx, dir, out);

try (JarFile jf = new JarFile(out)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void testResolveDependencies() {
ModularClassPath classpath = dr.resolveDependencies(deps, Collections.emptyList(), false, true);

// if returns 5 its because optional deps are included which they shouldn't
assertEquals(2, classpath.getClassPath().split(Settings.CP_SEPARATOR).length);
assertEquals(2, classpath.getClassPaths().size());

}

Expand All @@ -166,7 +166,7 @@ void testResolveDependenciesNoDuplicates() {
// if returns with duplicates its because some dependencies are multiple times
// in the
// classpath (commons-text-1.8, commons-lang3-3.9)
List<String> cps = Arrays.asList(classpath.getClassPath().split(Settings.CP_SEPARATOR));
List<String> cps = classpath.getClassPaths();

HashSet<String> othercps = new HashSet<>();
othercps.addAll(cps);
Expand All @@ -188,7 +188,7 @@ void testResolveNativeDependencies() {

ModularClassPath classpath = dr.resolveDependencies(deps, Collections.emptyList(), false, true);

assertEquals(46, classpath.getClassPath().split(Settings.CP_SEPARATOR).length);
assertEquals(46, classpath.getClassPaths().size());

}

Expand Down