grails / grails-core

The Grails Web Application Framework
http://grails.org
Apache License 2.0
2.78k stars 949 forks source link

Problems Running runCommand #13732

Closed jeffscottbrown closed 4 days ago

jeffscottbrown commented 1 week ago

Expected Behavior

The gradle runCommand task should work.

Actual Behaviour

In a default generated app, runCommand generates an error.

Steps To Reproduce

Invoke the runCommand task in a Grails 6.2.1 app.

$ [] sdk use grails 6.2.1

Using grails version 6.2.1 in this shell.
$ [] 
$ [] sdk use java 11.0.17-zulu 

Using java version 11.0.17-zulu in this shell.
$ [] 
$ [] grails create-app runcommanddemo
| Application created at /Users/jeffbrown/runcommanddemo
$ [] 
$ [] cd runcommanddemo 
$ [] 
$ [] 
$ [] ./gradlew runCommand -Pargs="url-mappings-report"

> Task :runCommand FAILED
Application class not found

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':runCommand'.
> Process 'command '/Users/jeffbrown/.sdkman/candidates/java/11.0.17-zulu/zulu-11.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 7s
6 actionable tasks: 6 executed

Environment Information

No response

Example Application

No response

Version

6.2.1

Taack commented 1 week ago

in the build.gradle, you should add the mainClass path.

application {
    mainClass.set("t621.Application")
}

But even with this path set, it does not work, stating:

> Task :runCommand FAILED
| Error Command not found for name: <name-of-the-command>

I can provide a test case if needed. I think a dependency is missing in my case (using default create-app deps).

jeffscottbrown commented 1 week ago

I can provide a test case if needed.

Thank you @Taack!

jamesfredley commented 6 days ago

It is working from ./grailsw or IntelliJ Grails Run Command.

https://github.com/grails/grails-gradle-plugin/blob/6.2.x/src/main/groovy/org/grails/gradle/plugin/web/GrailsWebGradlePlugin.groovy

grails> url-mappings-report

> Task :urlMappingsReport
Grails application running at http://localhost:0 in environment: development
Dynamic Mappings
 |    *     | ERROR: 404                                        | View:   /notFound        |
 |    *     | ERROR: 500                                        | View:   /error           |
 |    *     | /                                                 | View:   /index           |
 |    *     | /${controller}/${action}?/${id}?(.${format)?      | Action: (default action) |

BUILD SUCCESSFUL in 10s
6 actionable tasks: 3 executed, 3 up-to-date
jeffscottbrown commented 6 days ago

It does not appear that url-mappings-report has been migrated to work with ./gradlew runCommand.

Interesting, it does appear to work if I generate the app explicitly requesting JDK 11.

% sdk use grails 6.2.1
% sdk use java 11.0.22-zulu
% grails create-app --jdk=11  demoapp
% cd demoapp
% ./gradlew runCommand -Pargs="url-mappings-report"

> Task :runCommand
Grails application running at http://localhost:0 in environment: development
Dynamic Mappings
 |    *     | ERROR: 404                                        | View:   /notFound        |
 |    *     | ERROR: 500                                        | View:   /error           |
 |    *     | /                                                 | View:   /index           |
 |    *     | /${controller}/${action}?/${id}?(.${format)?      | Action: (default action) |

BUILD SUCCESSFUL in 5s
6 actionable tasks: 5 executed, 1 from cache
jeffscottbrown commented 6 days ago

in the build.gradle, you should add the mainClass path.

I don't think that is the issue. The generated build file does contain the following...

application {
    mainClass.set("demoapp.Application")
}
jeffscottbrown commented 6 days ago

Interesting, it does appear to work if I generate the app explicitly requesting JDK 11.

I have also verified that it appears to work when using JDK 17. I think the initial error reported above may be just a misleading indicator, and the JDK version incompatibility may be the root cause. When I filed the ticket I overlooked the fact that the source compatibility expressed in build.gradle is set to 17 by default when the app is created using create-app.

jeffscottbrown commented 6 days ago

https://github.com/grails/grails-forge/issues/383 is related the Java version issue.

jeffscottbrown commented 6 days ago

I think a dependency is missing in my case (using default create-app deps).

@Taack Do you know which dependency that is?

jeffscottbrown commented 6 days ago

@jamesfredley Do you feel that as far as we know, runCommand is working fine, and this report would be eligible to close as not-a-bug?

jamesfredley commented 6 days ago

@osscontributor I do believe we can attribute this to Java version and close it.

Taack commented 6 days ago

It is working from ./grailsw or IntelliJ Grails Run Command.

https://github.com/grails/grails-gradle-plugin/blob/6.2.x/src/main/groovy/org/grails/gradle/plugin/web/GrailsWebGradlePlugin.groovy

grails> url-mappings-report

> Task :urlMappingsReport
Grails application running at http://localhost:0 in environment: development
Dynamic Mappings
 |    *     | ERROR: 404                                        | View:   /notFound        |
 |    *     | ERROR: 500                                        | View:   /error           |
 |    *     | /                                                 | View:   /index           |
 |    *     | /${controller}/${action}?/${id}?(.${format)?      | Action: (default action) |

BUILD SUCCESSFUL in 10s
6 actionable tasks: 3 executed, 3 up-to-date

@jamesfredley Unfortunately, the Grails wrapper does not seem to work correctly in our case, because of our project's layout. See modular directory layout

Our layout:

intranet/grailsw
intranet/gradle*
intranet/server <- Grails main AND commands
intranet/app <- Apps plugin

Using gradlew we need to pass the gradle module where the command is, we cannot launch the command from server directly, unless we change all the completely the build:

./gradlew server:runCommand -Pargs="some-name"

I do not know how to do the same using the Grails Wrapper...

jamesfredley commented 5 days ago

@Taack it sounds like we have this solved for ./gradlew server:runCommand -Pargs="some-name", by ensuring the java version matches the one generated in build.gradle.

If you would also like to run this from grails-shell CLI for a multi-project build:

Grails-wrapper: I believe you would want to place the grails wrapper files in /server and /app and make the necessary configuration available which grails wrapper requires to start.

            props.load(new FileInputStream("gradle.properties"))
            grailsVersion = props.getProperty("grailsVersion")
            grailsShellVersion = props.getProperty("grailsShellVersion")
            groovyVersion = props.getProperty("groovyVersion")

or run from IntelliJ Run Grails Command: image