Fork me on GitHub

how to run a random .clj file's -main that has no 3rd party dependencies or a deps.edn file? I tried clojure -M some_folder/the_file.clj, it loads the file but it doesn't run its -main


Edit: sorry I read the question incorrectly. If you do add a deps.edn, then you can do this.. The -M flag specifies that the clojure command should call Clojure main in the project (as opposed to the -X flag that calls a qualified function) -m is required to state which namespace to run -main, the namespace that includes (defn -main ,,,) So from your example it would be clojure -M -m some-folder.the-file


looks like in that case I'd have to add some-folder to the classpath. How to do it in an inline fashion?


I was assuming that some_folder is under a src folder and you have a {:path ["src"]} in the deps.edn file Edit: can the -i flag be used for this, it loads a resource?


I believe you can use . Instead of src if there is no directory structure, but t that still requires a deps.edn file.


@U45T93RA6 this is a similar problem in babashka world. we invented this pattern for it: inspired by the Python way of doing things

👍 3
🙂 3

So you can check using that condition if you are directly invoking this file or if you are referring to that file using the classpath


But for clj this doesn't work. Some people also solve this using an env var in babashka: REPL=true -> don't invoke main at the bottom


clojure -M -i ./some-folder/the_file.clj -m the-file worked. Bit of an intrincate construction though :) Thank you both!


Thanks for sharing the solution that worked for you.


What's the point of having a -main btw if you are invoking this as a standalone file?


we are all a curious bunch :) This was a experimental CI-oriented script belonging to a big repo. Didn't seem to deserve its own deps.edn file. Indeed I could have unwrapped the main but there's some remote possibility of the top-level side-effect being executed by tooling (e.g. Eastwood, refactor-nrepl or t.n (refresh))


is there any way of using tools.deps code to retrieve the available list of versions for an artifact? This normally comes from the maven-metadata.xml file


I build something like this once back in 2017. It allow you to fetch the version from maven @U4VDXB2TU

Alex Miller (Clojure team)14:02:15

nothing in the tools.deps api, no

Alex Miller (Clojure team)14:02:43

you could use the lower parts of the tools.deps api as an access point into maven apis to answer that question though

Alex Miller (Clojure team)15:02:37

you basically want to use the Maven RepositorySystem API to make VersionRequests or VersionRangeRequests etc


@alexmiller ok thanks, that's the route I was heading, just wanted to make sure I wasn't reinventing the wheel