在我的项目中,实现“零配置”是一个要求。这对邮政局长意味着什么。一个命令用于配置整个项目,另一个命令用于启动整个项目。我在软件开发方面相对较新,尤其是使用gradle。下面是我所做的代码:
def frontendDir = "$projectDir/src/main/resources/assets/anwboard-frontend/"
task configureProject(type:Exec) {
workingDir "$frontendDir"
inputs.dir "$frontendDir"
def angularVersion = commandLine "ng", "--version"
def springbootVersion = commandLine "spring", "version"
def nodeVersion = commandLine "node", "-v"
if (nodeVersion == null) {
if (System.getProperty("os.name").toUpperCase().contains("WINDOWS")) {
commandLine "choco", "install", "node.jsinstall"
}else {
commandLine "brew", "install", "node"
}
}
if (angularVersion == null) {
if (System.getProperty("os.name").toUpperCase().contains("WINDOWS")) {
commandLine "npm", "install", "@angular/cli"
} else {
commandLine "npm", "install", "angular"
}
}
if (springbootVersion == null) {
if (System.getProperty("os.name").toUpperCase().contains("WINDOWS")) {
commandLine "choco", "install", "spring-boot-cli"
}else {
commandLine "brew", "tap", "pivotal/tap"
commandLine "brew", "install", "springboot"
}
}
}
task buildAngular(type:Exec) {
workingDir "$frontendDir"
inputs.dir "$frontendDir"
commandLine "ng", "serve", "-o"
}
task buildSpringboot(type:Exec){
workingDir "$projectDir"
inputs.dir "$projectDir"
commandLine "./gradlew", "bootRun"
}现在邮局说“肯定有更好的办法来解决这个问题”,但我不知道,我到处找了看,但没有结果。我尝试用一个命令启动angular和springboot,但任务从未完成,所以它只启动了其中的一个。有没有办法通过./gradlew build命令来安装angular、springboot和node.js,或者其他一些方法来减少必要的命令数量?任何帮助都将不胜感激。
发布于 2020-03-13 03:36:58
可以通过添加Gradle Node Plugin并将其用于节点和NPM安装来增强configureProject任务(只需使用npmSetup任务)。然后,可以使用NPM来安装Angular CLI,随后可以使用package.json脚本来运行ng serve。最后,这里看起来不像是在使用Spring Boot CLI。你确定你需要它吗?
如果您对Node/NPM/Angular CLI进行了上述更改,并删除了Spring Boot CLI用法,那么您的配置任务将会大大简化。
对于运行应用程序来说,Gradle实际上是一个构建工具(或者任务运行器)。它并不是真的意味着同时为多个应用程序编写执行。bootRun和长时间运行的NPM任务是可能的,但通常每个Gradle实例只能运行其中一个任务,因为Gradle只支持每个项目同时执行一个任务。
从技术上讲,可以使用Gradle运行多个应用程序,但您必须将它们从Gradle进程(https://stackoverflow.com/a/47202438/1941654)中分离出来,或者使用Gradle Worker API。您应该能够通过worker API将所有要执行的应用程序提交到工作队列,但请记住,这将是一个非常手动的解决方案,并且不是Gradle的目标,并且会有一些限制。例如,您必须确保有足够的Gradle工作线程来运行所需的所有并发应用程序。
如果您希望同时运行多个应用程序,则可能需要考虑使用其他一些工具。例如,如果您通过IntelliJ运行此任务,那么您可以创建一个共享的复合运行配置,它可以同时运行这两个任务。如果你在开发环境之外运行,那么你可以考虑使用Docker Compose。
https://stackoverflow.com/questions/60656852
复制相似问题