grails / grails-core

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

Grails 4 bootRun lost build/resources/main/ classpath. #11643

Open abcfy2 opened 3 years ago

abcfy2 commented 3 years ago

Thanks for reporting an issue for Grails framework, please review the task list below before submitting the issue. Your issue report will be closed if the issue is incomplete and the below tasks not completed.

NOTE: If you are unsure about something and the issue is more of a question a better place to ask questions is on Stack Overflow (http://stackoverflow.com/tags/grails) or Slack (http://slack-signup.grails.org). DO NOT use the issue tracker to ask questions.

Task List

Steps to Reproduce

Here is a sample BootStrap.groovy:

class BootStrap {
    def init = { servletContext ->
        System.getProperties().get("java.class.path").split(File.pathSeparator).each {
            println(it)
        }
    }
}

./gradlew bootRun or ./grailsw run-app output:

/home/fengyu/projects/creative/grails-rest-seed/src/main/resources
/home/fengyu/projects/creative/grails-rest-seed/grails-app/views
/home/fengyu/projects/creative/grails-rest-seed/grails-app/i18n
/home/fengyu/projects/creative/grails-rest-seed/grails-app/conf
/home/fengyu/projects/creative/grails-rest-seed/grails-app/migrations
/home/fengyu/projects/creative/grails-rest-seed/build/classes/java/main
/home/fengyu/projects/creative/grails-rest-seed/build/classes/groovy/main
/home/fengyu/projects/creative/grails-rest-seed/build/gson-classes/main
/home/fengyu/.gradle/caches/modules-2/files-2.1/org.springframework.boot/ ...
...

Lost build/resources/main in classpath.

But ./gradlew check or ./grailsw test-app output:

/home/fengyu/projects/creative/grails-rest-seed/build/classes/java/integrationTest
/home/fengyu/projects/creative/grails-rest-seed/build/classes/groovy/integrationTest
/home/fengyu/projects/creative/grails-rest-seed/build/resources/integrationTest
/home/fengyu/projects/creative/grails-rest-seed/build/classes/java/main
/home/fengyu/projects/creative/grails-rest-seed/build/classes/groovy/main
/home/fengyu/projects/creative/grails-rest-seed/build/resources/main    # <===== Here
/home/fengyu/projects/creative/grails-rest-seed/build/gson-classes/main
/home/fengyu/projects/creative/grails-rest-seed/build/classes/java/test
/home/fengyu/projects/creative/grails-rest-seed/build/classes/groovy/test
/home/fengyu/projects/creative/grails-rest-seed/build/resources/test
/home/fengyu/.gradle/caches/modules-2/files-2.1/org.springframework.boot/...
...

Contains build/resources/main in classpath

Expected Behaviour

bootRun should contain build/resources/main classpath

Actual Behaviour

Since Grails 4.0 lost build/resources/main classpath when bootRun

Environment Information

zyro23 commented 3 years ago

bootRun { sourceResources sourceSets.main } ?

./gradlew bootRun: .../src/main/resources ./gradlew check: .../build/resources/main

https://github.com/spring-projects/spring-boot/blob/v2.1.17.RELEASE/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/run/BootRun.java#L32-L42

jjelliott commented 3 years ago

This is intended behavior. It's required for hot reloading of GSPs/assets/etc.

What is your use case that this is breaking?

abcfy2 commented 3 years ago

Some resources is generated by building projects. E.g, com.gorylenko.gradle-git-properties plugin generate git.properties after building project.

If I want to get git info, I can inject GitProperties object:

class MyService {
    GitProperties gitProperties
    ... 
}

./grailws war is working, but ./grailsw run-app failed, because it cannot find git.properties in classpath.

And this code is working in Grails 3.x, but breaking in Grails 4.x.

jjelliott commented 3 years ago

If you want to keep GSP and other resource reloading, you should configure the plugin to add the git.properties file to src/main/resources/ instead and add that to your .gitignore.

Otherwise, you can remove the line sourceResources sourceSets.main from your bootRun configuration, but this will disable reloading from the following directories:

src/main/resources
grails-app/views
grails-app/i18n
grails-app/conf
grails-app/migrations
abcfy2 commented 3 years ago

gradle-git-properties will generate git.properties during building, and place to build/resources/main, which is not in bootRun classpath. An example: https://guides.grails.org/grails3/adding-commit-info/guide/index.html

But test task contains this path, and the old grails 3.x also contains build/resources/main in bootRun task.