unable to resolve ssh class in gradle.build - gradle

I am trying to invoke a custom groovy plugin from gradle build file. But I am getting the error while resolving the classes for ssh. Below is the build file, part of custom groovy plugin and the error.
build.gradle
plugins {
id 'org.sonarqube' version '2.0.1'
id 'groovy'
id 'org.hidetake.ssh' version'2.7.0'
}
dependencies {
compile gradleApi()
compile localGroovy()
}
CustPlugin.groovy
package com.nielsen.gradle
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.text.SimpleDateFormat
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.GradleException
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.tasks.bundling.Zip
import org.hidetake.groovy.ssh.Ssh.*
import org.hidetake.groovy.ssh.core.Service
import com.nielsen.gradle.cmRegistry.CMRegistryPlugin
Error
C:\Users\528302\Documents\gradle_all\projectf1>gradle build
:compileJava UP-TO-DATE
:compileGroovy
startup failed:
C:\Users\528302\Documents\gradle_all\projectf1\src\main\groovy\com\nielsen\gradle\CustPlugin.groovy: 14: unable to resolve class org.hidetake.groovy.ssh.Ssh
# line 14, column 1.
import org.hidetake.groovy.ssh.Ssh
^
C:\Users\528302\Documents\gradle_all\projectf1\src\main\groovy\com\nielsen\gradle\CustPlugin.groovy: 15: unable to resolve class org.hidetake.groovy.ssh.core.Service
# line 15, column 1.
import org.hidetake.groovy.ssh.core.Service
^
C:\Users\528302\Documents\gradle_all\projectf1\src\main\groovy\com\nielsen\gradle\CustPlugin.groovy: 17: unable to resolve class com.nielsen.gradle.cmRegistry.CMRegi
stryPlugin
# line 17, column 1.
import com.nielsen.gradle.cmRegistry.CMRegistryPlugin
^
Please help resolving this...Thanks.

You are mixing two things. By using the plugins { } closure you are adding dependencies for the buildscript itself. But in this case the code you are building has a dependency on some library, not the buildscript.
Try to add the following into dependencies { }
compile group: 'org.hidetake', name: 'groovy-ssh', version: '2.8.0'
so you end up having
plugins {
id 'org.sonarqube' version '2.0.1'
id 'groovy'
id 'org.hidetake.ssh' version'2.7.0'
}
dependencies {
compile gradleApi()
compile localGroovy()
compile group: 'org.hidetake', name: 'groovy-ssh', version: '2.8.0'
}

Related

Dockerfile can not build jar file : Main class name has not been configured

I am building a dockerfile for a microservices architecture (spring) but I find some difficulties.
I got this error when buidling the boot jar
Main class name has not been configured and it could not be resolved
my dockerfile is
FROM gradle:6.5-jdk11 AS TEMP_BUILD_IMAGE
ENV APP_HOME=/usr/app/
WORKDIR $APP_HOME
COPY build.gradle.kts settings.gradle.kts $APP_HOME
COPY gradle $APP_HOME/gradle
COPY --chown=gradle:gradle . /home/gradle/src
USER root
RUN chown -R gradle /home/gradle/src
RUN gradle bootJar || return 0
COPY . .
RUN gradle clean bootJar
FROM openjdk:11-jdk
ENV ARTIFACT_NAME=app.jar
ENV APP_HOME=/usr/app/
WORKDIR $APP_HOME
COPY --from=TEMP_BUILD_IMAGE $APP_HOME/build/libs/$ARTIFACT_NAME .
ENTRYPOINT exec java -jar ${ARTIFACT_NAME}
The error is in 9th step
RUN gradle bootJar || return 0
I tried to put the name of the class in the gradle file, and i got this error
Exception in thread "main" java.lang.NoSuchMethodException: com.test.config.ConfigServerApp.main([Ljava.lang.String;)
this is my gradle file
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.springframework.boot.gradle.tasks.bundling.BootJar
plugins {
id("io.spring.dependency-management") version "1.0.9.RELEASE"
id("org.springframework.boot") version "2.3.0.RELEASE"
kotlin("plugin.spring") version "1.3.72"
kotlin("jvm") version "1.3.72"
}
repositories {
mavenCentral()
}
group = "com.test"
version = "1.0.0"
java.sourceCompatibility = JavaVersion.VERSION_11
extra["springCloudVersion"] = "Hoxton.SR5"
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.cloud:spring-cloud-config-server")
implementation("org.springframework.cloud:spring-cloud-config-monitor")
implementation("org.springframework.cloud:spring-cloud-starter-bus-amqp")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = listOf("-Xjsr305=strict")
}
}
tasks.withType<BootJar> {
archiveFileName.set("app.jar")
mainClassName = "com.test.config.ConfigServerApp"
}
this is my main class (i m using kotlin)
package com.test.config
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.config.server.EnableConfigServer
#EnableConfigServer
#SpringBootApplication
class ConfigServerApp
fun main(args : Array<String>)
{
runApplication<ConfigServerApp>(*args)
}
when I launch the application from my editor it works, but when I launch it from the jar file or from docker, it does not work
how can i fix this please?
Just to make a formal answer from my comment since it worked. As I explained in the comment: When we use Kotlin, the real main class is generated behind the scenes with Kt suffix, you should find it in your build folder after running gradle bootJar.
You should update the mainClassName to com.test.config.ConfigServerAppKt as shown below:
tasks.withType<BootJar> {
archiveFileName.set("app.jar")
mainClassName = "com.test.config.ConfigServerAppKt"
}
As explained in the Kotlin docs :
All the functions and properties declared in a file app.kt inside a package org.example, including extension functions, are compiled into static methods of a Java class named org.example.AppKt.
So your main method will be part of java class named ConfigServerAppKt after compilation. So you should change your mainClassName to com.test.config.ConfigServerAppKt.

