Gradle – How to run a single unit test class

In Gradle, we can pass an –tests option to run a single unit test class. Read this Gradle Test Filtering. Terminal gradle test –test TestClass P.S Tested with Gradle 6.7.1 1. Run a single test class Review a simple unit test. DummyTest.java package com.mkyong.security.db; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class DummyTest { @Test void …

Read more

JUnit 5 + Gradle examples

This article shows you how to add JUnit 5 in a Gradle project. Technologies used: Gradle 5.4.1 Java 8 JUnit 5.5.2 1. Gradle + JUnit 5 1. Add the JUni 5 jupiter engine, and define the useJUnitPlatform() like the following: gradle.build plugins { id ‘java’ id ‘eclipse’ // optional, for Eclipse project id ‘idea’ // …

Read more

Gradle – Could not find method compileOnly

Git clone a new project, Gradle build and hits the following error message : Terminal $ gradle clean build FAILURE: Build failed with an exception. * Where: Build file ‘/Users/mkyong/Documents/workspace/hc2/web/build.gradle’ line: 25 * What went wrong: A problem occurred evaluating project ‘:web’. > Could not find method compileOnly() for arguments [org.springframework.boot:spring-boot-starter-tomcat] on project ‘:web’. * …

Read more

java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonMerge

Run a Jackson related project and hits the following JsonMerge not found error. Console java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonMerge at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector.<clinit>(JacksonAnnotationIntrospector.java:50) ~[jackson-databind-2.9.0.pr1.jar:2.9.0.pr1] at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:292) ~[jackson-databind-2.9.0.pr1.jar:2.9.0.pr1] at com.hostingcompass.core.utils.PrintUtils.<clinit>(PrintUtils.java:9) ~[main/:na] at com.hostingcompass.app.run.TurtleApp.run(TurtleApp.java:25) ~[main/:na] at com.hostingcompass.app.Main.run(Main.java:42) [main/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at com.hostingcompass.app.Main.main(Main.java:34) [main/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) …

Read more

Gradle is not sync with IntelliJ IDEA

Make some changes in the build.gradle file, but the changes never apply or sync to the IDEA (Tested with 2016.3) 1. Using IDEA plugin build.gradle apply plugin: ‘java’ apply plugin: ‘idea’ apply plugin: ‘org.springframework.boot’ 2. Make changes and issue the Gradle clean and idea command. Console $ gracle clean idea $ gradle cleanIdea idea $ …

Read more

Gradle – Multiple start script examples

Few build.gradle examples to show you how to create multiple start scripts or executable Java application. 1. Single Start Script 1.1 In Gradle, you can use the application plugin to create an executable Java application : build.gradle apply plugin: ‘application’ mainClassName = "com.mkyong.analyzer.run.threads.MainRunApp" applicationName = ‘mainApp’ applicationDefaultJvmArgs = ["-Xms512m", "-Xmx1024m"] 1.2 Create the executable Java …

Read more

Gradle – How to continue build if test is failed

By default, the Gradle build process will be stopped and failed if any unit test is failed. $ gradle build :clean :compileJava :processResources :classes :compileTestJava :processTestResources UP-TO-DATE :testClasses :test com.mkyong.example.TestExample > test_example FAILED java.lang.Exception at TestExample.java:9 //… 3 tests completed, 1 failed :test FAILED //… BUILD FAILED // <————– see status In this article, we …

Read more

Gradle – How to display test result in the console

By default, gradle test displays only the test summary. Terminal % gradle test BUILD SUCCESSFUL in 4s 6 actionable tasks: 3 executed, 3 up-to-date P.S Gradle test generates the detailed tests’ result at the build/reports/tests/test/index.html page. P.S Tested with Gradle 6.7.1 1. Display test result in the console. 1.1 We add the testLogging events to …

Read more

Gradle – Exclude commons-logging from Spring

Gradle example to exclude commons-logging from Spring frameworks. build.gradle compile(‘org.springframework:spring-webmvc:4.2.4.RELEASE’){ exclude group: ‘commons-logging’, module: ‘commons-logging’ } If you have multiple Spring dependencies, you have to exclude for each build.gradle compile(‘org.springframework:spring-webmvc:4.2.4.RELEASE’){ exclude group: ‘commons-logging’, module: ‘commons-logging’ } compile(‘org.springframework:spring-test:4.2.4.RELEASE’){ exclude group: ‘commons-logging’, module: ‘commons-logging’ } A better solution is excluding commons-logging at the project level. build.gradle configurations.all …

Read more

Jenkins – Could not find GradleWrapperMain

Create a Gradle project in Jenkins CI, source management with Git, and build with Gradle Wrapper 1. Problem Jenkins console output, the build is failed – “Could not find org.gradle.wrapper.GradleWrapperMain” [Gradle] – Launching build. [workspace] $ /var/lib/jenkins/jobs/GradleTest/workspace/gradlew clean test Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain Build step ‘Invoke Gradle script’ changed build …

Read more

How to use Gradle Wrapper

In this tutorial, we will show you how to create Gradle wrapper for your project and how to use it. What is Gradle wrapper? The Gradle wrapper allows you to run a Gradle task without requiring that Gradle is installed on your system. 1. Create a Gradle Wrapper 1.1 Declares a wrapper task. build.gradle task …

Read more

Gradle JaCoCo – Incompatible version 1006

A Gradle build + JaCoCo coverage report. build.gradle apply plugin: ‘java’ apply plugin: ‘eclipse-wtp’ apply plugin: ‘jacoco’ //code coverage repositories { mavenLocal() mavenCentral() } jacoco { toolVersion = "0.7.5+" } jacocoTestReport { reports { html.enabled = true xml.enabled = true csv.enabled = false } } P.S Tested with Gradle 2.10 1. Problem 1.1 Run the …

Read more

Gradle – Display project dependency

In this tutorial, we will show you how to display project dependencies with the Gradle build tool. Review a simple Spring MVC project dependencies. build.gradle dependencies { compile ‘org.slf4j:jcl-over-slf4j:1.7.12’ compile ‘ch.qos.logback:logback-classic:1.1.3’ compile(‘org.springframework:spring-webmvc:4.1.6.RELEASE’){ exclude group: ‘commons-logging’, module: ‘commons-logging’ } compile ‘org.hsqldb:hsqldb:2.3.2’ providedCompile ‘javax.servlet:servlet-api:2.5’ } P.S Tested with Gradle 2.4 1. gradle dependencies Display project dependencies (direct …

Read more

Gradle – Create Java project structure automatically

To quick start a new Gradle Java project, type gradle init –type java-library $ gradle init –type java-library :wrapper :init BUILD SUCCESSFUL Total time: 4.866 secs The following files and folders will be created automatically. P.S Tested with Gradle 2.0 1. Java Project Structure Both src/main/java and src/test/java folders are created. The Library*.java is a …

Read more

Gradle Application Plugin – APP_HOME in applicationDefaultJvmArgs

In Gardle, the application plugin, you can pass the system properties via applicationDefaultJvmArgs : gradle.build apply plugin:’application’ mainClassName = "com.mkyong.analyzer.engine.hydra.entryPointForJar" applicationName = ‘analyzer’ distZip { archiveName ‘analyzer-‘ + version + ‘.zip’ } applicationDefaultJvmArgs = ["-Dlogback.configurationFile=logback.xml"] The problem is how to get the APP_HOME for logback.xml? gradle.build applicationDefaultJvmArgs = ["-Dlogback.configurationFile=APP_HOME/logback.xml"] You can hard code the APP_HOME, …

Read more

Gradle War Plugin – Change output WAR filename

In Gradle, the WAR plugin will generate the final WAR file with the following pattern: ${baseName}-${appendix}-${version}-${classifier}.${extension} Example : hello.1.0.war To change the default WAR filename, update war.archiveName. For example : build.gradle project(‘:web’) { apply plugin: ‘war’ war { archiveName ‘hello-gradle.war’ } dependencies { compile project(‘:core’) providedCompile ‘javax.servlet:servlet-api:2.5’ providedCompile ‘org.apache.tomcat:tomcat-jsp-api:7.0.55’ //… } } Build it… $ …

Read more

Gradle – How to skip unit test

Be default, Gradle build is abort if any unit tests is failed. Oftentimes, we still need to build the project even the unit test is failed. To skip the entire unit tests in Gradle build, uses this option-x test gradle build -x test Review a sample output : 1. Default Gradle build : $ gradle …

Read more

Gradle – Create a Jar file with dependencies

In this tutorial, we will show you how to use Gradle build tool to create a single Jar file with dependencies. Tools used : Gradle 2.0 JDK 1.7 Logback 1.1.2 1. Project Directory Create following project folder structure : By default, Gradle is using the standard Maven project structure. ${Project}/src/main/java/ ${Project}/src/main/resources/ ${Project}/src/test/java/ 2. Java Files …

Read more

Gradle – bootstrap class path not set in conjunction with -source 1.5

My environment : JDK 1.7 Eclipse 4.4 Gradle 2.0 While gradle builld the project, I get following compile warning message : :compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.5 1 warning Figure : Eclipse console view. Solution The warning is saying you are using JDK 1.7, but try to compile the …

Read more

Gradle : Add Eclipse project nature

Eclipse project natures are configured in the .project file. For example : .project <?xml version="1.0" encoding="UTF-8"?> <projectDescription> <name>hello</name> <comment></comment> <projects> </projects> <buildSpec> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand> </buildSpec> <natures> <nature>org.springsource.ide.eclipse.gradle.core.nature</nature> <nature>org.eclipse.jdt.core.javanature</nature> </natures> </projectDescription> To add a nature, just modify the nature tag, and add whatever nature you want. In Gradle, you can add the Eclipse …

Read more