-
-
Notifications
You must be signed in to change notification settings - Fork 971
fix: configure Micronaut annotation processor and CLASSIC boot loader automatically #15411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.0.x
Are you sure you want to change the base?
Changes from 9 commits
32ac386
37f5114
6fd254b
19fa41e
beff44a
888fa45
6d4dfc9
fbf50a2
b3062c6
b04d6d5
4e9d6b9
ae7747a
e6e202a
782b950
03d1a51
79cdfc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.grails.forge.feature.micronaut; | ||
|
|
||
| import jakarta.inject.Singleton; | ||
| import org.grails.forge.application.ApplicationType; | ||
| import org.grails.forge.feature.Feature; | ||
| import org.grails.forge.feature.reloading.SpringBootDevTools; | ||
| import org.grails.forge.feature.validation.FeatureValidator; | ||
| import org.grails.forge.options.Options; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| @Singleton | ||
| public class GrailsMicronautValidator implements FeatureValidator { | ||
| @Override | ||
| public void validatePreProcessing(Options options, ApplicationType applicationType, Set<Feature> features) { | ||
| if (features.stream().anyMatch(f -> f instanceof GrailsMicronaut)) { | ||
| if (features.stream().anyMatch(f -> (f instanceof SpringBootDevTools))) { | ||
| // See: https://github.com/micronaut-projects/micronaut-spring/issues/769 | ||
| throw new IllegalArgumentException("Spring Boot Dev Tools are not supported with Grails Micronaut"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void validatePostProcessing(Options options, ApplicationType applicationType, Set<Feature> features) { | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,7 +71,10 @@ import org.springframework.boot.gradle.dsl.SpringBootExtension | |
| import org.springframework.boot.gradle.plugin.ResolveMainClassName | ||
| import org.springframework.boot.gradle.plugin.SpringBootPlugin | ||
| import org.springframework.boot.gradle.tasks.bundling.BootArchive | ||
| import org.springframework.boot.gradle.tasks.bundling.BootJar | ||
| import org.springframework.boot.gradle.tasks.bundling.BootWar | ||
| import org.springframework.boot.gradle.tasks.run.BootRun | ||
| import org.springframework.boot.loader.tools.LoaderImplementation | ||
|
|
||
| import javax.inject.Inject | ||
|
|
||
|
|
@@ -376,10 +379,22 @@ class GrailsGradlePlugin extends GroovyPlugin { | |
| } | ||
| } | ||
|
|
||
| project.logger.info('Adding Micronaut annotationProcessor dependencies to project {}', project.name) | ||
| // Add Micronaut annotation processor for Java source files. | ||
| // Groovy sources are handled by micronaut-inject-groovy AST transforms (via compileOnlyApi), | ||
| // but Java sources require the Java annotation processor to generate BeanDefinition classes. | ||
| // The annotationProcessor configuration only affects compileJava tasks, not compileGroovy. | ||
| project.logger.info('Adding Micronaut annotationProcessor for Java sources in {}', project.name) | ||
|
||
| project.getDependencies().add('annotationProcessor', project.dependencies.platform("io.micronaut.platform:micronaut-platform:$micronautPlatformVersion")) | ||
| project.getDependencies().add('annotationProcessor', 'io.micronaut:micronaut-inject-java') | ||
| project.getDependencies().add('annotationProcessor', 'jakarta.annotation:jakarta.annotation-api') | ||
|
|
||
| project.logger.info('Configuring CLASSIC boot loader for Micronaut compatibility in {}', project.name) | ||
| project.tasks.withType(BootJar).configureEach { BootJar bootJar -> | ||
| bootJar.loaderImplementation.convention(LoaderImplementation.CLASSIC) | ||
| } | ||
| project.tasks.withType(BootWar).configureEach { BootWar bootWar -> | ||
| bootWar.loaderImplementation.convention(LoaderImplementation.CLASSIC) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,8 @@ spring: | |
| management: | ||
| endpoints: | ||
| enabled-by-default: false | ||
| app: | ||
| name: test-micronaut-app | ||
|
|
||
| --- | ||
| grails: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package micronaut | ||
|
|
||
| import bean.injection.AppConfig | ||
| import bean.injection.FactoryCreatedService | ||
| import bean.injection.JavaSingletonService | ||
| import bean.injection.NamedService | ||
| import groovy.transform.CompileStatic | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired | ||
|
|
||
| @CompileStatic | ||
| class MicronautTestController { | ||
|
|
||
| @Autowired | ||
| JavaSingletonService javaSingletonService | ||
|
|
||
| @Autowired | ||
| FactoryCreatedService factoryCreatedService | ||
|
|
||
| @Autowired | ||
| AppConfig appConfig | ||
|
|
||
| @Autowired | ||
| NamedService namedService | ||
|
|
||
| def index() { | ||
| render(contentType: 'application/json', text: [ | ||
| javaMessage: javaSingletonService.message, | ||
| factoryName: factoryCreatedService.name, | ||
| appName: appConfig.name, | ||
| namedService: namedService.name | ||
| ]) | ||
jamesfredley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package micronaut | ||
|
|
||
| import bean.injection.AppConfig | ||
| import bean.injection.FactoryCreatedService | ||
| import bean.injection.JavaSingletonService | ||
| import bean.injection.NamedService | ||
|
|
||
| import grails.testing.mixin.integration.Integration | ||
| import io.micronaut.context.ApplicationContext as MicronautApplicationContext | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.context.ApplicationContext as SpringApplicationContext | ||
| import org.springframework.context.ApplicationContextAware | ||
| import spock.lang.Specification | ||
|
|
||
| /** | ||
| * Integration tests verifying no bean duplication occurs between Spring and Micronaut contexts. | ||
| * | ||
| * When micronaut-spring bridges Micronaut beans into Spring, each bean should appear | ||
| * exactly once in the Spring context (not duplicated). Similarly, Micronaut beans | ||
| * should maintain correct counts in the Micronaut context. | ||
| */ | ||
| @Integration | ||
| class MicronautBeanDuplicationSpec extends Specification implements ApplicationContextAware { | ||
|
|
||
| @Autowired | ||
| MicronautApplicationContext micronautContext | ||
|
|
||
| SpringApplicationContext springContext | ||
|
|
||
| void setApplicationContext(SpringApplicationContext applicationContext) { | ||
| this.springContext = applicationContext | ||
| } | ||
|
|
||
| void "Java @Singleton bean appears exactly once in Spring context"() { | ||
| when: | ||
| def beans = springContext.getBeansOfType(JavaSingletonService) | ||
|
|
||
| then: "only one instance is registered, not duplicated by the bridge" | ||
| beans.size() == 1 | ||
| } | ||
|
|
||
| void "@Factory/@Bean created bean appears exactly once in Spring context"() { | ||
| when: | ||
| def beans = springContext.getBeansOfType(FactoryCreatedService) | ||
|
|
||
| then: "only one instance is registered" | ||
| beans.size() == 1 | ||
| } | ||
|
|
||
| void "@ConfigurationProperties bean appears exactly once in Spring context"() { | ||
| when: | ||
| def beans = springContext.getBeansOfType(AppConfig) | ||
|
|
||
| then: "only one instance is registered" | ||
| beans.size() == 1 | ||
| } | ||
|
|
||
| void "NamedService implementations appear exactly 4 times in Spring context"() { | ||
| when: | ||
| def beans = springContext.getBeansOfType(NamedService) | ||
|
|
||
| then: "4 implementations, not 8 from duplication" | ||
| beans.size() == 4 | ||
| } | ||
|
|
||
| void "Micronaut context has exactly 4 NamedService implementations"() { | ||
| when: | ||
| def beans = micronautContext.getBeansOfType(NamedService) | ||
|
|
||
| then: | ||
| beans.size() == 4 | ||
| } | ||
|
|
||
| void "Grails service appears exactly once in Spring context"() { | ||
| when: | ||
| def beans = springContext.getBeansOfType(BeanInjectionService) | ||
|
|
||
| then: "Grails artefact not duplicated by Micronaut bridge" | ||
| beans.size() == 1 | ||
| } | ||
|
|
||
| void "bridged Micronaut bean in Spring is same instance as in Micronaut context"() { | ||
| when: | ||
| def fromSpring = springContext.getBean(JavaSingletonService) | ||
| def fromMicronaut = micronautContext.getBean(JavaSingletonService) | ||
|
|
||
| then: "bridge shares the same singleton, not a copy" | ||
| fromSpring.is(fromMicronaut) | ||
| } | ||
|
|
||
| void "factory-created bean in Spring is same instance as in Micronaut context"() { | ||
| when: | ||
| def fromSpring = springContext.getBean(FactoryCreatedService) | ||
| def fromMicronaut = micronautContext.getBean(FactoryCreatedService) | ||
|
|
||
| then: "bridge shares the same singleton" | ||
| fromSpring.is(fromMicronaut) | ||
| } | ||
|
|
||
| void "@ConfigurationProperties bean in Spring is same instance as in Micronaut context"() { | ||
| when: | ||
| def fromSpring = springContext.getBean(AppConfig) | ||
| def fromMicronaut = micronautContext.getBean(AppConfig) | ||
|
|
||
| then: "bridge shares the same singleton" | ||
| fromSpring.is(fromMicronaut) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.