JUnit Listener Configuration In Gradle - gradle

I'm new bee to Gradle. Have custom JUnit Listener, which reads the custom annotation data and generates report and need to configure it as part of Gradle. Is there anyway to configure below surefire plugin in Gradle 4.4.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<properties>
<property>
<name>listener</name>
<value>my.company.MyRunListener</value>
</property>
</properties>
</configuration>
</plugin>
I understand that, it may not possible to use maven plugin as is in gradle. I checked TestListener, it doesn't have support to read annotations to proceed with that.
I would like to understand the way to configure my JUnit Listener in Gradle.

The JUnit Foundation library enables you to declare your listeners in a service provider configuration file, which are then attached automatically - regardless of execution environment. Details can be found here.
Gradle Configuration for JUnit Foundation
// build.gradle
...
apply plugin: 'maven'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
...
}
dependencies {
...
compile 'com.nordstrom.tools:junit-foundation:12.2.0'
}
ext {
junitFoundation = configurations.compile.resolvedConfiguration.resolvedArtifacts.find { it.name == 'junit-foundation' }
}
test.doFirst {
jvmArgs "-javaagent:${junitFoundation.file}"
}
test {
// debug true
// not required, but definitely useful
testLogging.showStandardStreams = true
}
ServiceLoader provider configuration file
# src/main/resources/META-INF/services/com.nordstrom.automation.junit.JUnitWatcher
com.example.MyRunListener
With this configuration, the listener implemented by MyRunListener will be automatically attached to the RunNotifier supplied to the run() method of JUnit runners. This feature eliminates behavioral differences between the various test execution environments like Maven, Gradle, and native IDE test runners.

I’m afraid, there is currently no support for JUnit RunListeners in Gradle. There is only an open ticket requesting that feature: https://github.com/gradle/gradle/issues/1330
As someone has mentioned in the comments on that ticket, “the primary issue […] is the absence of TestDescriptor.getAnnotations()” in Gradle; otherwise you might have been able to rewrite your RunListener as a Gradle TestListener. So unless I’ve missed something when skimming through the ticket, it seems that you are mostly out of luck at the moment :-(

Related

Add custom codegen implementation for openapi-generator gradle plugin

I implement my custom code generation for https://github.com/OpenAPITools/openapi-generator
but i have no idea how to add this to gradle plugin. I need to add it to classpath while gradle perform openapi tasks
For maven i can easily add my custom implementation com.my.generator:customgenerator:1.0-SNAPSHOT in plugin dependency block,
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin-version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<templateDirectory>myTemplateDir</templateDirectory>
<apiPackage>${default.package}.handler</apiPackage>
<modelPackage>${default.package}.model</modelPackage>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.my.generator</groupId>
<artifactId>customgenerator</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
but in gradle i have no idea how to do it
The solution is simple if you know how Gradle plugins work. Here are steps how to do it:
You need to add your custom generator class to the classpath of the plugin. But, you can not use there any module of the Gradle project, in which you want to use the generator plugin, because Gradle plugins are applied before the whole compilation of the project and also before dependencies are resolved. So, you must use the already compiled jar file. For example, create a new Gradle project where you place custom generator code and publish it to maven local repository (How to publish source into local maven repository with Gradle?). Then you can add it to plugins classpath like this:
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.openapitools:openapi-generator:4.3.0"
classpath "some.custom.openapi:generator:0.0.1"
}
}
Openapi generator use Java service loader to load generators (https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html). So, in your custom generator project create file org.openapitools.codegen.CodegenConfig with content
some.custom.openapi.CustomJavaCodegen
(Here must be the name of the custom generator class) and place it to folder src/main/resources/META-INF/services/.
In your custom generator class override method getName with your generator name, which you will use in the configuration of openApiGenerator in the Gradle file.
I get this working with these steps. If I forget something to write it here, comment, and I will try to fill missing information.

Gradle Build and jUnit RunListener [duplicate]

