问题
我已经创建了一个Scala and Gatling项目,我想通过该项目执行一些负载测试。这个想法是我建立一个可执行的jar,然后简单地执行以运行所说的负载测试。
到目前为止,我在IDE中运行负载测试没有问题。但是,当我尝试将其组装时:
gradle assemble
jar 是空的,即包含来自src/main/scala
的实用程序类,但不包含来自src/gatling/*
的实用程序类(请参阅下文了解我的设置)。我已经尝试过胖 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
}
最佳答案
经过大量的实验和小时的搜索提示,这是构建gradle片段的方法:
configurations {
fatJarDependencies.extendsFrom gatling
}
task fatJar(type: Jar, dependsOn: [':gatlingClasses', ':processResources']) {
manifest {
attributes 'Implementation-Title': 'Preparing test',
'Implementation-Version': '',
'Main-Class': 'io.gatling.app.Gatling'
}
classifier = 'all'
from files(sourceSets.main.output.classesDirs)
from files(sourceSets.gatling.output)
from { configurations.fatJarDependencies.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
}
关键如下:1.配置
fatJarDependencies.extendsFrom gatling
您需要扩展gatling
,因为configurations.gatlingRuntimeClasspath.collect
或configurations.gatling.collect
之类的东西在fatJar任务中没有任何作用,即使这些配置存在(至少是后者)。为了使依赖项进入您的jar(对自身,scala和诸如此类的东西进行填充),这是必需的。2.取决于
确切地说:
(type: Jar, dependsOn: [':gatlingClasses', ':processResources'])
特别是gatlingClasses
,我是通过扫描gatling gradle plugin的文档页面发现的。这将代码添加到src/gatling/*
下我可以通过执行以下操作来运行模拟(
-DConfig
是可选的):java -Dconfig.resource=myConf.conf -jar the-jar-i-created.jar -s org.comp.pckg.DemoSimulation
如George Leung所指出的,如果您按上述方式开始注释,则还可以在jar之外生成报告(我错误地忘记添加了-nr
选项)。为了自定义文件夹,我在gatling.conf
中添加了带有以下值的src/gatling/resources
:gatling {
core {
directory {
results = "/gatling/reports"
}
}
}
这将覆盖您可以找到here的默认值。
关于scala - 用Gradle构建可执行的Jar以进行加特林,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64232022/