example · java · daukle/java

java-pinned-classpath

A Java project that compiles against a third-party library without a resolver, a repository or a lock file. Every classpath entry is a url and the sha256 of the bytes that url must serve, written out in the manifest by hand, and daukle refuses an entry that has no digest.

$ daukle java:run
hello from example.Main

What to look at

There is no unpinned form. A [[toolchains.java.classpath]] entry carries a url and a sha256, and that is the whole vocabulary. An entry without a digest is refused by name rather than fetched and trusted, which is the same rule every acquisition in daukle obeys: nothing arrives unverified.

as is a label, not a coordinate. It is what diagnostics call the jar, so a message names slf4j-api 1.7.36 rather than a path into a cache directory. Nothing resolves it and nothing parses it.

The jar is cached by digest, so a second project pinning the same url and digest downloads nothing, and two projects pinning the same url with *different* digests are two different jars and are kept apart.

This is what daukle/maven exists to stop you doing. Writing a url and a digest per jar is exact and it does not scale: a real library has a transitive closure, and eight declared coordinates became twenty pinned blocks the first time that was measured here. daukle/maven turns a coordinate into exactly these blocks so that you do not write them. This example is the layer underneath it, and it is worth seeing once.

What this example cannot show

Resolution. One jar is pinned here and nothing computes it. A real dependency brings its own dependencies, and working out which versions those are is the whole of what a resolver does. That is daukle/maven, which writes blocks of exactly this shape into a generated file the manifest includes.

A version conflict. With one jar there is nothing to conflict. Gradle settles three of them in the library this toolchain was measured against, and a hand-written classpath cannot notice that two entries are the same artifact at different versions.

Tests. This example compiles and runs a main. daukle/java has java:test and the example next door, java-hello-jar, is the one that packages a jar.

A multi-release jar. An exploded jar is not a jar, and a dependency with versioned classes loses them with no diagnostic, which is why the classpath keeps archives rather than directories. Nothing here has one.

Every file in it

daukle.toml

schema = 1
project = "daukle/example-pinned-classpath"
version = "1.0.0"

[modules]

[resolvers.github]
url = "https://raw.githubusercontent.com/daukle/daukle/1f515141e06d16eace718a45944a39933611fa39/plugins/github-releases.lua"
sha256 = "3789865c266eb53fafcf7300d77a24461170c615195d92d0ddf88078c556e23e"

[plugins]
java = { resolver = "github", coordinate = "daukle/java@^1.0.0" }

[toolchains.java]
version = "17"
main = "example.Main"

# A dependency, by hand. Every classpath entry is a url and the sha256 of the
# bytes that url must serve: there is no unpinned form. Writing these out is
# exactly what daukle/maven exists to stop you doing, and page 4 says so.
[[toolchains.java.classpath]]
url = "https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar"
sha256 = "d3ef575e3e4979678dc01bf1dcce51021493b4d11fb7f1be8ad982877c16a1c0"
as = "slf4j-api 1.7.36"

src/main/java/example/Main.java

package example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Uses a dependency, so the classpath below is load bearing rather than decorative. */
public final class Main {
    private static final Logger LOG = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        LOG.info("compiled and run by daukle");
        System.out.println("hello from " + Main.class.getName());
    }
}

Not shown here: needs-tools.