问题
我创建了一个Scala和Gatling项目,想要执行一些负载测试。我的想法是构建可执行的jar文件,然后简单地执行它以运行所述的负载测试。
到目前为止,在我的IDE中运行负载测试没有任何问题。但是,当我尝试使用以下命令进行组装时:
gradle assemble
这个jar包是空的,即只包含我从src/main/scala
中提取的实用类,而没有src/gatling/*
中的内容(有关我的设置,请参见下文)。
我尝试使用fat jar,但该任务给出了以下错误:
Execution failed for task ':fatJar'.
> Cannot expand ZIP 'D:\projects\load-test\build\classes\java\main' as it does not exist.
我不知道为什么它首先期望Java。
我的设置
我的项目结构如下:
load-test/
├── src/
│ ├── gatling/
│ │ ├── resources
│ │ └── scala
│ ├── main/
│ │ ├── resources
│ │ └── scala
│ └── test/
│ ├── resources
│ └── scala
├── ...
└── build.gradle
我发现 Gatling 插件非常顽固,它只在
gatlingCompileClasspath
中包含 Gatling 依赖项。因此,为了能够编写负载测试,我需要有一个 gatling
源集。 main/scala
目录用于实现一些实用程序功能,例如创建自定义时间戳等。 test/scala
用于测试这些功能。我的
build.gradle
如下:plugins {
id "com.github.maiflai.scalatest" version "0.25"
id 'io.gatling.gradle' version "3.4.0"
id "scala"
}
apply plugin: "scala"
apply plugin: "com.github.maiflai.scalatest"
compileScala.targetCompatibility = 1.8
ScalaCompileOptions.metaClass.useAnt = false
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
sourceSets {
gatling {
scala.srcDirs = ["src/gatling/scala"]
resources.srcDirs = ["src/gatling/resources"]
}
test {
scala.srcDirs = ["src/test/scala"]
resources.srcDirs = ["src/test/resources"]
}
}
dependencies {
testCompile "gradle.plugin.com.github.maiflai:gradle-scalatest:0.25"
testCompile "org.scala-lang:scala-library:2.12.12"
testCompile 'org.scalatest:scalatest_2.12:3.0.0'
testRuntime 'org.pegdown:pegdown:1.4.2'
}
gatling {
toolVersion = '3.4.0'
scalaVersion = '2.12.12'
simulations = {
include "**/*Simulation.scala"
}
systemProperties = ['file.encoding': 'UTF-8']
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Version': project.version,
'Main-Class': 'io.gatling.app.Gatling'
}
setArchiveBaseName project.name + '-all'
from {
configurations.gatlingRuntimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude 'META-INF/MANIFEST.MF'
exclude 'META-INF/*.SF'
exclude 'META-INF/*.DSA'
exclude 'META-INF/*.RSA'
}
with jar
}