SpringBoot gradle plugin complains "Unresolved reference: providedRuntime"

I'm following the latest SpringBoot gradle plugin reference in order to setup a SpringBoot project that can build a uberjar.
Below is the full content of build.gradle.kts:
plugins {
java
application
id("org.springframework.boot") version "2.2.1.RELEASE"
id("io.spring.dependency-management") version "1.0.8.RELEASE"
}
repositories {
jcenter()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
testImplementation("org.testng:testng:6.14.3")
}
application {
// Define the main class for the application.
mainClassName = "com.demo.App"
}
val test by tasks.getting(Test::class) {
// Use TestNG for unit tests
useTestNG()
}
dependencyManagement {
imports {
mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
}
}
Unfortunately, when I run gradle commands I get the following error:
> Configure project :
e: /Users/xux/Documents/toys/web/build.gradle.kts:15:5: Unresolved reference: providedRuntime
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/xux/Documents/toys/web/build.gradle.kts' line: 15
* What went wrong:
Script compilation error:
Line 15: providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
^ Unresolved reference: providedRuntime
1 error
I'm using Java 1.8.0_201, Gradle 6.0, Kotlin 1.3.50, Mac OS X 10.15.1 x86_64.

Error with Building Fatjar Using Kotlin DSL

I am trying to build a fatjar using ShadowJar. My app and gradle code are below. I'm using Gradle 5.0 for the build. When I run ./gradlew run, the code works. When I run 'gradle shadowjar', and run the fatjar using 'java -jar' in the 'build/lib' folder, I get the below error.
I'm guessing the dependencies are not getting loaded in the fatjar? I have also used Groovy to build the Gradle file, and I get the same error.
Am I correct in that I am not including all of the dependencies in the fatjar file? If that is the case, any idea on how to modify the Gradle file to make sure this is included?
Gradle Kotlin DSL
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
// Apply the Kotlin JVM plugin to add support for Kotlin on the JVM.
id("org.jetbrains.kotlin.jvm").version("1.3.21")
id("com.github.johnrengelman.shadow") version "5.0.0"
// Apply the application plugin to add support for building a CLI application.
application
}
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
dependencies {
// Use the Kotlin JDK 8 standard library.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")
// Use the Kotlin JUnit integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
implementation("org.http4k:http4k-core:3.143.1")
implementation("org.http4k:http4k-server-jetty:3.143.1")
implementation("org.http4k:http4k-client-okhttp:3.143.1")
implementation("org.http4k:http4k-client-apache:3.143.1")
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
application {
// Define the main class for the application.
mainClassName = "com.benito.AppKt"
}
tasks.withType<ShadowJar> {
archiveBaseName.set("${project.name}-all")
}
Kotlin Code
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.routing.bind
import org.http4k.routing.path
import org.http4k.routing.routes
import org.http4k.server.Jetty
import org.http4k.server.asServer
fun main(args: Array<String>) {
println("Starting")
val app: HttpHandler = routes(
"/ping" bind Method.GET to { _: Request -> Response(OK).body("pong!") },
"/greet/{name}" bind Method.GET to { req: Request ->
val path: String? = req.path("name")
Response(OK).body("hello ${path ?: "anon!"}")
}
)
app.asServer(Jetty(8080)).start()
}
Error Message
2019-05-15 11:15:13.925:INFO::main: Logging initialized #287ms to org.eclipse.jetty.util.log.StdErrLog
2019-05-15 11:15:14.039:INFO:oejs.Server:main: jetty-9.4.z-SNAPSHOT; built: 2019-04-29T20:42:08.989Z; git: e1bc35120a6617ee3df052294e433f3a25ce7097; jvm 1.8.0_211-b12
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.eclipse.jetty.http.MimeTypes$Type.<init>(MimeTypes.java:103)
at org.eclipse.jetty.http.MimeTypes$Type.<clinit>(MimeTypes.java:58)
at org.eclipse.jetty.http.MimeTypes.<clinit>(MimeTypes.java:191)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:836)
at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:278)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:167)
at org.eclipse.jetty.server.Server.start(Server.java:418)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.server.Server.doStart(Server.java:382)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.http4k.server.Jetty$toServer$3.start(jetty.kt:33)
at com.benito.AppKt.main(App.kt:38)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at org.eclipse.jetty.http.PreEncodedHttpField.<clinit>(PreEncodedHttpField.java:70)
... 14 more
The problem here is that during the process of merging all the JARs in to one FatJAR, the contents of the META-INF folder in the http4k-core JAR (which contains the mime.types file used for matching a request to a content type) is somehow omitted or overridden by the merging process.
This isn't http4k specific and is (quite) a common problem with the use of the shadow-jar gradle plugin. But it can be easily solved by just correctly configuring the plugin as below:
shadowJar {
mergeServiceFiles() // <-- this!
}
From the http4k side, the exception message generated has been changed to highlight this problem.

