Jenkins用户在构建节点运行测试时因缺失scala-library.jar失败的问题
Missing scala-library.jar When Running SBT Tasks as Jenkins User Hey there, I’ve run into this exact kind of Jenkins + SBT permission/cache issue a few times. Let’s break down why this is happening and how to fix it:
Your root/local user can run the SBT command fine because it’s using the cached scala-library.jar in /root/.cache/coursier/, but the Jenkins user doesn’t have access to that directory. Add to that, SBT 0.13.x has some quirks with Coursier caching that can make this worse. Here are the most effective solutions:
Why This Happens
- The Jenkins user lacks read permissions for the root user’s Coursier cache directory (
/root/.cache/coursier/). - SBT might be trying to reuse root’s cached dependencies, but can’t access them.
- Missing environment variables or SBT config for the Jenkins user could prevent proper dependency pulls from Artifactory.
Solutions to Try
1. Set Up a Coursier Cache for Jenkins User
First, give Jenkins its own cache directory so it doesn’t rely on root’s files:
- Switch to the Jenkins user on your build slave:
sudo su - jenkins - Create and secure the cache directory:
mkdir -p ~/.cache/coursier/v1 chmod -R 755 ~/.cache - Manually trigger a dependency pull to populate the cache:
/usr/lib/jvm/java-1.8.0/bin/java -Xmx4250M -Dsbt.log.noformat=true -jar /tmp/jenkins/slaves/build-slave-cb996aa/tools/org.jvnet.hudson.plugins.SbtPluginBuilder_SbtInstallation/default/bin/sbt-launch.jar update
This will download scala-library.jar (and other dependencies) into Jenkins’ own cache, so the assembly command can find it later.
2. Configure SBT Globally for Jenkins
Tweak SBT’s global config to use Jenkins’ cache and explicitly define repositories:
- As the Jenkins user, create an SBT repositories file:
sudo su - jenkins mkdir -p ~/.sbt/0.13 cat > ~/.sbt/0.13/repositories << EOF [repositories] local maven-central: https://repo1.maven.org/maven2/ artifactory: $ARTIFACTORY_HOST EOF - Add a cache path override to SBT options:
echo "-Dsbt.coursier.cache=~/.cache/coursier" >> ~/.sbt/0.13/sbtopts
This tells SBT exactly where to store/retrieve dependencies for the Jenkins user.
3. Verify Jenkins’ Environment & Artifactory Access
Make sure Jenkins has the right environment variables and Artifactory permissions:
- Add a step in your Jenkins job to print variables and test Artifactory access:
echo "ARTIFACTORY_HOST: $ARTIFACTORY_HOST" echo "USER: $USER" # Test if Jenkins can reach Artifactory curl -u $USER:$PASSWORD $ARTIFACTORY_HOST/org/scala-lang/scala-library/2.11.11/
If the curl fails, fix the credentials or Artifactory permissions for the Jenkins user—sometimes a failed auth makes it look like a missing jar.
4. Clean & Rebuild Dependencies from Scratch
If all else fails, wipe Jenkins’ SBT/cache files and start fresh:
sudo su - jenkins rm -rf ~/.sbt ~/.cache/coursier # Run the full command to pull dependencies and assemble /usr/lib/jvm/java-1.8.0/bin/java -Xmx4250M -Dsbt.log.noformat=true -jar /tmp/jenkins/slaves/build-slave-cb996aa/tools/org.jvnet.hudson.plugins.SbtPluginBuilder_SbtInstallation/default/bin/sbt-launch.jar 'set credentials += Credentials("Artifactory Realm", $ARTIFACTORY_HOST, $USER, $PASSWORD)' clean update assembly
Verify the Fix
After trying any of the above, test the command manually as the Jenkins user to confirm it works:
sudo su - jenkins /usr/lib/jvm/java-1.8.0/bin/java -Xmx4250M -Dsbt.log.noformat=true -jar /tmp/jenkins/slaves/build-slave-cb996aa/tools/org.jvnet.hudson.plugins.SbtPluginBuilder_SbtInstallation/default/bin/sbt-launch.jar 'set credentials += Credentials("Artifactory Realm", $ARTIFACTORY_HOST, $USER, $PASSWORD)' assembly
内容的提问来源于stack exchange,提问作者Nir Orman

