使用sbt-native-packager构建含Gatling的Scala项目Docker镜像遇异常
问题:使用sbt-native-packager构建含Gatling的Docker镜像时提示无法找到Gatling库
我正尝试使用sbt-native-packager为包含Gatling负载测试的Scala项目构建Docker镜像。Gatling负载测试可正常运行,但执行docker:publishLocal命令构建镜像时,抛出java.lang.IllegalArgumentException异常,提示“Couldn't locate Gatling libraries in the classpath”。
相关配置
build.sbt
ThisBuild / version := "0.1.0-SNAPSHOT" ThisBuild / scalaVersion := "2.13.4" ThisBuild / scapegoatVersion := "1.4.8" lazy val root = (project in file(".")) .settings( name := "face-recognition" ) enablePlugins(GatlingPlugin, JavaAppPackaging, DockerPlugin, ScoverageSbtPlugin) Docker / packageName := "face-recognition" Docker / version := "0.0.1" dockerExposedPorts := Seq(8080) dockerChmodType := DockerChmodType.UserGroupWriteExecute dockerPermissionStrategy := DockerPermissionStrategy.CopyChown dockerAdditionalPermissions += (DockerChmodType.UserGroupPlusExecute, "/var/run/") Docker / daemonUserUid := None Docker / daemonUser := "root" dockerBaseImage := "amazoncorretto:11-alpine3.18-jdk" libraryDependencies ++= Seq( "io.gatling.highcharts" % "gatling-charts-highcharts" % "3.9.1" % "test", "io.gatling" % "gatling-test-framework" % "3.9.1" % "test", "com.typesafe.akka" %% "akka-slf4j" % "2.8.0", "io.gatling" % "gatling-http" % "3.5.1" )
plugin.sbt
addSbtPlugin("io.gatling" % "gatling-sbt" % "4.2.6")
错误信息
[error] stack trace is suppressed; run last Gatling-it / enterprisePackage for the full output [error] (Gatling-it / enterprisePackage) java.lang.IllegalArgumentException: Couldn't locate Gatling libraries in the classpath
解决方案
1. 统一Gatling依赖版本
当前配置中gatling-http版本为3.5.1,与其他Gatling依赖的3.9.1版本不一致,这会导致类路径依赖冲突或缺失。将所有Gatling依赖版本统一为3.9.1:
libraryDependencies ++= Seq( "io.gatling.highcharts" % "gatling-charts-highcharts" % "3.9.1" % "test", "io.gatling" % "gatling-test-framework" % "3.9.1" % "test", "com.typesafe.akka" %% "akka-slf4j" % "2.8.0", "io.gatling" % "gatling-http" % "3.9.1" // 统一版本 )
2. 调整Gatling依赖作用域或禁用不必要的任务
- 如果你的Docker镜像需要运行Gatling测试,将Gatling核心依赖从
test作用域改为compile,确保打包时包含这些依赖:
libraryDependencies ++= Seq( "io.gatling.highcharts" % "gatling-charts-highcharts" % "3.9.1", "io.gatling" % "gatling-test-framework" % "3.9.1", "com.typesafe.akka" %% "akka-slf4j" % "2.8.0", "io.gatling" % "gatling-http" % "3.9.1" )
- 如果只是打包应用,不需要在Docker构建过程中触发Gatling的
enterprisePackage任务,直接禁用该任务:
// 禁用Gatling的enterprisePackage任务,避免构建Docker时触发 Gatling / enterprisePackage := {} Gatling-it / enterprisePackage := {}
3. 确保打包时包含必要的测试依赖(如果需要)
如果Docker镜像需要包含测试代码和依赖,配置sbt-native-packager将Test作用域的内容加入打包:
// 将Test作用域的映射添加到Universal打包中 mappings in Universal ++= (mappings in Test).value // 确保Test依赖被包含 dependencyClasspath in Universal ++= (dependencyClasspath in Test).value
内容的提问来源于stack exchange,提问作者Asif khAn
相关产品推荐
相关产品推荐