Stackoverflow error when uses Apache Jmeter Core

I am new in Gradle. I am using my gradle with kotlin dsl script. When I execute using
gradle bootRun
Then it throw StackoverFlow error for log4J
Exception in thread "main" java.lang.StackOverflowError at
java.lang.reflect.InvocationTargetException.(InvocationTargetException.java:72)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:110)
at
org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:123)
at
org.apache.logging.log4j.util.StackLocatorUtil.getCallerClass(StackLocatorUtil.java:55)
As Soon as I comment the implementation( "org.apache.jmeter:ApacheJMeter_core:5.0") from the build script, it doesn't throw any Error, what I should do? Please help.
Build.Gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
java
kotlin("jvm") version "1.3.11"
"kotlin-spring"
id("org.springframework.boot") version "2.1.1.RELEASE"
}
group = "org.kayd"
version = "1.0-DEV"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
implementation("org.apache.commons:commons-collections4:4.2")
implementation("org.springframework.boot:spring-boot-starter:2.1.1.RELEASE")
implementation( "org.apache.jmeter:ApacheJMeter_core:5.0")
// implementation("org.apache.jmeter:ApacheJMeter_java:5.0")
// implementation( "org.apache.jmeter:ApacheJMeter_http:5.0")
// implementation( "org.antlr:antlr:3.1.3")
// implementation( "org.json:json:20180813")
testCompile("junit", "junit", "4.12")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
Main Fun file
package org.kayd
import org.springframework.boot.Banner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication
#EnableConfigurationProperties
#SpringBootApplication
open class StartApplication
fun main(args: Array<String>) {
runApplication<StartApplication>(*args) {
setBannerMode(Banner.Mode.OFF)
setLogStartupInfo(true)
}
}
If you list your project dependencies you will see that
org.apache.jmeter:ApacheJMeter_core:5.0 has transitive dependency on commons-collections:commons-collections:3.2.2
which in its turn clashes with your org.apache.commons:commons-collections4:4.2
So you're suffering from a form of a Jar Hell. You cannot reliably use 2 different versions of a same JAR in one application as the order of loaded classes in the CLASSPATH is a big question mark. Theoretically you can control this using a custom classloader however it might be an overkill for your project.
The easiest would be just removing your implementation("org.apache.commons:commons-collections4:4.2") dependency and re-write the impacted code to use Commons Collections 3 syntax.
Alternatively you will have to reconsider the way you're using JMeter in your application and go for one of the alternative JMeter execution options.

