0

In my project it is a requirement to achieve "zero configuration". What that means for the p.o. is that there is one command which configures the whole project and another command which starts the whole project. I'm relatively new in software development, especially with gradle. Here is my code what i did:

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"
}

Now the p.o said "there is definitely a better way to solve this" but i don't know one, i looked everywhere but without a result. I tried to start angular and springboot with one command, but the task never gets finished, so it only starts one of them. Is there maybe a way to somehow install angular, springboot and node.js with the ./gradlew build command or some other way to reduce the amount of commands neccessary? Any help would be greatly appreciated.

Fabio
  • 1
  • 2

1 Answers1

0

The configureProject task could be enhanced by adding the Gradle Node Plugin and using that for Node and NPM installation (just use the npmSetup task). NPM can then be used to install the Angular CLI, and subsequently a package.json script can be used to run ng serve. Finally, it doesn't look like you're using the Spring Boot CLI here. Are you sure you need it?

If you make the above changes for Node/NPM/Angular CLI and remove the Spring Boot CLI usages, then your configuration task will get simplified a lot.

For running the applications, Gradle is really a build tool (or alternatively a task runner) at its heart. It's not really meant to compose execution for multiple apps simultaneously. bootRun and long-running NPM tasks are possible, but typically only one of them can run per Gradle instance since Gradle only supports one simultaneous task execution per project.

Technically it's possible to run multiple apps with Gradle, but you'll have to either detach them from the Gradle process (https://stackoverflow.com/a/47202438/1941654) or use the Gradle Worker API. You should be able to submit all applications to be executed to the work queue via the worker API, but bear in mind that this is going to be a pretty manual solution and isn't what Gradle is intended for and will have some limitations. For example, you must ensure that you have enough Gradle workers to run all of the concurrent applications that you need.

If you have multiple applications that you want to run simultaneously, then you might want to consider using some other tools. For example, if you're running this through IntelliJ, then you can create a shared Compound Run Configuration which runs both tasks. If you're running this outside of a development environment, then you might consider using Docker Compose.

Mike Hill
  • 3,622
  • 23
  • 27
  • I typed in the task `./gradlew npmSetup` but i get the error "a problem occurred starting process 'command 'npm' ' when i try to do the command `npm install angular`. How can i fix this? – Fabio Mar 16 '20 at 09:13
  • Sorry, the `npmSetup` task just installs NPM to a Gradle-specific directory where it can then be used for other NPM tasks. Subsequent commands will still have to be executed through Gradle. Try adding a `package.json` script which executes `ng serve` (e.g., `"start": "ng serve"`). Your resulting Gradle task will be similar to NPM command, replacing spaces with underscores: `./gradlew npm_start`. – Mike Hill Mar 16 '20 at 15:37