Migrate maven dependencies to gradle - java

I start with software testing - using Cucumber, Java, gradle.
I try to learn this with the book "The Cucumber for Java Book"
But I try to do I with gradle instead of maven... But now I have some problems...
I stick on page 149. I have to give so dependecies:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
I try to "translate" this to gradle
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile 'io.cucumber:cucumber-java:2.4.0'
testCompile 'io.cucumber:cucumber-junit:2.4.0'
testCompile group: 'info.cukes', name: 'cucumber-picocontainer', version: '1.2.5'
compile group: 'org.eclipse.jetty', name: 'jetty-webapp', version: '9.4.12.v20180830'
}
Is this right?
compile group: 'org.eclipse.jetty', name: 'jetty-webapp', version: '9.4.12.v20180830'
After that I have to run:
mvn exec:java -Dexec.mainClass="nicebank.AtmServer"
But how can I do this with gradle?
I hope someone can help me :)

Your dependency looks good. Just one note: consider using implementation over compile as it improves the performance. Read about compile deprecation here.
You can also put your properties in gradle.properties file and reference them in the build script:
gradle.properties:
jettyVersion=9.4.12.v20180830
build.gradle:
implementation group: 'org.eclipse.jetty', name: 'jetty-webapp', version: jettyVersion
Jetty team also published BOMs: — org.eclipse.jetty:jetty-bom:9.4.12.v20180830 in your case. If you use multiple projects of the same version you can import the BOM and skip the version completely:
dependencies {
implementation 'org.eclipse.jetty:jetty-bom:9.4.12.v20180830'
implementation 'org.eclipse.jetty:jetty-webapp'
implementation 'org.eclipse.jetty:jetty-runner'
}
As for the "exec" task: if you have only one main class in your project, like nicebank.AtmServer, consider using Gradle's Application Plugin:
plugins {
id 'application'
}
mainClassName = 'nicebank.AtmServer'
This way you don't need to create "exec" task manually, you'll get one (run) from the plugin. As a bonus you'll get two "distribution" tasks that will create a ready-for-distribution archive with your app: distZip and distTar.

As I said in my comment, the dependency for jetty-webapp seems OK but you should use implementation instead of compile ( compile has been deprecated, see Java dependency configurations):
implementation group: 'org.eclipse.jetty', name: 'jetty-webapp', version: '9.4.12.v20180830'
or
implementation "org.eclipse.jetty:jetty-webapp:9.4.12.v20180830"
For the equivalent of "maven exec:java" in Gradle , you could use the Gradle JavaExec task type: try to define a task in your build as follows:
task runApp(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'nicebank.AtmServer'
}
(not tested, you migth have to adapt it) , and run it with
gradle runApp
You could alternatively use Gretty plugin to run your webapp (no need to define your own JavaExec task in this case), as documented here and here:
plugins{
// your existing plugins
id "org.gretty" version "2.2.0"
}
You can then run the application with:
gradle appRun

Related

How to resolve the DNS issue with GCP Pub/Sub

Background:
I'm working on creating a messaging library that basically abstracts GCP Pub/Sub. Accordingly, I am creating a fat JAR with relocated packages, and this fat JAR is imported in other projects. The point to isolate the fat JAR's dependencies from those of the importing projects.
When I test this locally from an IDE, the fat JAR works fine. However, when I export a project to a JAR and run it, I get the following error.
I have also checked the logs and found the following which I have found in this issue that these configurations are correct.
Is there something wrong with the address provided here, or should I configure anything on my end?
These are the dependencies I'm fetching for the library via Gradle.
implementation group: 'io.grpc', name: 'grpc-okhttp', version: '1.40.1'
implementation group: 'io.perfmark', name: 'perfmark-impl', version: '0.25.0'
implementation group: 'com.google.cloud', name: 'google-cloud-pubsub', version: '1.114.6'
implementation group: 'io.grpc', name: 'grpc-context', version: '1.42.2'
implementation group: 'org.slf4j', name: 'slf4j-api', version: "${slf4jApiVersion}"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testImplementation "org.junit.jupiter:junit-jupiter:5.8.1"
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
Thanks.

