Fork me on GitHub
#tools-deps
<
2023-02-09
>
Sam Ritchie16:02:24

does anyone know how I can add a “provided” dependency to my generated pom.xml, using b/write-pom? I’m reading code here: https://github.com/clojure/tools.build/blob/master/src/main/clojure/clojure/tools/build/tasks/write_pom.clj#L222 and it looks like there is no way to add my own pom entries… dependencies is overwritten by the basis

seancorfield16:02:50

I'm curious about the use case -- provided has always seemed a bit odd to me: either the dependency is needed or it isn't... :thinking_face:

borkdude16:02:02

provided is usually used for local testing or optional deps but in deps.edn you can do this with aliases

Sam Ritchie16:02:25

@U04V70XH6 cljdoc needs these to do analysis if you have a dependency that you want the user to specify

Alex Miller (Clojure team)16:02:41

you are correct that there is no way to add custom deps to the generated pom.xml

Sam Ritchie16:02:43

so if I want to force the user to manually declare an SCI dependency, for example

Alex Miller (Clojure team)16:02:06

if you want to make a feature request --> https://ask.clojure.org

👍 2
Sam Ritchie16:02:09

@U064X3EF3 I’m going to add it to the basis, and then do

(xml/alias-uri 'pom "")

(alter-var-root
 #'write-pom/to-dep
 (fn [old]
   (fn [[_ {:keys [mvn/scope]} :as pair]]
     (cond-> (old pair)
       scope
       (conj [::pom/scope scope])))))

Sam Ritchie16:02:48

@U04V70XH6 for example if @U04V15CAJ wanted cljdoc to work with https://github.com/babashka/sci.configs (which I know he doesn’t!), he’d need to add provided deps

seancorfield16:02:53

That's a "bug" with cljdoc as far as I'm concerned -- and they know about it but haven't come up with a good way to address it yet.

borkdude16:02:08

but doesn't tools build merge with an existing pom.xml ? I remember there was something like this...? So you could add some stuff in there which should always left in or does it override dependencies? I think there was already a request for this on ask, I'll see if I can find it

seancorfield16:02:10

(and I've run into it with some of my libraries on cljdoc)

Sam Ritchie16:02:29

@U04V15CAJ yeah, but it only merges one level deep

Sam Ritchie16:02:41

dependencies from the basis wipe out the dependencies entry in the pom.xml template

seancorfield16:02:45

@U04V15CAJ It completely replaces <dependencies> I believe, not merge.

👍 2
Sam Ritchie16:02:51

@U04V70XH6 I personally like provided dependencies… “I won’t work without this but you need to be explicit about what you want”

borkdude16:02:46

@U017QJZ9M7W clojure.data.xml + add some deps yourself in tools.build? It's not that hard :) it comes with bb too

Sam Ritchie16:02:37

yup, doing that now!

borkdude16:02:39

btw I can't find the ask issue, so maybe there wasn't one

Alex Miller (Clojure team)17:02:35

it completely replaces some sections -depedencies, repositories, etc

Sam Ritchie17:02:04

(def basis
  (-> (b/create-basis
       {:project "deps.edn"
        :extra
        {:deps
         {'org.babashka/sci
          {:mvn/version "0.6.37"
           :mvn/scope "provided"}}}})))

Sam Ritchie17:02:15

that works, given

(ns build
  "tools.build declarations for the mafs.cljs library."
  (:require [clojure.data.xml :as xml]
            [clojure.tools.build.api :as b]
            [clojure.tools.build.tasks.write-pom :as write-pom]))

(xml/alias-uri 'pom "")

(alter-var-root
 #'write-pom/to-dep
 (fn [old]
   (fn [[_ {:keys [mvn/scope]} :as pair]]
     (cond-> (old pair)
       scope
       (conj [::pom/scope scope])))))

dominicm18:02:41

@U04V70XH6 use case is for libraries with namespaces that integrate with third party libraries that are optional?

borkdude18:02:07

@U017QJZ9M7W What I meant was rewriting the pom after tools build had written it, not actually monkey-patch tools build but since this is in your private build, I guess do what you want :)

seancorfield18:02:08

@U09LZR36F Right, but you don't need those in the POM -- you can use aliases to develop with.

dominicm18:02:00

Ah, but cljdoc was maven oriented. And any other maven oriented tools need that annotation.

seancorfield19:02:35

Right, but cljdoc is also looking at generating docs from source only (git dep) projects so I'd say its current Maven dependence is historical 🙂

Sam Ritchie19:02:43

I’ll do it the right way eventually, you’re of course right @U04V15CAJ

dominicm20:02:35

I'm a couple years out of date, I think 😅 I've got catching up to do

lread21:02:37

Cljdoc will also eventually support static analysis to glean the public API. Which should help with many awkwardnesses and failed API analyses. I think this will be the default, and for libs that generate their API at load time - they will still be able to opt for dynamic analysis.

borkdude21:02:51

(A foretaste of docs using static analysis: https://github.com/borkdude/quickdoc)

lread21:02:58

> Maven dependence is historical But will likely persist @U04V70XH6, I think, anyway.

seancorfield21:02:43

@UE21H2HHD Well, if cljdoc is able to process git deps, those won't require pom.xml and so cljdoc would be able to analyze libraries without the "Maven dependence" which was what I was leaning into there... 🙂

borkdude21:02:44

Maybe what @U09LZR36F meant is that your analyzed dependency is a mvn artifact and that you can't update your docs unless you push a new version? I don't know if that's still the case, but this was coupled to a mvn version

lread21:02:44

Gotcha @U04V70XH6, makes sense, somebody with a lib that has optional deps would be more likely to take the analyze from git repo sources route (when that becomes available).

Drew Verlee18:02:31

The trade off between building calling a function through the java command line against a jar, and calling cli -X and passing it a clojure function, is just that the later will be slower right?

Drew Verlee18:02:37

As in, for functions where the execution time isn't a factor, it's fine to use either route.

seancorfield18:02:07

The former can be used standalone, the latter requires source etc.

👍 2
dpsutton18:02:41

can you not do clj -Sdeps '{:deps {my/lib {:local/root "jar.jar"}}}' -X some/fn regardless if source is present or not?

👀 2
hiredman18:02:20

what the later does is more context dependent than the former, and what it does will determine if it is slower than the former or not

👀 2
Drew Verlee19:02:42

Is there a way to have two -main functions? Asking the question another way, If the .clj files on my deps :path already contain a -main function with a the command to generate classes (:genc-class), can I add another -main function, and specify that one in a separate build (build.clj) so I can end up with two jars each that call different main functions? Maybe using the build api function https://clojure.github.io/tools.build/clojure.tools.build.api.html#var-set-project-root.21! ? which takes, at a glance, a :main option for main class symbol? Does this question indicate i have strayed from the path and should trying something else?

seancorfield19:02:50

You can have -main in every single namespace but you can only have one default -main that is invoked via java -jar without specifying the class to invoke.

seancorfield19:02:16

java -jar ... clojure.main -m some.name will invoke

seancorfield19:02:46

(we have several JAR files that contain multiple -main functions and we choose which one to invoke via clojure.main)

Drew Verlee19:02:25

that makes sense, for some reason i hadn't considered that you could pass the namespace to the java cli.

Drew Verlee19:02:40

is that what it's called? the java cli?

seancorfield19:02:46

I suspect people just call it "the java command" but I hadn't really thought about it...

seancorfield19:02:44

java -jar ... MyPackage.MyClass has been supported for all of Java's life I think -- and clojure.main is "just" a class in the Clojure runtime.

seancorfield19:02:03

(and you can probably just do java -jar ... some.name if you have (:gen-class) in that ns -- I'm just used to bouncing via clojure.main for a ns that might not be AOT compiled)

seancorfield19:02:00

Oh, not java -jar ... MyPackage.MyClass -- java -cp ... MyPackage.MyClass sorry.

seancorfield19:02:25

Here we go:

(~/clojure/mains)-(!2016)-> cat src/multi/another.clj
(ns multi.another
  (:gen-class))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println "I'm another main!"))

Thu Feb 09 11:18:09
(~/clojure/mains)-(!2017)-> cat src/multi/mains.clj
(ns multi.mains
  (:gen-class))

(defn greet
  "Callable entry point to the application."
  [data]
  (println (str "Hello, " (or (:name data) "World") "!")))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (greet {:name (first args)}))

Thu Feb 09 11:18:12
(~/clojure/mains)-(!2018)-> java -jar target/net.clojars.multi/mains-0.1.0-SNAPSHOT.jar
Hello, World!

Thu Feb 09 11:18:22
(~/clojure/mains)-(!2019)-> java -jar target/net.clojars.multi/mains-0.1.0-SNAPSHOT.jar argument
Hello, argument!

Thu Feb 09 11:18:28
(~/clojure/mains)-(!2020)-> java -cp target/net.clojars.multi/mains-0.1.0-SNAPSHOT.jar multi.mains
Hello, World!

Thu Feb 09 11:18:48
(~/clojure/mains)-(!2021)-> java -cp target/net.clojars.multi/mains-0.1.0-SNAPSHOT.jar multi.mains argument
Hello, argument!

Thu Feb 09 11:18:52
(~/clojure/mains)-(!2022)-> java -cp target/net.clojars.multi/mains-0.1.0-SNAPSHOT.jar multi.another
I'm another main!

Thu Feb 09 11:19:02
(~/clojure/mains)-(!2023)->

👍 2
lread21:02:22

Ah thanks @U04V70XH6 and @U0NCTKEV8 the -cp variant had not occurred to me!