read excel file from build.gradle - gradle

i am new to gradle.Can someone please help me out to find out a way in which we can read some values from an excel file in build.gradle script. these values will be used to decide the testng test suites thatneed to be run dynamically

use imports to add poi jars and use them as you do in normal java program
remmber to import packges at the first very first section(top) of build.gradle file
buildscript {
repositories { mavenCentral() }
dependencies {
// classpath group: 'org.apache.commons', name: 'commons-csv', version: '1.0'
classpath group: 'org.apache.poi', name: 'poi', version: '3.13'
classpath group: 'org.apache.poi', name: 'poi-ooxml', version: '3.13'
}

Related

Build jar with binaries for required platform only (javacpp)

I want to build a fat jar for tesseract. With the following build settings I get a jar of about 68 MB with dependencies for all supported platforms:
dependencies {
implementation group: 'org.bytedeco', name: 'tesseract-platform', version: '4.1.1-1.5.3'
}
jar {
manifest { attributes 'Main-Class': 'BasicExample' }
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}
In order to reduce this size, I tried to only include the dependencies for my platform following this guideline - but without success, see also this SO question and #Samuel Audet's answer to it (I want to stick with gradle). Therefore I decided to manually include only the required dependencies from the POM file:
dependencies {
implementation group: 'org.bytedeco', name: 'tesseract', version: '4.1.1-1.5.3'
implementation group: 'org.bytedeco', name: 'tesseract', version: '4.1.1-1.5.3', classifier: 'windows-x86_64'
implementation group: 'org.bytedeco', name: 'leptonica', version: '1.79.0-1.5.3', classifier: 'windows-x86_64'
}
This reduces the size of the jar to about 7 MB and works fine (at least for the basic example). Nevertheless I get warnings:
Warning: Could not load ...: java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path: ...
Comparing the two jars I found that the small jar lacks the lib path with all the so libs along with header, cmake and pkgconfig files and I assume that this is the reason for the warnings.
So my questions are:
If these files are apparently not necessary to run the jar, why are they included normally?
How can I prevent these warnings while keeping the jar size small?
Any other way to build a jar with just the required dependencies for one platform is of course also welcomed.
I'll have to update that guide a little bit, thanks for reporting! We now also need javacpp-platform, which would give us something like this for windows-x86_64 in this case:
dependencies {
implementation group: 'org.bytedeco', name: 'tesseract', version: '4.1.1-1.5.3'
implementation group: 'org.bytedeco', name: 'tesseract', version: '4.1.1-1.5.3', classifier: 'windows-x86_64'
implementation group: 'org.bytedeco', name: 'leptonica', version: '1.79.0-1.5.3', classifier: 'windows-x86_64'
implementation group: 'org.bytedeco', name: 'javacpp', version: '1.5.3', classifier: 'windows-x86_64'
}
This is needed to fix some issues at load time as explained in this issue:
https://github.com/bytedeco/javacv/issues/1305
UPDATE: It's now possible to use Gradle JavaCPP to do this more easily this way:
plugins {
id 'java-library'
id 'org.bytedeco.gradle-javacpp-platform' version "$javacppVersion"
}
// We can set this on the command line too this way: -PjavacppPlatform=linux-x86_64,macosx-x86_64,windows-x86_64,etc
ext {
javacppPlatform = 'linux-x86_64,macosx-x86_64,windows-x86_64,etc' // defaults to Loader.getPlatform()
}
dependencies {
api "org.bytedeco:tesseract-platform:$tesseractVersion"
}

Gradle 4.x API instead of implementation - How can i use gradle API feature to expose internal dependencies

I'm using gradle 4.x and here is my problem.
I've two modules A and B.
Module B has project dependency on module A. Here are the gradle files.
build.gradle for module A
apply plugin: 'java-library'
dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-starter'
implementation group: 'org.springframework.kafka', name: 'spring-kafka' , version: '2.2.7.RELEASE'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
testImplementation group: 'io.github.benas', name: 'random-beans', version: '3.7.0'
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
Here is the build.gradle for module B
dependencies {
implementation project(':moduleA')
implementation group: 'org.springframework.boot', name: 'spring-boot-starter'
implementation group: 'org.springframework.kafka', name: 'spring-kafka' , version: '2.2.7.RELEASE'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
testImplementation group: 'io.github.benas', name: 'random-beans', version: '3.7.0' }
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
If you observe the build.gradle file for module B, i've a dependency for spring-kafka but it's a duplicate since it's already in module A build.gradle.
Now how can use gradle API option to expose spring-kafka (from module A) without actually mentioning it in module B's build.gradle when the jar file of module B is being consumed by another app?
Please suggest.
This is explained in the docs here (emphasis mine):
The plugin exposes two configurations that can be used to declare dependencies: api and implementation. The api configuration should be used to declare dependencies which are exported by the library API, whereas the implementation configuration should be used to declare dependencies which are internal to the component.
So simply change implementation to api in moduleA:
api group: 'org.springframework.kafka', name: 'spring-kafka' , version: '2.2.7.RELEASE'
Then remove the entry from moduleB's Gradle file.

How to Package a JavaFX Application as a standalone executable EXE using Gradle

I recently downloaded the JavaFX 12 SDK and started a Gradle Application in IntelliJ IDE.I have also imported all the necessary dependencies in the build.gradle file. Now I want to package the final JavaFX application as a standalone executable exe file that can be clicked and run on Windows. How can I do this since I started the project as a Gradle Project. The Build Artifact Menu Item on IntelliJ seems to be permanently disabled.
Here is a setup of my build.gradle file
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.7'
}
javafx {
version = "12.0.1"
modules = ['javafx.base', 'javafx.controls', 'javafx.web']
}
group 'condo'
version '1'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
mainClassName = 'rumbler.launcher.ApplicationLoader'
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation group: 'org.iq80.leveldb', name: 'leveldb', version: '0.12'
implementation 'org.controlsfx:controlsfx:11.0.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation group: 'org.json', name: 'json', version: '20180813'
}
Thanks alot.

