Skip to content

Commit

Permalink
fix: Using correct backingResoure for JarSources (quarkusio#738)
Browse files Browse the repository at this point in the history
A JarSource that is created from a ScriptSource should have the
same backingResource as the original ScriptSource.

Fixes quarkusio#734
  • Loading branch information
quintesse authored Feb 18, 2021
1 parent 515a61e commit 09a85e0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
13 changes: 9 additions & 4 deletions src/main/java/dev/jbang/source/JarSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@
*/
public class JarSource implements Source {
private final ResourceRef resourceRef;
private final File jarFile;

private String classPath;
private String mainClass;
private List<String> javaRuntimeOptions;
private int buildJdk;

private JarSource(ResourceRef resourceRef) {
private JarSource(ResourceRef resourceRef, File jar) {
this.resourceRef = resourceRef;
this.jarFile = jar;
this.javaRuntimeOptions = Collections.emptyList();
File jar = getResourceRef().getFile();
if (jar.exists()) {
try (JarFile jf = new JarFile(jar)) {
Attributes attrs = jf.getManifest().getMainAttributes();
Expand Down Expand Up @@ -71,7 +72,7 @@ public ResourceRef getResourceRef() {

@Override
public File getJarFile() {
return getResourceRef().getFile();
return jarFile;
}

@Override
Expand Down Expand Up @@ -125,6 +126,10 @@ public List<String> getRuntimeOptions() {
}

public static JarSource prepareJar(ResourceRef resourceRef) {
return new JarSource(resourceRef);
return new JarSource(resourceRef, resourceRef.getFile());
}

public static JarSource prepareJar(ResourceRef resourceRef, File jarFile) {
return new JarSource(resourceRef, jarFile);
}
}
9 changes: 2 additions & 7 deletions src/main/java/dev/jbang/source/RunContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -274,14 +273,10 @@ public ModularClassPath getClassPath() {
* If the given source is a JarSource its metadata will be copied to this
* RunContext and the JarSource will be returned. In any other case the given
* source will be returned;
*
* @return
*/
public Source importJarMetadataFor(Source src) {
File jarFile = src.getJarFile();
if (jarFile.exists()) {
JarSource jar = JarSource.prepareJar(
ResourceRef.forNamedFile(src.getResourceRef().getOriginalResource(), jarFile));
JarSource jar = src.asJarSource();
if (jar != null && jar.getJarFile().exists()) {
setMainClass(jar.getMainClass());
setPersistentJvmArgs(jar.getRuntimeOptions());
setBuildJdk(JavaUtil.javaVersion(jar.getJavaVersion()));
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/dev/jbang/source/ScriptSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,7 @@ public JarSource asJarSource() {
JarSource result = null;
File jarFile = getJarFile();
if (jarFile != null && jarFile.exists()) {
JarSource jarSrc = JarSource.prepareJar(
ResourceRef.forNamedFile(getResourceRef().getOriginalResource(), jarFile));
JarSource jarSrc = JarSource.prepareJar(getResourceRef(), jarFile);
if (jarSrc.resolveClassPath(Collections.emptyList()).isValid()) {
result = jarSrc;
}
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/dev/jbang/cli/TestInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ void testInfoToolsBuilt() {
assertThat(info.applicationJar, allOf(
containsString("quote.java."),
endsWith(".jar")));
assertThat(info.backingResource, allOf(
containsString("quote.java."),
endsWith(".jar")));
assertThat(info.backingResource, equalTo(Paths.get("itests/quote.java").toString()));
assertThat(Integer.parseInt(info.javaVersion), greaterThan(0));
assertThat(info.mainClass, equalTo("quote"));
assertThat(info.resolvedDependencies, Matchers.<Collection<String>>allOf(
Expand Down

0 comments on commit 09a85e0

Please sign in to comment.