用Gradle构建可执行的Jar以进行加特林

Younes IT

问题

我已经创建了一个Scala and Gatling项目,我想通过该项目执行一些负载测试。这个想法是我建立一个可执行的jar,然后简单地执行以运行所说的负载测试。

到目前为止,我在IDE中运行负载测试没有问题。但是,当我尝试将其组装时:

gradle assemble

罐子是空的,即包含来自的我的实用工具类src/main/scala,但没有来自src/gatling/*下面的我的设置)。

我已经尝试过胖罐子,但是该任务给了我错误:

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插件非常固执,仅在中包含Gatling依赖关系gatlingCompileClasspath因此,为了能够编写负载测试,我需要具有一个源集gatlingmain/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
}
Younes IT

经过大量的实验和小时的搜索提示,这是构建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,因为即使存在那些配置(至少是后者),在fatJar任务中configurations.gatlingRuntimeClasspath.collect还是类似configurations.gatling.collect什么都不做。为了使依赖项进入您的jar(对自身,scala和诸如此类的东西进行填充),这是必需的。

2.取决于

确切地说:

(type: Jar, dependsOn: [':gatlingClasses', ':processResources'])

特别是gatlingClasses通过扫描gatling gradle插件的文档页面发现的这将代码添加到src/gatling/*

我可以通过执行以下操作来运行模拟(-DConfig是可选的):

java -Dconfig.resource=myConf.conf -jar the-jar-i-created.jar -s org.comp.pckg.DemoSimulation

正如George Leung所指出的,如果您按上述方式开始注释,那么也有可能在jar之外生成报告(我错误地忘记了我添加了option -nr)。为了自定义文件夹,我gatling.confsrc/gatling/resources其中添加了以下值:

gatling {
  core {
    directory {
      results = "/gatling/reports"
    }
  }
}

这将覆盖您在此处可以找到的默认值

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章