当我需要在同一项目中运行两个或更多的 Clojure 实例(带有不同的参数)时,它们最终会竞争同一目录中创建的相同缓存文件夹。在这种情况下,我们看到的错误是找不到或加载主类 clojure.main
。有时候会有其他错误。
如果我的脚本逻辑理解正确,它会选择当前目录作为缓存的存储位置,并使用参数的组合作为唯一的标识符,但在我这个例子中,生成的文件夹是相同的。
# Determine whether to use user or project cache
if [[ -f deps.edn ]]; then
cache_dir=.cpcache
else
cache_dir="$user_cache_dir"
fi
最好是引入一种锁定机制来防止竞争情况,或者作为权宜之计,提供一个选项来指定缓存目录的额外参数或校验和。
可能的解决方案
diff --git a/src/main/resources/clojure/install/clojure b/src/main/resources/clojure/install/clojure
index 67272b7..53fd7ec 100755
--- a/src/main/resources/clojure/install/clojure
+++ b/src/main/resources/clojure/install/clojure
@@ -22,6 +22,7 @@ jvm_opts=()
resolve_aliases=()
classpath_aliases=()
repl_aliases=()
+cache_extra=
mode="repl"
while [ $# -gt 0 ]
do
@@ -108,6 +109,11 @@ do
deps_data="${1}"
shift
;;
+ -Scache)
+ shift
+ cache_extra="${1}"
+ shift
+ ;;
-Scp)
shift
force_cp="${1}"
@@ -197,7 +203,7 @@ if "$help"; then
on the JVM, e.g. to start a REPL or invoke a specific function with data.
The Clojure tools will configure the JVM process by defining a classpath
(of desired libraries), an execution environment (JVM options) and
- specifying a main class and args.
+ specifying a main class and args.
Using a deps.edn file (or files), you tell Clojure where your source code
resides and what libraries you need. Clojure will then calculate the full
@@ -234,6 +240,7 @@ if "$help"; then
-Spath Compute classpath and echo to stdout only
-Spom Generate (or update) pom.xml with deps and paths
-Stree Print dependency tree
+ -Scache CACHE Provide arbitrary data to include into cache hash
-Scp CP Do NOT compute or cache classpath, use this one instead
-Srepro Ignore the ~/.clojure/deps.edn config file
-Sforce Force recomputation of the classpath (don't use the cache)
@@ -320,7 +327,7 @@ else
fi
# Construct location of cached classpath file
-val="$(join '' ${resolve_aliases[@]})|$(join '' ${classpath_aliases[@]})|$(join '' ${repl_aliases[@]})|$exec_aliases|$main_aliases|$deps_data|$tool_name|$tool_aliases"
+val="${cache_extra}|$(join '' ${resolve_aliases[@]})|$(join '' ${classpath_aliases[@]})|$(join '' ${repl_aliases[@]})|$exec_aliases|$main_aliases|$deps_data|$tool_name|$tool_aliases"
for config_path in "${config_paths[@]}"; do
if [[ -f "$config_path" ]]; then
val="$val|$config_path"