Skip to content

Commit

Permalink
Introduce itr option (#1820)
Browse files Browse the repository at this point in the history
* rebase with minor fixes

* add docs and have --itr be on top

* do not update junit libs

---------

Co-authored-by: Max Rydahl Andersen <[email protected]>
  • Loading branch information
cstamas and maxandersen authored Nov 16, 2024
1 parent 2c867b8 commit 2e3f0db
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/modules/ROOT/pages/dependencies.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ username/passwords.

By default, `jbang` uses `~/.m2` as local repository, but this can be overwritten by the environment variable `JBANG_REPO`.

=== Transitive repositories

JBang honors by default the repositories found in transitive dependencies (similar to how Maven does).

You can however disable this by using the `--ignore-transitive-repositories` (or `-itr`) option.

== Using `@Grab`

There is also support for using Groovy lang style `@Grab` syntax.
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/dev/jbang/cli/DependencyInfoMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;
import java.util.Map;

import dev.jbang.util.Util;

import picocli.CommandLine;

public class DependencyInfoMixin {
Expand All @@ -18,6 +20,13 @@ public class DependencyInfoMixin {
@CommandLine.Option(names = { "--cp", "--class-path" }, description = "Add class path entries.")
List<String> classpaths;

@CommandLine.Option(names = {
"--ignore-transitive-repositories",
"--itr" }, description = "Ignore remote repositories found in transitive dependencies")
void setIgnoreTransitiveRepositories(boolean ignoreTransitiveRepositories) {
Util.setIgnoreTransitiveRepositories(ignoreTransitiveRepositories);
}

public List<String> getDependencies() {
return dependencies;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/jbang/dependencies/ArtifactResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static class Builder {
private List<MavenRepo> repositories;
private int timeout;
private boolean offline;
private boolean ignoreTransitiveRepositories;
private boolean withUserSettings;
private Path localFolder;
private Path settingsXml;
Expand Down Expand Up @@ -98,6 +99,11 @@ public Builder offline(boolean offline) {
return this;
}

public Builder ignoreTransitiveRepositories(boolean ignoreTransitiveRepositories) {
this.ignoreTransitiveRepositories = ignoreTransitiveRepositories;
return this;
}

public Builder logging(boolean logging) {
this.loggingEnabled = logging;
return this;
Expand Down Expand Up @@ -132,6 +138,8 @@ private ArtifactResolver(Builder builder) {
ContextOverrides.Builder overridesBuilder = ContextOverrides.create()
.userProperties(userProperties)
.offline(builder.offline)
.ignoreArtifactDescriptorRepositories(
builder.ignoreTransitiveRepositories)
.withUserSettings(builder.withUserSettings)
.withUserSettingsXmlOverride(builder.settingsXml)
.withLocalRepositoryOverride(builder.localFolder)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dev/jbang/dependencies/DependencyResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public DependencyResolver addClassPaths(List<String> classPaths) {
public ModularClassPath resolve() {
ModularClassPath mcp = DependencyUtil.resolveDependencies(
new ArrayList<>(dependencies), new ArrayList<>(repositories),
Util.isOffline(), Util.isFresh(), !Util.isQuiet(), Util.downloadSources());
Util.isOffline(), Util.isIgnoreTransitiveRepositories(), Util.isFresh(), !Util.isQuiet(),
Util.downloadSources());
if (classPaths.isEmpty()) {
return mcp;
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/dev/jbang/dependencies/DependencyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ private DependencyUtil() {
}

public static ModularClassPath resolveDependencies(List<String> deps, List<MavenRepo> repos,
boolean offline, boolean updateCache, boolean loggingEnabled, boolean downloadSources) {
boolean offline, boolean ignoreTransitiveRepositories, boolean updateCache, boolean loggingEnabled,
boolean downloadSources) {

// if no dependencies were provided we stop here
if (deps.isEmpty()) {
Expand Down Expand Up @@ -102,6 +103,8 @@ public static ModularClassPath resolveDependencies(List<String> deps, List<Maven
.withUserSettings(true)
.localFolder(getJBangLocalMavenRepoOverride())
.offline(offline)
.ignoreTransitiveRepositories(
ignoreTransitiveRepositories)
.forceCacheUpdate(updateCache)
.logging(loggingEnabled)
.downloadSources(downloadSources)
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/dev/jbang/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public class Util {
private static boolean quiet;
private static boolean offline;
private static boolean fresh;
private static boolean ignoreTransitiveRepositories;
private static boolean preview;

private static Path cwd;
Expand Down Expand Up @@ -127,6 +128,10 @@ public static void setOffline(boolean offline) {
}
}

public static void setIgnoreTransitiveRepositories(boolean ignoreTransitiveRepositories) {
Util.ignoreTransitiveRepositories = ignoreTransitiveRepositories;
}

public static void setDownloadSources(boolean flag) {
downloadSources = flag;
}
Expand All @@ -142,6 +147,10 @@ public static boolean isOffline() {
return offline;
}

public static boolean isIgnoreTransitiveRepositories() {
return ignoreTransitiveRepositories;
}

public static void setFresh(boolean fresh) {
Util.fresh = fresh;
if (fresh) {
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/dev/jbang/dependencies/DependencyResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ void testResolveDependencies() {
List<String> deps = Arrays.asList("com.offbytwo:docopt:0.6.0.20150202", "log4j:log4j:1.2+");

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

// if returns 5 its because optional deps are included which they shouldn't
Expand All @@ -151,6 +152,7 @@ void testResolveDependenciesNoDuplicates() {
"org.apache.commons:commons-text:1.8");

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

// if returns with duplicates its because some dependencies are multiple times
Expand All @@ -172,6 +174,7 @@ void testResolveNativeDependencies() {
List<String> deps = Collections.singletonList("com.github.docker-java:docker-java:3.1.5");

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

assertEquals(46, classpath.getClassPaths().size());
Expand All @@ -183,7 +186,7 @@ void testResolveJavaModules() throws IOException {
List<String> deps = Arrays.asList("org.openjfx:javafx-graphics:11.0.2:mac", "com.offbytwo:docopt:0.6+");

ModularClassPath cp = new ModularClassPath(
DependencyUtil .resolveDependencies(deps, Collections.emptyList(), false, false, true, false)
DependencyUtil .resolveDependencies(deps, Collections.emptyList(), false, false, false, true, false)
.getArtifacts()) {
@Override
protected boolean supportsModules(JdkProvider.Jdk jdk) {
Expand All @@ -205,6 +208,7 @@ void testImportPOM() {
List<String> deps = Arrays.asList("com.microsoft.azure:azure-bom:1.0.0.M1@pom", "com.microsoft.azure:azure");

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

assertEquals(62, classpath.getArtifacts().size());
Expand All @@ -221,6 +225,7 @@ void testImportMultipleBoms() {
"org.slf4j:slf4j-simple:1.7.30");

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

Optional<ArtifactInfo> coord = classpath.getArtifacts()
Expand Down Expand Up @@ -253,7 +258,7 @@ void testImportMultipleBoms() {
"org.apache.camel:camel-core",
"org.apache.camel:camel-vertx",
"org.slf4j:slf4j-simple:1.7.30");
classpath = DependencyUtil.resolveDependencies(deps, Collections.emptyList(), false, false, true, false);
classpath = DependencyUtil.resolveDependencies(deps, Collections.emptyList(), false, false, false, true, false);

coord = classpath .getArtifacts()
.stream()
Expand All @@ -268,6 +273,7 @@ void testResolveTestJar() {
List<String> deps = Arrays.asList("org.infinispan:infinispan-commons:13.0.5.Final@test-jar");

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

assertThat(classpath.getArtifacts(), hasSize(7));
Expand Down
1 change: 1 addition & 0 deletions src/test/java/dev/jbang/dependencies/TestArtifactInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void testDependencyCache() {
"org.apache.commons:commons-text:1.8");

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

DependencyCache.cache("wonka", classpath.getArtifacts());
Expand Down

0 comments on commit 2e3f0db

Please sign in to comment.