How to run custom task within gradle build?

I have a Spring Boot web application which uses Gradle and NPM. I created a package.json file and npm install works fine. But I'd like to be able to run npm install automatically when I run gradle build. Here's my build file:
plugins {
id 'org.springframework.boot' version '1.5.10.RELEASE'
id 'java'
id 'war'
id "org.ysb33r.nodejs.base" version "0.1"
id "org.ysb33r.nodejs.npm" version "0.1"
}
group 'com.Blahblah'
version '1.0-SNAPSHOT'
war {
baseName = 'Blahblah'
version = '0.0.1'
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
maven { url "http://repo.spring.io/libs-snapshot" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.3'
compile group: 'com.cloudinary', name: 'cloudinary-core', version: '1.17.0'
compile group: 'com.cloudinary', name: 'cloudinary-http44', version: '1.17.0'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.5.10.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jersey', version: '1.5.10.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '1.5.10.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.10.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: '1.5.10.RELEASE'
classpath "gradle.plugin.org.ysb33r.gradle:nodejs-gradle-plugin:0.1"
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.10.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
apply plugin: "org.ysb33r.nodejs.base"
apply plugin: "org.ysb33r.nodejs.npm"
I added the ysb33r parts in plugins, dependencies and apply plugin sections, but now I get an error at the line:
classpath "gradle.plugin.org.ysb33r.gradle:nodejs-gradle-plugin:0.1"
It says:
A problem occurred evaluating root project 'Blahblah'.
> Could not find method classpath() for arguments [gradle.plugin.org.ysb33r.gradle:nodejs-gradle-plugin:0.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Before I added the ysb33r lines, the build worked fine. What could be the problem? And when the problem is solved, how should I proceed in order to run npm install automatically?
Thanks.
A couple of issues:
Exec.commandLine is a String array
Your println is happening in the configuration phase, put it in a doLast block so it executes in the execution phase
Eg:
task npmInstall(type: Exec) {
commandLine 'npm install'.split(' ')
doLast {
println 'DONE!'
}
}
Rather than calling npm via Exec task, you should consider one of the gradle/nodejs plugins. Most of these are able to use an embedded nodejs version, downloaded from a repository, to execute nodejs commands rather than require a nodejs installation on the machine
See: https://plugins.gradle.org/search?term=nodejs
Eg: http://ysb33rorg.gitlab.io/nodejs-gradle-plugin/

Unable to pass argument from gradle command line to Spring project (not Boot)

I am developing Spring project.
I would like to load credentials from the command line not storying them in the code. I'm trying to execute this gradle command
gradlew build -Dspring.datasource.username=tester
and when I startup the Spring project, the program stops on a breakpoint and I see whether variable is declared or not. I have tried using -P instead of -D but it still didn't help.
I deploy the spring app remotely using bmuschko plugin I've tried to use, but also without success. I checked in java code Properties by using System.getProperties() and Environment object supported by Spring.
gradlew cargoredeployremote -Dspring.datasource.username=tester
Application properties are loaded succesfully.
IMPORTANT: I saw many tutorials how to make it but using Spring Boot I use just selected components from Spring.
For instance: http://nixmash.com/post/passing-arguments-to-spring-boot - this doesn't work in my case because I have no bootRun task.
Any ideas? Am I missing something in my steps?
Here is my build.gradle
group 'example'
version '1.0.0'
apply plugin: 'application'
apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'com.bmuschko.cargo'
apply plugin: 'org.liquibase.gradle'
compileJava.options.encoding = 'UTF-8'
mainClassName = 'api.optic.config.WebAppInitializer'
sourceCompatibility = 1.8
buildscript {
repositories{
jcenter()
mavenCentral()
}
dependencies{
classpath 'com.bmuschko:gradle-cargo-plugin:2.2.3'
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3'
classpath 'org.liquibase:liquibase-core:3.4.1'
classpath "org.liquibase:liquibase-gradle-plugin:1.2.4"
classpath "mysql:mysql-connector-java:5.1.13"
}
}
project.ext {
springVersion = "4.3.6.RELEASE"
junitVersion = "5.0.0-RC3"
}
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework:spring-core:${springVersion}"
compile "org.springframework:spring-context:${springVersion}"
compile "org.springframework:spring-context-support:${springVersion}"
compile "org.springframework:spring-beans:${springVersion}"
compile "org.springframework:spring-web:${springVersion}"
compile "org.springframework:spring-webmvc:${springVersion}"
compile "org.springframework:spring-orm:${springVersion}"
compile "org.springframework:spring-oxm:${springVersion}"
compile "org.springframework:spring-jdbc:${springVersion}"
compile "org.springframework:spring-test:${springVersion}"
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.38'
compile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.6'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.2'
compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-parameter-names', version: '2.9.0.pr2'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jdk8', version: '2.9.0.pr2'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.0.pr2'
compile 'javax.servlet:javax.servlet-api:3.1.0'
testCompile "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}
cargo {
containerId = 'tomcat8x'
port = 8080
deployable {
context = 'example'
}
remote {
hostname = 'host.name.com'
username = 'tomcat'
password = 'pass'
}
Basically were 2 issues
1. Name mismatch: in application.properties the name inside the ${} was different than this provided from command-line
Solution:
application.properties
spring.datasource.username=${username}
and in gradle command-line
gradle build -Pusername=tester
2. Dot issue with gradle:
Can't put
gradle build -Pspring.datasource.username=tester
even if you have in application.properties
spring.datasource.username=${spring.datasource.username}
because you get an exception:
Execution failed for task ':processResources'.
Could not copy file '.\src\main\resources\application.properties' to '.\build\resources\main\application.properties'.
Solution:
Instead of dots use _ sign
gradle build -Pspring_datasource_username=tester
and in Application properties
spring.datasource.username=${spring_datasource_username}

Resources