maven - 将 Spring 依赖项添加到 gradle 任务中

标签 maven gradle groovy

我想在我的机器上添加一个特殊的 gradle 任务作为 init 脚本。该脚本位于 ~/.gradle/init.d 中。我们将其命名为 servertest.gradle。它看起来像这样:

import org.springframework.http.MediaType

allprojects {
    task servertest {
        doLast {
            MediaType.parseMediaType("application/json")
        }
    }
}

我可以运行该任务,但它说:

> startup failed:
  initialization script '/home/user1/.gradle/init.d/servertest.gradle': 1: unable to resolve class org.springframework.http.MediaType
   @ line 1, column 1.
     import org.springframework.http.MediaType
     ^

  1 error

当然,我真正需要做的比这更复杂,但这是在 Gradle 任务中使用 Spring 库的简化示例。

如何将 spring 库导入到任务中?

最佳答案

基本上,依赖项缺失。

MediaType 类位于 spring-web 库中。

以下是具有依赖项的 gradle 文件的样子:

import org.springframework.http.MediaType
initscript {
    repositories {
        mavenCentral()
    }
    dependencies {
       // https://mvnrepository.com/artifact/org.springframework/spring-web
       classpath group: 'org.springframework', name: 'spring-web', version: '4.3.11.RELEASE'
    }
}

allprojects {
    task servertest {
        doLast {
            MediaType.parseMediaType("application/json")
        }
    }
}

关于maven - 将 Spring 依赖项添加到 gradle 任务中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46332910/

相关文章:

grails - 如何将保存在 SQL DB 中的字节数组绑定(bind)到 DataGrid 中 GSP 中的 Image 标签?

java - JAXB @XmlSchema 位置被忽略

java - 在 Ecliipse 中连接到 Oracle 数据库可以工作,但在 Maven 中连接失败

maven - 具有Jenkinsfile和Docker最佳实践的多分支管道项目

android - Android Gradle 构建有大小限制吗?

groovy - Gradle ,过载扩展设定器

java - 我的 ErrorHandler 上的 Jetty Maven 插件 ClassNotFoundException

java - Maven:使用 jar-with-dependencies 分发源代码

spring - Gradle,Spring Boot,spring-boot-starter-data-jpa升级

Grails Service 无法创建 Sql 实例