Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create an uberjar from a clojure classpath
# Assembles dependencies from a classpath generated by clojure -Spath
#
# Usage: ./assemble.sh $(clojure -R:dev -Spath)
classpath=$1
# Clean output dir
mkdir -p target/classes
rm -rf target/classes/*
for path in ${classpath//:/ }; do
if [[ -d $path ]]; then
# Copy our source paths
echo "Copying $path"
cp -R $path/* target/classes/
elif [[ -f $path && $path == *.jar ]]; then
# Unpack jars, never overwriting (to retain classpath precedence)
# Also exclude some extensions that will cause issues bundling signed jars
echo "Unpacking $path"
unzip -n -qq $path -x META-INF/*.SF META-INF/*.RSA META-INF/*.DSA -d target/classes > /dev/null 2>&1
else
echo "Ignoring $path"
fi
done
# Optional step
# Add your project pom with correct version to the output
# Assumes you have a :project-version in your deps.edn
version=$(cat deps.edn | grep :project-version | cut -d'"' -f2)
clojure -Spom
sed -i '' "s/<version>.*<\/version>/<version>$version<\/version>/" pom.xml
mkdir -p target/classes/META-INF/maven/foo/foo
cp pom.xml target/classes/META-INF/maven/foo/foo/
# Creates an executable jar, which can be run with:
# java -jar target/app.jar
# java -cp target/app.jar clojure.main -m app.core
#
# --main-class expects that you have a main class which you compiled before assembling.
# clj -e "(compile 'app.core)"
# See https://clojure.org/guides/deps_and_cli#aot_compilation for details
jar --create --file target/app.jar --main-class app.core -C target/classes/ .
@DogLooksGood

This comment has been minimized.

Copy link

DogLooksGood commented Nov 22, 2019

Hi, could you give a sample deps.edn file for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.