Gradle to use a jar-with-dependencies in compile task

We have a project that make use of 'jrs-rest-java-client', version: '6.3.1'
The site we used to get the jar from has a certificate issue since September. https://jaspersoft.artifactoryonline.com
We then had to get the jar from a different site.
https://jaspersoft.jfrog.io/
The problem is that a dependency require is missing, but if we use the jar that has "-jar-with-dependencies" it is working. I tried by downloading that jar locally and changing the .gradle to use the local version.
What I would prefer is to have the build to fetch that version directly without having to download first.
How do we specify what jar to use?
dependencies {
compile fileTree(dir: 'lib',
includes: [
'ojdbc8.jar',
])
//compile group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1'
compile group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1', USETHISONE: 'jar-with-dependencies'
//compile files("${buildDir}/jrs-rest-java-client-6.3.1-jar-with-dependencies.jar")
}
I have now tried as suggested;
repositories {
mavenCentral()
// to handle broked jasper reports dependencies
maven {
url 'http://jasperreports.sourceforge.net/maven2'
url 'https://jaspersoft.jfrog.io/jaspersoft/third-party-ce-artifacts/'
url "https://jaspersoft.jfrog.io/jaspersoft/jaspersoft-clients-releases"
}
}
dependencies {
implementation project(':common:project-common-properties')
implementation project(':common:project-common-mail')
implementation fileTree(dir: 'lib', includes: [
'ojdbc8.jar'
])
implementation group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1', classifier: 'jar-with-dependencies'
}
I'm still getting errors at build time...
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all files for configuration ':services:notificationService:compileClasspath'.
> Could not find com.jaspersoft.jasperserver:jasperserver-dto:6.3.0.
Required by:
project :services:notificationService > com.jaspersoft:jrs-rest-java-client:6.3.1
That library is not required if the jrs-rest-java-client-6.3.1-jar-with-dependencies.jar is used.
Thanks all,
The solution was, as seen if the video (Thanks!)
adding a new url:
url "https://jaspersoft.jfrog.io/jaspersoft/jrs-ce-releases"
From the jfrog repo, it shows you how to do this:
compile(group: 'com.jaspersoft', name: 'jrs-rest-java-client', version: '6.3.1', classifier: 'jar-with-dependencies')
Add the repo for gradle:
repositories {
jcenter {
name "jaspersoft-releases"
url "https://jaspersoft.jfrog.io/jaspersoft/jaspersoft-clients-releases"
}
}
I'd recommend switching from compile to implementation and using a shorthand to declare the dependency:
implementation "com.jaspersoft:jrs-rest-java-client:6.3.1:jar-with-dependencies"
Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for his life time.
I decided to record a short clip of how I found the appropriate repositories for the artifacts you needed, on jfrog:

How to run tests from a test-jar in gradle?

