depresolve
CONTENT

Overview

depresolve - command line tool which helps to manage Java dependencies in your local Maven repository.

You can think of depresolve as something similar to "apt" command for Debian packages (or "pip" command for Python packages). Example:

Supported functionality:

depresolve may be useful for Java scripting (see examples below) as it allows you to use any Java libraries from the Maven Central repository. You don't need to create Maven (or Gradle) project or define pom.xml (or build.gradle). Instead you can focus on writing Java scripts right away.

Download

You can download depresolve from here

Requirements

Java 11+

Install

Download depresolve package and extract it to some folder.

Follow instructions below based on OS you are using. Please note DEPRESOLVE_INSTALL_DIR it is full path to extracted depresolve directory.

Linux

Run following command if you use bash:

echo "export PATH=$PATH:<DEPRESOLVE_INSTALL_DIR>" >> ~/.bashrc

Or in case you use zsh:

echo "export PATH=$PATH:<DEPRESOLVE_INSTALL_DIR>" >> ~/.zshrc

Windows

Run cmd.exe and execute following command:

setx PATH "%PATH%;<DEPRESOLVE_INSTALL_DIR>"

Usage

depresolve [ -cp | -classpath ] [ --repo-home <REPO_FOLDER> ] [ --output|--output-links <OUTPUT_FOLDER> ] [--scope <test|compile> ] [--exec <COMMAND>] <ARTIFACT_NAME> [ ... [--scope <test|compile> ] <ARTIFACT_NAME> ]

By default depresolve resolves ARTIFACT_NAME including all its dependencies and downloads them into your Maven local repository.

Examples

Resolve classpath

Following command downloads specified Java library "org.apache.maven.resolver:maven-resolver-impl:1.6.2" from Maven Central into users local Maven repository (default ~/.m2/respository) and returns classpath string to it plus all its dependencies:

depresolve -cp org.apache.maven.resolver:maven-resolver-impl:1.6.2
/home/ubuntu/.m2/repository/org/apache/commons/commons-lang3/3.8.1/commons-lang3-3.8.1.jar:/home/
ubuntu/.m2/repository/org/apache/maven/resolver/maven-resolver-api/1.6.2/maven-resolver-api-1.6.2.jar:
/home/ubuntu/.m2/repository/org/apache/maven/resolver/maven-resolver-impl/1.6.2/maven-resolver-impl-1.
6.2.jar:/home/ubuntu/.m2/repository/org/apache/maven/resolver/maven-resolver-spi/1.6.2/maven-resolver-
spi-1.6.2.jar:/home/ubuntu/.m2/repository/org/apache/maven/resolver/maven-resolver-util/1.6.2/maven-re
solver-util-1.6.2.jar:/home/ubuntu/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar

Resolving classpath when run Java scripts

Starting from Java 11 you can activate source-file mode which allows to run single source files without compiling them:

java --source 11 <FILENAME> [args]

It is a very useful feature which allows you to write scripts in Java.

Let's try to write some scripts which will depend on external libraries (JavaFX and LIBGDX) and see how depresolve can help to manage these dependecies.

JavaFX

This example demonstrates how you can use depresolve to run a Java script which has a dependency on JavaFX without downloading it or creating pom.xml (or build.gradle) files.

Copy script below to the file javafx-demo.java and save it.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class MyApp extends Application {
    public void start(Stage stage) {
        Circle circ = new Circle(40, 40, 30);
        Group root = new Group(circ);
        Scene scene = new Scene(root, 400, 300);

        stage.setTitle("My JavaFX Application");
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        Application.launch(MyApp.class, args);
    }
}

This script is taken from JavaFX documentation.

Now you can run it like:

depresolve org.openjfx:javafx-controls:11 --exec "java --source 11 javafx-demo.java"

We call depresolve with option --exec which will resolve any given Java packages (in our example it is "org.openjfx:javafx-controls:11") from the Maven local repository to the classpath string. If some of the packages are missing in the Maven local repository then it will download them from the Maven Central repository to the local one. Finally resolved classpath string is placed into CLASSPATH environment variable and passed to the command "java --source 11 javafx-demo.java"

Linux

Linux supports command substitution feature which means users can inject classpath string into Java applications and achieve same result with -cp option instead of --exec:

java --class-path $(depresolve -cp org.openjfx:javafx-controls:11) --source 11 javafx-demo.java

LibGDX

Now let's create Java script for LibGDX sample code. The steps are similar except here we will need to specify a classifier in the name of the gdx-platform artifact to chose the platform where we will run the script (in our case it is desktop).

Copy script below to the file libgdx.java and save it.

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;

public class DesktopLauncher {
    public static void main (String[] arg) {
        Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
        new Lwjgl3Application(new HelloWorld(), config);
    }
}

public class HelloWorld extends ApplicationAdapter {
    ShapeRenderer shapeRenderer;

    @Override
    public void create () {
        shapeRenderer = new ShapeRenderer();
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(.25f, .25f, .25f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(0, 1, 0, 1);
        shapeRenderer.circle(200, 100, 75);
        shapeRenderer.end();
    }
	
    @Override
    public void dispose () {
        shapeRenderer.dispose();
    }
}

Now run:

depresolve com.badlogicgames.gdx:gdx-platform:jar:natives-desktop:1.12.1 com.badlogicgames.gdx:gdx:1.12.1 com.badlogicgames.gdx:gdx-backend-lwjgl3:1.12.1 --exec "java --source 11 libgdx.java"

You should see something like:



If you run Windows inside the VM you may get an error: "The driver does not appear to support OpenGL". In this case make sure video drivers are installed.

Resolving classpath when using jshell

In this example we will run jshell (Java interactive code evaluator) and call some APIs from Deep Java Library without installing or downloading it manually.

In particular we will create NDArray with 2x2 dimension.

depresolve ai.djl:api:0.10.0 ai.djl.mxnet:mxnet-engine:0.10.0 ai.djl.mxnet:mxnet-native-auto:1.7.0-b --exec jshell
|  Welcome to JShell -- Version 11.0.2
|  For an introduction type: /help intro

jshell> import ai.djl.ndarray.*;

jshell> import ai.djl.ndarray.types.*;

jshell> var a = NDManager.newBaseManager().create(new Shape(2,2));
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
a ==> ND: (2, 2) cpu() float32
[[-1.08421872e-19,  4.56 ... e-19,  4.56921390e-41],
]

jshell> System.out.println(a)
ND: (2, 2) cpu() float32
[[-1.08421872e-19,  4.56921390e-41],
 [-1.08421872e-19,  4.56921390e-41],
]

jshell> 

Downloading all dependencies to single folder

This will download following artifacts:

with all their dependencies to djl folder:

depresolve --output djl ai.djl:api:0.10.0 ai.djl.mxnet:mxnet-engine:0.10.0 ai.djl.mxnet:mxnet-native-auto:1.7.0-b ls djl
api-0.10.0.jar             jna-5.3.0.jar                  xz-1.8.jar
commons-compress-1.20.jar  mxnet-engine-0.10.0.jar        zstd-jni-1.4.4-7.jar
dec-0.1.2.jar              mxnet-native-auto-1.7.0-b.jar
gson-2.8.6.jar             slf4j-api-1.7.30.jar