From c48da3d316bc56cc994eb1d3ca2fedbe00768523 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 11 Jan 2022 16:30:22 +0100 Subject: [PATCH] :recycle: Refactor backend bundle build process. Now the final artifact is a single, compiled uberjar. It considerably improves startup speed. --- backend/build.clj | 37 +++++++++++++ backend/resources/log4j2.xml | 2 +- backend/scripts/build | 85 +++++------------------------- backend/scripts/manage.template.sh | 2 +- backend/scripts/run.template.sh | 2 +- backend/src/app/main.clj | 3 +- manage.sh | 4 ++ 7 files changed, 59 insertions(+), 76 deletions(-) create mode 100644 backend/build.clj diff --git a/backend/build.clj b/backend/build.clj new file mode 100644 index 000000000..738c82c24 --- /dev/null +++ b/backend/build.clj @@ -0,0 +1,37 @@ +(ns build + (:refer-clojure :exclude [compile]) + (:require + [clojure.tools.build.api :as b] + [clojure.java.io])) + +(def class-dir "target/classes") +(def basis (b/create-basis {:project "deps.edn"})) +(def jar-file "target/penpot.jar") + +(defn clean [_] + (b/delete {:path "target"})) + +(defn jar [_] + (b/copy-dir + {:src-dirs ["src" "resources"] + :target-dir class-dir}) + + (b/compile-clj + {:basis basis + :src-dirs ["src"] + :class-dir class-dir}) + + (b/uber + {:class-dir class-dir + :uber-file jar-file + :main 'clojure.main + :basis basis})) + +(defn compile [_] + (b/javac + {:src-dirs ["dev/java"] + :class-dir class-dir + :basis basis + :exclude [#"com.google.*" #"goog.*"] + :javac-opts ["-source" "11" "-target" "11"]})) + diff --git a/backend/resources/log4j2.xml b/backend/resources/log4j2.xml index 96750c045..d2a045c36 100644 --- a/backend/resources/log4j2.xml +++ b/backend/resources/log4j2.xml @@ -2,7 +2,7 @@ - + diff --git a/backend/scripts/build b/backend/scripts/build index 54c6ea868..d2b7b5966 100755 --- a/backend/scripts/build +++ b/backend/scripts/build @@ -1,79 +1,20 @@ -#!/usr/bin/env bb +#!/usr/bin/env bash -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) UXBOX Labs SL +CURRENT_VERSION=$1; -(ns build - (:require - [clojure.string :as str] - [clojure.java.io :as io] - [clojure.pprint :refer [pprint]] - [babashka.fs :as fs] - [babashka.process :refer [$ check]])) +set -ex -(defn split-cp - [data] - (str/split data #":")) +rm -rf target; +mkdir -p target/classes; +mkdir -p target/dist; +echo "$CURRENT_VERSION" > target/classes/version.txt; -(def classpath - (->> ($ clojure -Spath) - (check) - (:out) - (slurp) - (split-cp) - (map str/trim))) +clojure -T:build jar; +mv target/penpot.jar target/dist/penpot.jar +cp scripts/run.template.sh target/dist/run.sh; +cp scripts/manage.template.sh target/dist/manage.sh; +chmod +x target/dist/run.sh; +chmod +x target/dist/manage.sh; -(def classpath-jars - (let [xfm (filter #(str/ends-with? % ".jar"))] - (into #{} xfm classpath))) -(def classpath-paths - (let [xfm (comp (remove #(str/ends-with? % ".jar")) - (filter #(.isDirectory (io/file %))))] - (into #{} xfm classpath))) -(def version - (or (first *command-line-args*) "%version%")) - -;; Clean previous dist -(-> ($ rm -rf "./target/dist") check) - -;; Create a new dist -(-> ($ mkdir -p "./target/dist/deps") check) - -;; Copy all jar deps into dist -(run! (fn [item] (-> ($ cp ~item "./target/dist/deps/") check)) classpath-jars) - -;; Create the application jar -(spit "./target/dist/version.txt" version) - -(-> ($ jar cvf "./target/dist/deps/app.jar" -C ~(first classpath-paths) ".") check) -(-> ($ jar uvf "./target/dist/deps/app.jar" -C "./target/dist" "version.txt") check) -(run! (fn [item] - (-> ($ jar uvf "./target/dist/deps/app.jar" -C ~item ".") check)) - (rest classpath-paths)) - -;; Copy logging configuration -(-> ($ cp "./resources/log4j2.xml" "./target/dist/") check) - -;; Create classpath file -(let [jars (->> (into ["app.jar"] classpath-jars) - (map fs/file-name) - (map #(fs/path "deps" %)) - (map str))] - (spit "./target/dist/classpath" (str/join ":" jars))) - -;; Copy run script template -(-> ($ cp "./scripts/run.template.sh" "./target/dist/run.sh") check) - -;; Copy run script template -(-> ($ cp "./scripts/manage.template.sh" "./target/dist/manage.sh") check) - -;; Add exec permissions to scripts. -(-> ($ chmod +x "./target/dist/run.sh") check) -(-> ($ chmod +x "./target/dist/manage.sh") check) - -nil diff --git a/backend/scripts/manage.template.sh b/backend/scripts/manage.template.sh index 31f261b6b..f3469f597 100644 --- a/backend/scripts/manage.template.sh +++ b/backend/scripts/manage.template.sh @@ -16,4 +16,4 @@ if [ -f ./environ ]; then source ./environ fi -exec $JAVA_CMD $JVM_OPTS -classpath $(cat classpath) -Dlog4j2.configurationFile=./log4j2.xml clojure.main -m app.cli.manage "$@" +exec $JAVA_CMD $JVM_OPTS -jar penpot.jar -m app.cli.manage "$@" diff --git a/backend/scripts/run.template.sh b/backend/scripts/run.template.sh index 2742fe9fe..33c8eda2e 100644 --- a/backend/scripts/run.template.sh +++ b/backend/scripts/run.template.sh @@ -17,4 +17,4 @@ if [ -f ./environ ]; then fi set -x -exec $JAVA_CMD $JVM_OPTS -classpath "$(cat classpath)" -Dlog4j2.configurationFile=./log4j2.xml "$@" clojure.main -m app.main +exec $JAVA_CMD $JVM_OPTS "$@" -jar penpot.jar -m app.main diff --git a/backend/src/app/main.clj b/backend/src/app/main.clj index ff6954c66..27e47fb0e 100644 --- a/backend/src/app/main.clj +++ b/backend/src/app/main.clj @@ -9,7 +9,8 @@ [app.common.logging :as l] [app.config :as cf] [app.util.time :as dt] - [integrant.core :as ig])) + [integrant.core :as ig]) + (:gen-class)) (def system-config {:app.db/pool diff --git a/manage.sh b/manage.sh index a2a07b4b5..fc2e43d63 100755 --- a/manage.sh +++ b/manage.sh @@ -207,6 +207,10 @@ function usage { } case $1 in + version) + print-current-version + ;; + ## devenv related commands pull-devenv) pull-devenv ${@:2};