Skip to content

Instantly share code, notes, and snippets.

@gnarroway
Last active November 22, 2019 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gnarroway/888ad96a2b7135b2ef2648b3880f36ec to your computer and use it in GitHub Desktop.
Save gnarroway/888ad96a2b7135b2ef2648b3880f36ec to your computer and use it in GitHub Desktop.
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
Copy link

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