简体   繁体   English

将 maven 依赖迁移到 gradle

[英]Migrate maven dependencies to gradle

I start with software testing - using Cucumber, Java, gradle.我从软件测试开始——使用 Cucumber、Java、gradle。 I try to learn this with the book "The Cucumber for Java 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:但是我尝试用 gradle 而不是 maven 来做我......但现在我有一些问题......我坚持第 149 页。我必须给出这样的依赖:

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>

I try to "translate" this to gradle我尝试将其“翻译”为 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'编译组:'org.eclipse.jetty',名称:'jetty-webapp',版本:'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?但是我怎么能用 gradle 做到这一点呢?

I hope someone can help me :)我希望有一个人可以帮助我 :)

Your dependency looks good.你的依赖看起来不错。 Just one note: consider using implementation over compile as it improves the performance.只是一个注意事项:考虑使用implementation不是compile因为它可以提高性能。 Read about compile deprecation here . 在此处阅读有关compile弃用的信息

You can also put your properties in gradle.properties file and reference them in the build script:您还可以将您的属性放在gradle.properties文件中并在构建脚本中引用它们:

gradle.properties : gradle.properties

jettyVersion=9.4.12.v20180830

build.gradle :构建.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. Jetty 团队还发布了BOM : — org.eclipse.jetty:jetty-bom:9.4.12.v20180830在你的情况下。 If you use multiple projects of the same version you can import the BOM and skip the version completely:如果您使用相同版本的多个项目,您可以导入 BOM并完全跳过版本:

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 :至于“exec”任务:如果您的项目中只有一个主类,例如nicebank.AtmServer ,请考虑使用 Gradle 的应用程序插件

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.这样你就不需要手动创建“exec”任务,你将从插件中获得一个( run )。 As a bonus you'll get two "distribution" tasks that will create a ready-for-distribution archive with your app: distZip and distTar .作为奖励,您将获得两个“分发”任务,这些任务将使用您的应用程序创建一个可供分发的存档: distZipdistTar

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 ):正如我在评论中所说的,jetty-webapp 的依赖似乎没问题,但您应该使用implementation而不是compilecompile已被弃用,请参阅Java 依赖配置):

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:对于 Gradle 中的“maven exec:java”等价物,您可以使用Gradle JavaExec 任务类型:尝试在您的构建中定义一个任务,如下所示:

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 :您也可以使用Gretty插件来运行您的 web 应用程序(在这种情况下无需定义您自己的 JavaExec 任务),如此此处所述

plugins{
    // your existing plugins   
    id "org.gretty" version "2.2.0"
}

You can then run the application with:然后,您可以使用以下命令运行应用程序:

gradle appRun

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM