我想使用Gradle在Spring应用程序中定义两个不同版本的bootRun
任务。这是我的尝试,它成功地定制了单个bootRun
任务。但是,对于包含bootRun
的多个任务,最后一个bootRun
将覆盖前面的任务。
task local {
bootRun {
systemProperty "spring.profiles.active", "dev,postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
}
}
local.finalizedBy bootRun
task prod {
bootRun {
systemProperty "spring.profiles.active", "postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED"
}
}
prod.finalizedBy bootRun
在这里,当我运行./gradlew :local
时,spring配置文件仅为postgres
。当我注释掉prod
任务时,我得到了预期的两个dev,postgres
。
发布于 2022-11-02 10:40:34
正如目前所写的,您的local
和prod
任务是默认任务,没有真正做任何事情。您在配置中使用bootRun
正在改变Spring的Gradle插件定义的现有bootRun
任务的配置。
定义任务时,需要告诉Gradle它们是BootRun
任务:
task local(type: org.springframework.boot.gradle.tasks.run.BootRun) {
// …
}
您还需要配置主类名和新任务的类路径。您可能希望它们与默认的bootRun
任务相同:
task local(type: org.springframework.boot.gradle.tasks.run.BootRun) {
mainClass = bootRun.mainClass
classpath = bootRun.classpath
}
然后,可以像以前一样自定义系统属性和JVM参数。将所有这些放在一起,您可以像这样配置您的local
和prod
任务:
task local(type: org.springframework.boot.gradle.tasks.run.BootRun) {
mainClass = bootRun.mainClass
classpath = bootRun.classpath
systemProperty "spring.profiles.active", "dev,postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
}
task prod(type: org.springframework.boot.gradle.tasks.run.BootRun) {
mainClass = bootRun.mainClass
classpath = bootRun.classpath
systemProperty "spring.profiles.active", "postgres"
jvmArgs "--add-opens=java.base/java.util=ALL-UNNAMED"
}
https://stackoverflow.com/questions/74282726
复制相似问题