babashka

2025-04-28T16:43:01.685539Z

I see that for writing tasks, it's possible to provide requires separate from the task body. What if my task is using some Java classes? I can write out fully qualified class names, but is there any way to do an import? Do I just wrap an import and the task body together in a do?

borkdude 2025-04-28T17:26:28.962859Z

no wait to do imports, unless in :init

borkdude 2025-04-28T18:15:08.791139Z

so like this:

{:tasks {:init (import '[java.util.concurrent Executor])
         a (do ,,,
               Executor)}}

👍🏻 1
2025-04-28T18:24:41.622989Z

And that would apply the import to every task, right? Is there a downside to using init for that if, say, only one of five tasks actually needs those classes?

borkdude 2025-04-28T18:25:25.297059Z

it would apply to all tasks yes. the only alternative is to require a namespace on the classpath and move the code there

borkdude 2025-04-28T18:25:59.730559Z

there's not really a downside I'd say, extra startup cost of that is negligible

2025-04-28T18:30:18.316909Z

Thanks 🙂