I'm new bee to Gradle. Have custom JUnit Listener, which reads the custom annotation data and generates report and need to configure it as part of Gradle. Is there anyway to configure below surefire plugin in Gradle 4.4.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<properties>
<property>
<name>listener</name>
<value>my.company.MyRunListener</value>
</property>
</properties>
</configuration>
</plugin>
I understand that, it may not possible to use maven plugin as is in gradle. I checked TestListener, it doesn't have support to read annotations to proceed with that.
I would like to understand the way to configure my JUnit Listener in Gradle.
The JUnit Foundation library enables you to declare your listeners in a service provider configuration file, which are then attached automatically - regardless of execution environment. Details can be found here.
Gradle Configuration for JUnit Foundation
// build.gradle
...
apply plugin: 'maven'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
...
}
dependencies {
...
compile 'com.nordstrom.tools:junit-foundation:12.2.0'
}
ext {
junitFoundation = configurations.compile.resolvedConfiguration.resolvedArtifacts.find { it.name == 'junit-foundation' }
}
test.doFirst {
jvmArgs "-javaagent:${junitFoundation.file}"
}
test {
// debug true
// not required, but definitely useful
testLogging.showStandardStreams = true
}
ServiceLoader provider configuration file
# src/main/resources/META-INF/services/com.nordstrom.automation.junit.JUnitWatcher
com.example.MyRunListener
With this configuration, the listener implemented by MyRunListener will be automatically attached to the RunNotifier supplied to the run() method of JUnit runners. This feature eliminates behavioral differences between the various test execution environments like Maven, Gradle, and native IDE test runners.
I’m afraid, there is currently no support for JUnit RunListeners in Gradle. There is only an open ticket requesting that feature: https://github.com/gradle/gradle/issues/1330
As someone has mentioned in the comments on that ticket, “the primary issue […] is the absence of TestDescriptor.getAnnotations()” in Gradle; otherwise you might have been able to rewrite your RunListener as a Gradle TestListener. So unless I’ve missed something when skimming through the ticket, it seems that you are mostly out of luck at the moment :-(

Maven configuration in gradle

I would like to specify the below maven plugin in gradle. How to specify the configuration in gradle?
<build>
<plugins>
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>${swagger-maven-plugin-version}</version>
<configuration>
<apiSources>
<apiSource>
test
</apiSource>
</apiSources>
</configuration>
</plugin>
</plugins>
In general, it's impossible to use Maven plugin 'as is' in Gradle. Maven plugins internally depend on some classes that exist only in Maven universe.
So, the best approach would be porting the plugin to Gradle.
In your particular case there is a Gradle port of Swagger Maven plugin. I haven't used it by myself, but quick googling reveals this:
Gradle Swagger plugin
I aggree with Mark Bramnik answer but you can try what can work by comparing documentation of both repositories : https://github.com/dave-ellis/gradle-swagger-plugin and https://github.com/kongchen/swagger-maven-plugin. All name identified in table called "Configuration for apiSource" of the former one could be a variable inside swagger block for gradle.
buildscript {
repositories {
mavenLocal()
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
classpath group: 'com.github.gradle-swagger', name: 'gradle-swagger-plugin', version: '1.0.1-SNAPSHOT'
}
}
apply plugin: 'maven'
apply plugin: 'swagger'
apply plugin: 'java'
swagger {
endPoints = [
'com.foo.bar.apis',
'com.foo.bar.apis.internal.Resource'
]
apiVersion = 'v1'
basePath = 'http://www.example.com'
mustacheFileRoot = "${projectDir}/src/main/resources/"
outputTemplate = "${mustacheFileRoot}/strapdown.html.mustache"
swaggerDirectory = "${buildDir}/site/api-docs"
outputPath = "${buildDir}/site/swagger/strapdown.html"
}

Sonar not showing code coverage after jenkin's build

We have a multi module maven project. We are using jacoco for code coverage analysis. I prepared the pom file to run the test cases and pick up the code coverage. When i build the project on my local i can see the code coverage on the sonar dashboard.
After pushing the changes to the github and when jenkins build got triggered the code coverage is not available on the sonar dashboard. I found the following error in the jenkins build logs:
Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?
How can I make the code coverage available after the successful jenkins build?
You have to make sure that while compiling Java or Groovy etc code, the debug option is turned on. I use Gradle (instead of Maven) and to enable debug option, I do something like this in Gradle. You can put the following code in project's build.gradle OR top level $GRADLE_HOME/init.d/some-global.gradle file (inside allprojects { ... } section).
tasks.withType(JavaCompile) {
options.debug = true
options.compilerArgs = ["-g"]
}
tasks.withType(GroovyCompile) {
options.debug = true
}
Do the same in Maven (how to turn debug option on using -g for Java and true for Groovy based projects).
After this, all you need is make sure your sonar.xx.yyy variables are set correctly while calling sonar runner (i.e. sonarRunner task in Gradle).
I set those variables as (UT for Unit tests, IT for Integration Tests). IT code coverage will be generated only if you attach jacocoagent.jar to the Tomcat/external JVM (instead of what Gradle/Maven JVM is) which runs your application's .war/.ear file.
-Dsonar.jacoco.itReportPath=build/jacoco/IT/jacocoIT.exec
-Dsonar.jacoco.reportPath=build/jacoco/UT/jacocoUT.exec
There are other sonar.xx.yy variables you can see online/docs.
In Maven, you can turn Java debug on using this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<debug>true</debug>
<debuglevel>lines,vars,source</debuglevel>
</configuration>
</plugin>

Add test jars in maven-jetty-plugin or create test-war with maven-war-plugin

I'm using maven to build a multi-module webapp. I would like to run my integration tests in their own module and use the jetty plugin. In order to get everything to work I will need to add a couple of jars to the classpath for the war but I see no option for such a thing in the documentation http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html#deploy-war-running-pre-assembled-war
I am able to deploy the war but it fails because it's missing the two jars I need to add.
Is there a way for me to add a couple extra jars to the plugin configuration?
If not, is there a way for me to package a "test-war" like you can do with test-jar in maven?
There are multiple ways to extend the web application classpath with the jetty-maven-plugin. The most appropriated for you would be to set the extraClasspath field in the webAppConfig block of your plugin configuration:
<configuration>
...
<webAppConfig>
...
<extraClasspath>path/to/your/custom-dependency.jar</extraClasspath>
</webAppConfig>
</configuration>
The documentation is not very consistent about that. But the javadoc is quite clear.
You can find relevant configuration examples on my jetty plugin wiki page.
Add the dependencies directly to the plugin's <dependencies/>. No need for scopes or anything -- they'll not enter your final artifact, but rather -- only be used by the Jetty plugin during execution.

Resources