jbang
jbang copied to clipboard
//REPOS example not working for private gitlab repo
So, I am trying to create a example on how to use a library which is there in my organization's private repo. I stumbled across jbang, thought if I can create an example project in jbang, it will be easier for other team mates to try. Here is how I am using it:
///usr/bin/env jbang "$0" "$@" ; exit $?
//REPOS <repo id>=https://<company domain>/api/v4/projects/10125/packages/maven
//DEPS com.example:demo:1.1.3
import static java.lang.System.*;
public class hello {
public static void main(String... args) {
out.println("Hello World");
}
}
If you notice, I am not using mavencentral here, just to keep things simple. But still, its not working for me:
Caused by: org.jboss.shrinkwrap.resolver.api.NoResolvedResultException: Unable to collect/resolve dependency tree for a resolution due to: Could not find artifact com.example:demo:1.1.3 in <repo id> (https://<company domain>/api/v4/projects/10125/packages/maven), caused by: Could not find artifact com.example:demo:1.1.3 in <repo id> (https://<company domain>/api/v4/projects/10125/packages/maven)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenWorkingSessionImpl.wrapException(MavenWorkingSessionImpl.java:503)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenWorkingSessionImpl.resolveDependencies(MavenWorkingSessionImpl.java:242)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenStrategyStageBaseImpl.using(MavenStrategyStageBaseImpl.java:70)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenStrategyStageBaseImpl.withTransitivity(MavenStrategyStageBaseImpl.java:52)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenStrategyStageBaseImpl.withTransitivity(MavenStrategyStageBaseImpl.java:39)
at dev.jbang.dependencies.DependencyUtil.lambda$resolveDependenciesViaAether$3(DependencyUtil.java:194)
I installed jbang using sdkman. I think I am using the latest version 0.83.0. Let me know if you need any more info.
How is this private repo Secured ? How do you provide the security settings on M2 settings ?
also, i'm not a gitlab user but is there any way I can reproduce this setup somehow ?
Yeah, so, here is an example of my settings.xml
<settings>
<servers>
<server>
<id>repo id</id>
<configuration>
<httpHeaders>
<property>
<name>Private-Token</name>
<value>Some token</value>
</property>
</httpHeaders>
</configuration>
</server>
</servers>
</settings>
Let me quickly check if gitlab lets us create a private artifact repo as an sample.
Here is an example. I think you should be able to create a similar repo on Gitlab: https://gitlab.com/prakhar.mishra1990/demo-maven-artifact
I created a repo, wrote a class, mvn deployed it in gitlab here:
https://gitlab.com/prakhar.mishra1990/demo-maven-artifact/-/packages
After removing it from ~/.m2/repository, I tried accessing it from a jbang initiated Java file:
///usr/bin/env jbang "$0" "$@" ; exit $?
//REPOS mavencentral,gitlab-maven=https://gitlab.com/api/v4/projects/31303112/packages/maven
//DEPS org.example:demo-maven-artifact:1.0
import static java.lang.System.*;
import com.example.CalculatorWithChaining;
public class hello {
public static void main(String... args) {
out.println("Hello World");
int result = new CalculatorWithChaining(0)
.plus(10)
.minus(5)
.getResult();
out.println("result=" + result);
}
}
Its giving out same issue, Could not resolve dependency org.example:demo-maven-artifact:1.0
Let me know if you need any more info around it
Ok. That should be enough for me to recreate. Thanks!
Is there anything that can be contributed? Turning up some debug logging, it seems that jbang-shrinkwrap is not sending any additional headers when resolving a jar from a private gitlab registry.
Setup server credentials in ~/.m2/settings.xml and jbang Will use that for authentication.
It will not work. I'm using jbang 0.98.0 and during dependency resolution, gitlab authentication headers defined in ~/.m2/settings.xml will not be respected, resulting in no dependencies being pulled from the gitlab registry.
Ok. That's unfortunate. Works just fine with GitHub private repos.
Anyone know what's special about gitlab?
They don't use basic auth (username/password) but instead rely on custom headers (see https://github.com/jbangdev/jbang/issues/1077#issuecomment-968248365).
I did some more digging, the issue seems to be how the dependency resolver is set up.
Under the hood, the maven subsystem expects certain system properties to be set in order to pass on those headers (aether.connector.http.headers.<server-id>)
Maven core extract those from the settings.xml when setting up a session (see https://github.com/apache/maven/blob/master/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java#L238-L263).
Similar code in the jbang-resolver does no such thing (https://github.com/jbangdev/jbang-resolver/blob/main/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenRepositorySystem.java#L75-L100).
So a fix would be to also set those settings in jbang-resolver.
This seems similar to the problems with proxies mentioned in #1010 and #447. It's related to the Shrinkwrap library not using all the information in settings.xml to set up the Session. We probably have to look at making more changes to our fork of Shrinkwrap or go ahead and see if we can remove this unsupported dependency. Wdyt @maxandersen ?
@prakharmishra there's a PR (#1456) that should fix your issue, you want to help us by trying it out and telling us if it works? You can download the specific JBang version for that PR here: https://github.com/jbangdev/jbang/suites/8332460302/artifacts/366373352
@JakobFels too :point_up:
Does not work for me, I'll comment on the PR