Deviasa
Deviasa

Reputation: 153

Migrate maven dependencies to gradle

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 :)

Upvotes: 2

Views: 342

Answers (2)

M.Ricciuti
M.Ricciuti

Reputation: 12086

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

Upvotes: 0

madhead
madhead

Reputation: 33384

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.

Upvotes: 2

Related Questions