jbangdev / jbang

Unleash the power of Java - JBang Lets Students, Educators and Professional Developers create, edit and run self-contained source-only Java programs with unprecedented ease.
https://jbang.dev
MIT License
1.43k stars 159 forks source link

Support for maven style version ranges #1267

Open jmini opened 2 years ago

jmini commented 2 years ago

Is your feature request related to a problem? Please describe. I would like to use a version range like in maven. Example [1.0.0, 1.1.0)

Describe the solution you'd like Currently only 1.+ is supported. But internally it is turned to a maven range. As seen in the [jbang] Resolving dependencies... lines

Additional context Example:

///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS ch.qos.reload4j:reload4j:[1.2.18,1.2.19)

import static java.lang.System.out;

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

import java.util.Arrays;

class logdemo {

    static final Logger logger = Logger.getLogger(logdemo.class);

    public static void main(String[] args) {
        BasicConfigurator.configure();
        logger.info("Welcome to jbang");

        Arrays.asList(args).forEach(arg -> logger.warn("arg: " + arg));
        logger.info("Hello from Java!");
    }
}

Should resolve to 1.2.18.5.

Currently failing with this error:

[jbang] Resolving dependencies...
[jbang] [ERROR] [ERROR] Invalid dependency locator: '1.2.19)'.  Expected format is groupId:artifactId:version[:classifier]
jmini commented 2 years ago

Probably it is more a bug than a feature.

This is problematic:

https://github.com/jbangdev/jbang/blob/ef362429c1c9a60aec8200b3f3184cbb74160c98/src/main/java/dev/jbang/source/ScriptSource.java#L158-L160

And the corresponding test: https://github.com/jbangdev/jbang/blob/ef362429c1c9a60aec8200b3f3184cbb74160c98/src/test/java/dev/jbang/source/TestScript.java#L265-L273

The , inside a range [1.0.0, 1.1.0) should be ignored when it comes to split.

jmini commented 2 years ago

Workaround:

///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.apache.groovy:groovy:4.0.0

import static java.lang.System.out;

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

import java.util.Arrays;

import groovy.lang.Grab;
import groovy.lang.Grapes;
import groovy.lang.GrabResolver;

@GrabResolver("mavenCentral")
@Grapes({
    @Grab(group="ch.qos.reload4j", module="reload4j", version="[1.2.18,1.2.19)")
})
class logdemo {

    static final Logger logger = Logger.getLogger(logdemo.class);

    public static void main(String[] args) {
        BasicConfigurator.configure();
        logger.info("Welcome to jbang");

        Arrays.asList(args).forEach(arg -> logger.warn("arg: " + arg));
        logger.info("Hello from Java!");
    }
}

Is correctly resolved.

Check with jbang info tools logdemo and check the resolvedDependencies. It contains:

jmini commented 2 years ago

As reaction @maxandersen https://github.com/jbangdev/jbang/pull/1271#issuecomment-1110845388 and @quintesse https://github.com/jbangdev/jbang/pull/1271#issuecomment-1110848139

Currently @Grab is the only way I found to be able to define version range.

If , is a valid separator for

//DEPS blah, blue

Then parsing the line to decide if the , is a separator or if this is part of a range (instead of blah we can have ch.qos.reload4j:reload4j:[1.2.18,1.2.19) and in this case , is no longer a separator) starts to be really difficult.