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.
@vemv this is a similar problem in babashka world. we invented this pattern for it: https://book.babashka.org/#main_file inspired by the Python way of doing things
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!
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 https://github.com/agilecreativity/pom-versions @mbjarland
nothing in the tools.deps api, no
you could use the lower parts of the tools.deps api as an access point into maven apis to answer that question though
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
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)
)