Full Clojure project in 5 seconds!
Every time you start a new Clojure project, there is a lot of boilerplate that needs to be added:
deps.ednwith aliases to run tests and build actionsbuild.cljwith build actions.gitignoreREADME.md- you name it…
Creating all these can be easily automated with deps-new.
Install deps-new
deps-new intended to be installed as a “tool” for Clojure CLI.
“Tools” in Clojure CLI
Tools are 3rd party libraries that can be installed and than invoked from Clojure CLI using clj -T option. They use their own classpath and do not interfere with the project’s deps.edn if you run tool commands from the project directory.
Few commands you may need:
clj -Ttools list - to view already installed tools
clj -Ttools remove :tool tool-name - to remove the tool
clj -Ttools install tool-path tool-version :as tool-name - to install a new tool
More info about tools you can find in this video.
Installing deps-new tool
Here is a command to add deps-new to your system
|
|
:as new part of the command means that “new” is now a name for deps-new tool on your system. You can use any name you want.
Let’s check our list of tools now:
|
|
To check what functions new tool exposes you can run the command:
clojure -A:deps -Tnew help/doc
Create a library
Now, when deps-new is installed, we can use it to create a new library.
The following command will create a library with root namespace vkjr and one source file mycoollib.clj in it
|
|
And here is a content of new library:
|
|
Top level directory of the library was also named mycoollib but you can override this behavior by providing additional key/value pair to the library creation command :target-dir some-folder-name.
Content of created library
As you can see, there are plenty of automatically generated files in our new lib.
deps.edn from the start contains aliases for testing and build actions
|
|
src/vkjr/mycoollib.clj contains a dummy function
|
|
And in test/vkjr/mycoollib_test.clj already exists one failing test
|
|
build.clj contains build actions test, ci, install and deploy
|
|
Note, that build.clj implements actions using the build-clj library instead of standard Clojure’s tools.build. That library hides some unnecessary details and provides the functionality of deploying to Clojars, which is missed in tools.build
There are a bunch of other useful files also: .gitignore, README.md, pom.xml, LICENSE.. real time-saver, ha?
Create an application
Creating an application with deps-new as simple as a library:
|
|
And here is a folder structure:
|
|
Main differences from the library project:
src/vkjr/mycoolapp.clj contains the (-main) and (greet) functions
|
|
deps.edn has additional aliases run-m and run-x to run (main) and (greet) functions respectively
|
|
build.clj creates an uberjar instead of a jar and doesn’t have (install) and (deploy) functions
|
|
Create a minimal “scratch” project
For the cases when you don’t need a full-blown application, but merely a playground, deps-new supports creating a “scratch” projects:
|
|
Folder structure for such “scratch”:
|
|
With almost empty deps.edn and scratch.clj with (-main) and (greet) functions, like in the application example
Using deps-new you can create your own project template, but this is another story… ;)