depresolve
CONTENT

Overview

depresolve - command line tool which helps to manage Java dependencies (from Maven repository).

Supported functionality:

depresolve may be useful for Java scripting as it allows you to use any Java libraries from the 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 the script 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_FOLDER> ] [--scope <test|compile> ] <ARTIFACT_NAME> [ ... [--scope <test|compile> ] <ARTIFACT_NAME> ]

Where:

Examples

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:

java --module-path $(depresolve -cp org.openjfx:javafx-controls:11) --add-modules ALL-MODULE-PATH --source 11 javafx-demo.java

As you see we call depresolve with option -cp which will resolve all JavaFX packages from your local repository to the classpath string. If some of the packages are missing then it will download them from Central repository to your local one.

In Windows command line you cannot use $() expression so it will not work. There are 2 ways how to overcome it:

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.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        new LwjglApplication(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();
    }
}

If you running Linux:

chmod a+x libgdx.java java -cp $(depresolve -cp com.badlogicgames.gdx:gdx-platform:jar:natives-desktop:1.9.14 com.badlogicgames.gdx:gdx:1.9.14 com.badlogicgames.gdx:gdx-backend-lwjgl:1.9.14) libgdx.java

If you running Windows:

depresolve -cp com.badlogicgames.gdx:gdx-platform:jar:natives-desktop:1.9.14 com.badlogicgames.gdx:gdx:1.9.14 com.badlogicgames.gdx:gdx-backend-lwjgl:1.9.14 set CLASSPATH=<COPY_OUTPUT_OF_DEPRESOLVE_HERE> java libgdx.java

You should see something like:



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.

Linux

jshell -c $(depresolve -cp ai.djl:api:0.10.0 ai.djl.mxnet:mxnet-engine:0.10.0 ai.djl.mxnet:mxnet-native-auto:1.7.0-b)
|  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> 

Windows

First use depresolve to get a classpath string:

C:\WINDOWS\system32>depresolve -cp ai.djl:api:0.10.0 ai.djl.mxnet:mxnet-engine:0.10.0 ai.djl.mxnet:mxnet-native-auto:1.7.0-b

Copy it to CLASSPATH variable:

C:\WINDOWS\system32>set CLASSPATH=<COPY_OUTPUT_OF_DEPRESOLVE_HERE>

And run jshell:

C:\WINDOWS\system32>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.63227305e+22,  1.2 ... 31, -5.23924592e+16],
]

jshell> System.out.println(a)
ND: (2, 2) cpu() float32
[[ 1.63227305e+22,  1.23936011e+33],
 [-1.89369072e-31, -5.23924592e+16],
]

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