I have a multi project build in gradle. In project gradle-playground-a I'm creating a test-jar with some junit tests:
plugins {
id 'java'
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
repositories {
mavenCentral()
}
test.enabled = false
configurations {
testArtifacts.extendsFrom testRuntime
}
task testJar(type: Jar) {
classifier "test"
from sourceSets.test.output
}
artifacts {
testArtifacts testJar
}
Now I would like to run the tests of the test-jar within another project gradle-playground-b. Therefore I have:
plugins {
id 'java'
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testRuntimeOnly project (path: ":gradle-playground-a", configuration: 'testArtifacts')
}
repositories {
mavenCentral()
}
However the tests from the test-jar are not run. When I look at gradles debug output I see that the test-jar is included in the classpath for the test execution of gradle-playground-b.
What I'm trying to do is to have an equivalent of mavens surefire depednenciesToScan functionality.
How do I execute the tests from the test-jar when testing gradle-playground-b?
I just use a "button" in netbeans on the first project to activate the second project. That's the only way I really know how to do it because I have done sortof limited projects I am pretty sure you could do this in VB with Visual STudio but since your in Java use netbeans and it's free.

Why intelliJ IDEA dependency scope is "provided" instead of "compile"?

I would like IntelliJ IDEA to have my libraries as "compile" scope instead of "provided" scope. This is a part of my gradle file:
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// Logging
compile 'ch.qos.logback:logback-classic:1.2.1'
compile 'com.getsentry.raven:raven-logback:7.8.2'
// BigQuery
compile 'com.google.api-client:google-api-client:1.20.0'
compile 'com.google.apis:google-api-services-bigquery:v2-rev227-1.20.0'
// Configuration management
compile 'commons-configuration:commons-configuration:1.10'
//Json
compile 'org.json:json:20160810'
//Kafka
compile "org.apache.kafka:kafka-clients:0.10.1.1"
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile 'org.assertj:assertj-core:3.0.0'
testCompile 'org.mockito:mockito-all:1.10.19'
}
task wrapper(type: Wrapper) {
gradleVersion = '3.4'
}
The scope always reverts to "provided" in the dependency tab which is very annoying:
I am running:
IntelliJ IDEA 2016.3.4
Build #IC-163.12024.16, built on January 31, 2017
JRE: 1.8.0_112-release-408-b6 x86_64
It's a known issue in IntelliJ IDEA that is specific to Gradle 3.4:
IDEA-167412 Gradle 3.4-rc-1 changes compile dependencies to provided
original bug report in the Gradle project with more details
comment from the responsible developer regarding "Create Module per source set" option and how Gradle integration works in IntelliJ IDEA
It's already fixed in 2017.1 EAP build.
You can use Gradle 3.3 or older as a workaround until IDEA 2017.1 is released.

gradle unable to find artifact in maven repository

I am doing a simple gradle build and I have deployed a particular artifact to my internal sonatype maven repo
My build.gradle file looks like this:
apply plugin: 'java'
sourceCompatibility = 1.8
version = '1.0'
repositories {
mavenCentral()
maven {url "http://maven.ebay.com/nexus/content/repositories/sre-snapshots/"}
}
dependencies {
compile group: "com.typesafe", name: "config", version: "1.3.0"
//compile project(':metrics')
compile group: 'com.ebay.telemetry', name: 'client-library', version: '0.1-SNAPSHOT'
compile group: "org.slf4j", name: "slf4j-jdk14", version: "1.7.12"
testCompile group: 'junit', name: 'junit', version: '4.11'
}
I run this command on the command line: gradle build
I get this error:
> Could not find net.alchim31:metrics-influxdb:0.7.1-ebay-SNAPSHOT.
Searched in the following locations:
https://repo1.maven.org/maven2/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/maven-metadata.xml
http://maven.ebay.com/nexus/content/repositories/sre-snapshots/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/maven-metadata.xml
http://maven.ebay.com/nexus/content/repositories/sre-snapshots/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/metrics-influxdb-0.7.1-ebay-20150708.054833-4.pom
http://maven.ebay.com/nexus/content/repositories/sre-snapshots/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/metrics-influxdb-0.7.1-ebay-20150708.054833-4.jar
However when I go to http://maven.ebay.com/nexus/content/repositories/sre-snapshots/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/
I see the following files :
metrics-influxdb-0.7.1-ebay-20150708.054830-3.pom
metrics-influxdb-0.7.1-ebay-20150708.054830-3.jar
So why is gradle looking for metrics-influxdb-0.7.1-ebay-20150708.054833-4.jar when my repo has metrics-influxdb-0.7.1-ebay-20150708.054830-3.jar
It seems like you have declared another dependency besides the one you showed above. It should probably look something like this:
compile group: 'net.alchim31', name: 'metrics-influxdb', version: '0.7.1-ebay-SNAPSHOT'
If you find this dependency declaration, you might also find the source of the problem.
The second possibility is, that your maven-metadata.xml (at http://maven.ebay.com/nexus/content/repositories/sre-snapshots/net/alchim31/metrics-influxdb/0.7.1-ebay-SNAPSHOT/maven-metadata.xml) is messed up and points towards the nonexisting build 4.

Categories