gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:48:27 26 4
gpt4 key购买 nike

我想在我的机器上添加一个特殊的gradle任务作为初始化脚本。该脚本位于〜/ .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 Task中使用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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com