Fork me on GitHub
#tools-build
<
2021-09-28
>
AustinP00:09:09

Correct, all the paths in my deps.edn have ../.. ahead of them, pointing up the filesystem to bases and components

seancorfield00:09:02

@apaine13 In a Polylith project, you should just be able to cd projects/whatever && clojure -T:build uber with just a bare bones build.clj script and it should "just work". Your project's deps.edn file should have all the deps declared that it needs and you just create a basis and run uber with that and declaring the main class -- there's nothing to copy, generally, with Polylith since projects folders have no src or resources in a lot of cases.

seancorfield00:09:46

It might be better to take this to the #polylith channel -- we use tools.build with Polylith at work and it works just fine.

seancorfield00:09:49

I'm going to post a shell session in a thread of creating a Polylith workspace (using clj-new) and building the uberjar with tools.build so you can see the build.clj and deps.edn files involved...

seancorfield00:09:42

@apaine13

(! 812)-> clojure -Tclj-new polylith :name austin/p
Generating a project called p based on the 'polylith' template.
Initialized the project for use with 'git'.

(! 813)-> cat p/projects/p/build.clj 
(ns build
  (:refer-clojure :exclude [test])
  (:require [org.corfield.build :as bb]))

(def lib 'net.clojars.austin/p)
(def version "0.1.0-SNAPSHOT")
(def main 'austin.p.cli.main)

(defn uber "Build the uberjar." [opts]
  (-> opts
      (assoc :lib lib :version version :main main)
      (bb/clean)
      (bb/uber)))

(! 814)-> cat p/projects/p/deps.edn 
{:paths [] ; no src or resources

 :deps {org.clojure/clojure {:mvn/version "1.10.3"}
        poly/component-greeter {:local/root "../../components/greeter"}
        poly/base-cli {:local/root "../../bases/cli"}}

 :aliases {:test {:extra-paths ["test"]
                  :extra-deps {org.clojure/test.check {:mvn/version "1.1.0"}}}

           :build {:deps {io.github.seancorfield/build-clj
                          {:git/tag "v0.4.0" :git/sha "54e39ae"}}
                   :ns-default build}}}

(! 815)-> (cd p/projects/p && clojure -T:build uber)

Cleaning target...

Writing pom.xml...
Skipping coordinate: {:local/root /Users/sean/clojure/fresh/p/components/greeter, :deps/manifest :deps, :deps/root /Users/sean/clojure/fresh/p/components/greeter, :parents #{[]}, :paths [/Users/sean/clojure/fresh/p/components/greeter/src /Users/sean/clojure/fresh/p/components/greeter/resources]}
Skipping coordinate: {:local/root /Users/sean/clojure/fresh/p/bases/cli, :deps/manifest :deps, :deps/root /Users/sean/clojure/fresh/p/bases/cli, :parents #{[]}, :paths [/Users/sean/clojure/fresh/p/bases/cli/src /Users/sean/clojure/fresh/p/bases/cli/resources]}
Copying src, resources...
Compiling austin.p.cli.main...
Building uberjar target/p-0.1.0-SNAPSHOT.jar...

(! 816)-> java -jar p/projects/p/target/p-0.1.0-SNAPSHOT.jar 
Hello, World!
Does that help?

seancorfield00:09:06

This uses my build-clj wrapper to simplify the uber function but under the hood it's basically just what's in the tools.build guide on http://clojure.org -- see https://github.com/seancorfield/build-clj/blob/main/src/org/corfield/build.clj#L207-L215

AustinP00:09:14

Thanks for posting this Sean. The clj-new build.clj is helpful, I was trying to get mine going with the "basic" tools.build to understand it before i grabbed your build-clj lib, but I might just try converting mine to build-clj and see if that solves anything

AustinP00:09:38

its interesting that i dont need to copy anything into target. So the basis that's created from the deps.edn knows of everything since its in the deps.edn, even in a polylith monorepo. My copying everything over could very well be whats throwing this all off. Let me try with just the basis and main class and see what happens

AustinP01:09:13

I think where mine got messy is I have config files in my project directory that the base needs to read from in order to compile. So I was getting build errors since it couldnt find those files, so I was trying to copy-dir everything into the same place, project dir and base src. Since the base is being used by multiple projects, each with a unique set of config files to be fed into the base at compile time.

AustinP01:09:32

So where in the build.clj is the right place to point to the project specific configs, so the base can compile? Or should we be structuring the monorepo differently in order to avoid this situation?

seancorfield01:09:30

Each project should be complete and self-contained, so that's where you'd specify config stuff.

seancorfield01:09:03

If you have a resources folder in your project, you may want :paths ["resources"] in your (project) deps.edn file (but with tools.build you probably don't need it, now I think about it).

seancorfield01:09:16

The build-clj's uber task will copy that automatically. If you wanted it to do it yourself, you would (copy-dir {:src-dirs ["resources"] :target-dir "target/classes"})

seancorfield01:09:45

Several tools.build tasks take the computed basis and that's how they find everything -- and that basis is computed from the project's deps.edn file (i.e., it's automatic if you have cd projects/whatever before you start).

AustinP01:09:45

I do have the project's resources listed in my deps.edn :paths after all the ../../components and ../../base. Why do you say I dont need that there with tools.build?

AustinP01:09:35

hmm okay, if the build-clj's uber will grab all that correctly, and its all based on the basis, let me roll all this back and get on the simplest case using build-clj and see where i get. Will update here, appreciate all the help

seancorfield01:09:34

You should not be using :paths for components or bases. You should be using :deps with :local/root

seancorfield01:09:59

See the project deps.edn I posted above:

:deps {org.clojure/clojure {:mvn/version "1.10.3"}
        poly/component-greeter {:local/root "../../components/greeter"}
        poly/base-cli {:local/root "../../bases/cli"}}

seancorfield01:09:07

The reason you don't need resources with tools.build is that you copy-dir from resources to target/classes and the uber task builds the JAR from target/classes.

seancorfield01:09:56

If you wanted to run code directly from the projects/whatever folder -- or if the resources stuff was needed for running tests in the context of that project then, yes, you still need it in :paths in deps.edn there.

seancorfield01:09:19

But those are two separate contexts: building a JAR vs running code.

AustinP20:09:07

So a large part of whatever was causing my previous issues was likely due to the fact that we've been putting off our migration to the latest Polylith, hence still having :paths and no :local/root in deps. Successfully migrated the workspace today, and after the dust settled, was able to get our projects happily uberjared with your simple build.clj from clj-new ! Much appreciate the help, and the creation and maintenance of such useful tools!!

seancorfield21:09:41

@apaine13 Glad to hear it! When you mentioned :paths like that, I wondered if you were trying to target the older deps.edn version of Polylith. When we started our migration, we targeted the issue-66 branch, so we've been following the latest developments, which definitely made life easier.