Karate Gatling with gradle build is not working [duplicate]

This question already has answers here:
How to run Karate and Gatling with Gradle build system
(2 answers)
Closed 1 year ago.
I am trying to run gatling with karate in gradle build and getting below error,
/smoketests/SmokeTestRunner.java:19: error: package org.junit.runner does not exist
import org.junit.runner.RunWith;
^
/smoketests/SmokeTestRunner.java:21: error: package com.intuit.karate.junit4 does not exist
import com.intuit.karate.junit4.Karate;
^
/smoketests/SmokeTestRunner.java:30: error: cannot find symbol
#RunWith(Karate.class)
^
symbol: class RunWith
/wskadmin/WskAdminRunner.java:19: error: package org.junit does not exist
import org.junit.Test;
^
/wskadmin/WskAdminRunner.java:20: error: package org.junit.runner does not exist
import org.junit.runner.RunWith;
^
/wskadmin/WskAdminRunner.java:22: error: package com.intuit.karate.junit4 does not exist
import com.intuit.karate.junit4.Karate;
and exception as
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':compileGatlingScala'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)
For more information,
below is my build.gradle file,
plugins {
id "com.github.lkishalmi.gatling" version "0.7.3"
}
apply plugin: 'java'
apply plugin: 'maven'
group = 'karate-gatling'
version = '1.0-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
mavenCentral()
maven { url "http://repo.maven.apache.org/maven2" }
}
sourceSets {
gatling {
scala.srcDirs = ['src/test/java']
}
test {
java
{
srcDir file('src/test/java')
// exclude '**/*.java'
}
resources
{
srcDir file('src/test/java')
// exclude '**/*.java'
}
}
}
dependencies {
compile group: 'com.jcraft', name: 'jsch', version:'0.1.53'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version:'2.10.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version:'2.10.0'
compile group: 'com.intuit.karate', name: 'karate-netty', version:'0.8.0.1'
testCompile group: 'net.masterthought', name: 'cucumber-reporting', version:'3.8.0'
testCompile group: 'com.intuit.karate', name: 'karate-apache', version:'0.8.0.1'
testCompile group: 'com.intuit.karate', name: 'karate-junit4', version:'0.8.0.1'
testCompile group: 'com.intuit.karate', name: 'karate-gatling', version:'0.8.0.1'
gatling 'com.intuit.karate:karate-gatling:0.8.0.1'
gatling 'com.google.code.gson:gson:2.8.0'
gatling 'org.apache.httpcomponents:httpclient:4.3.2'
gatlingCompile 'org.apache.commons:commons-lang3:3.4'
gatlingRuntime 'cglib:cglib-nodep:3.2.0'
}
gatling {
sourceRoot = 'src/test/java'
toolVersion = '2.3.1'
}
to run my simulation class i am using below command,
./gradlew clean gatlingRun-mypackage.LoadTest
Below is my LoadTest.scala file
class LoadTest extends Simulation {
before{
println("Simulation is about to start!")
}
val createActionTest = scenario("smoke").exec(karateFeature("classpath:path/myfeature.feature"))
setUp(createActionTest.inject(rampUsers(5) over (5 seconds))
).maxDuration(1 minutes).assertions(global.responseTime.mean.lt(1100))
after {
println("Simulation is finished!")
}
}
And below is my runner file
import org.junit.runner.RunWith;
import com.intuit.karate.junit4.Karate;
import cucumber.api.CucumberOptions;
#RunWith(Karate.class)
#CucumberOptions(tags = {"~#ignore","~#driver","~#reliability","~#resiliency","~#concurrent","~#wskfunctions"})
public class SmokeTestRunner {
}
Any help on this is really appriciated
You need to add a testCompile dependency on JUnit, such as:
testCompile 'junit:junit:4.12'
gradle karate karate-gatling junit gradle-gatling-plugin

Resources