Fork me on GitHub
#polylith
<
2023-10-18
>
Mark Sto17:10:02

Hi all! Any clues on how to pre-compile Java sources in a Polylith workspace? I now have a single component with a single Java class, so I don’t want to extract it into a library and would like to keep things simple. I’ve already tried to configure a build profile and ns as per https://github.com/seancorfield/java-clojure-example/tree/master and https://andersmurphy.com/2021/12/12/clojure-compiling-java-source-with-tools-build.html, but it doesn’t seem to work. Does anyone have an example of a similar project?

seancorfield18:10:27

Can you explain "it doesn’t seem to work" and also provide a bit more detail on what you have actually tried? Is having a standalone clojure -T:build compile-java command sufficient for your needs, for example? (i.e., that you run manually as needed to work with your :dev REPL)

Mark Sto08:10:55

Sean, hi! I had no doubt that you would be the one to respond. 😃 Sorry, I was in a hurry yesterday and didn’t go into details, hoping that someone will just send me a link to the existing repo. In essence, the commands I tried completed without any errors (i.e. the classpath was assembled correctly), but as a result, the target directory did not appear anywhere in the project. And, yeah, so far I find having a standalone command / bb task sufficient for the project needs — I’ll overengineer this later on. 😂 So, I played around with this a little more today and came up with a working setup. Here it is!

├── components
│   └── happy-face
│       └── src
│           └── 
│       └── java-src
│           └── 
│       └── deps.edn
├── build.clj
└── deps.edn
The root deps.edn has a :build alias as simple as in examples I’ve linked above. The component’s deps.edn lists "target/classes" in :paths. My build.clj script is in the root of the workspace and looks like this:

Mark Sto08:10:09

@U04V70XH6, the only problem that remains is tests failing in IntelliJ IDEA with its own classpath, even though the compiled class file is available. NB: I still use a rather old IntelliJ version, 2020.2.4, that comes with Cursive of the time, which doesn’t understand Polylith projects quite well yet.

Mark Sto09:10:39

I scanned the IntelliJ’s classpath and it turned out to be a bit outdated (the brick’s src-java made it to there, but not the target/classes). I then double-checked that I have all aliases enabled in “Clojure Deps” tab and refreshed the project. Now my tests are back to life! 😃

1
Mark Sto09:10:16

@U04V70XH6, what best practices can you recommend for similar mixed Clojure/Java codebase projects in production development? Do you use Java in the World Singles codebase?

seancorfield16:10:32

We do not use Java code at all at work, just lots of Java libraries. We've never felt a need to "drop down" to Java -- Clojure does all we need.