Skip to content
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

added a new MicronautGrailsApp #5

Merged
merged 7 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
plugins {
id 'org.kordamp.gradle.groovy-project'
id 'org.kordamp.gradle.bintray'
id 'com.github.kt3k.coveralls'
}

if (!project.hasProperty('bintrayUsername')) ext.bintrayUsername = '**UNDEFINED**'
Expand Down Expand Up @@ -115,6 +114,7 @@ subprojects { Project subproject ->
dependencyManagement {
imports {
mavenBom "io.micronaut:micronaut-bom:$micronautVersion"
mavenBom "org.grails:grails-bom:${grailsVersion}"
}
}

Expand Down Expand Up @@ -178,18 +178,3 @@ subprojects { Project subproject ->
}
}
}

gradle.taskGraph.whenReady {
coveralls {
sourceDirs = subprojects.findAll { it.plugins.hasPlugin('java') } sourceSets.main.allSource.srcDirs.flatten()
jacocoReportPath = "${buildDir}/reports/jacoco/root/jacocoTestReport.xml"
}
}

tasks.coveralls {
group = 'Coverage reports'
description = 'Uploads the aggregated coverage report to Coveralls'

dependsOn jacocoRootReport
onlyIf { System.env.CI || System.env.GITHUB_ACTIONS }
}
72 changes: 62 additions & 10 deletions docs/guide/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ image::https://coveralls.io/repos/github/{project-slug}/badge.svg?branch=master[

= Micronaut Grails

Micronaut Grails package helps using Micronaut beans in the Grails application or any other Spring application.
There are two additional features which cannot be found the official https://docs.micronaut.io/latest/guide/index.html#springBeans[Spring support for Micronaut]:

1. Micronaut beans' names defaults to lower-cased simple name of the class as expected by Grails
2. Ability to reuse existing properties declared by Grails - e.g. `grails.redis.port` can be injected as `@Value('${redis.port}')`


== Installation

[source,indent=0,role="primary",subs='verbatim,attributes']
Expand All @@ -38,7 +31,66 @@ TIP: If you plan to reuse same library for Micronaut and Grails, you can declare

== Usage

The integration is handle by bean processor which needs to be injected into Spring application context. The easiest
=== Grails 4

Micronaut Grails library helps to use Micronaut beans in the Grails 4 application or any other Spring application.
The main additional feature is the ability to do more customisation to the Micronaut context such as adding a package for scanning.

Replace the body of the `main` method inside `grails-app/init/.../Application.groovy` with the following code.

[source,groovy]
.Grails 4 Usage
----
include::{root-dir}/examples/micronaut-grails-example/grails-app/init/micronaut/grails/example/Application.groovy[lines=20..-1]
----
<1> Use `MicronautGrailsApp` instead of `GrailsApp`
<2> Set the compatibility mode (see below - defaults to `STRICT`)
<3> Set the source to current class
<4> Forward the arguments
<5> customise the environment
<6> For example, customise the packages to be scanned
<7> Allow system properties to be all-upper-case (useful for AWS Beanstalk Tomcat environment)

==== Compatibility Modes

The compatibility mode signals what is the level of compatibility with the legacy features mentioned in Grails 3 section.
The default is `STRICT` which means no features are enabled and Grails 4 Micronaut out-of-box integration is used.
If you are migrating from Grails 3 then you should select `LEGACY` and enable `com.agorapulse.micronaut.grails` logging
to `INFO` to see the deprecation details.

|===
|Mode |LEGACY |BRIDGE |STRICT|

|Single Application Context
|No
|Yes
|Yes
|If _No_ then two application contexts are created - one for beans using `@Inject` and another one for beans injected by name.

|Injection by Name
|Yes
|Yes
|No
|If _No_ then the beans require `@Inject` annotation at the injection point, and they can be no longer injected by name.

|Property Translation
|Yes
|No
|No
|If _No_ then Spring beans of type `PropertyTranslatingcustomiser` are ignored prefix replacements no longer work. Otherwise, the customization applies to all beans injected by name (not using `@Inject`).
|===


=== Grails 3

Micronaut Grails library helps to use Micronaut beans in the Grails 3 application or any other Spring application.
There are three additional features which cannot be found the official https://micronaut-projects.github.io/micronaut-spring/latest/guide/[Spring support for Micronaut]:

1. Ability to modify Micronaut environment (e.g. add packages for scanning)
2. Micronaut beans' names defaults to lower-cased simple name of the class as expected by Grails
3. Ability to reuse existing properties declared by Grails - e.g. `grails.redis.port` can be injected as `@Value('${redis.port}')`

The integration is handled by bean processor which needs to be injected into Spring application context. The easiest
thing is to create Spring configuration placed next to your Micronaut classes. The Spring configuration class will
create `MicronautBeanImporter` which will be later processed by the processor bean:

Expand All @@ -49,13 +101,13 @@ include::{root-dir}/subprojects/micronaut-grails/src/test/groovy/com/agorapulse/
<1> Define class as Spring `@Configuration`
<2> Declare method which returns bean processor `@Bean`
<3> The name of the Spring bean defaults to the _property name_ of the class, e.g. `widget`
<4> You can provide different name
<4> You can provide a different name
<5> You can qualify using a stereotype (annotation)
<6> You can qualify using the name of the bean which will be the same in the Spring application
<7> You can combine any qualifiers possible to narrow the search to single bean which needs to be available from the Spring application context


WARNING: If more then one bean qualifies the criteria then an exception will be thrown.
WARNING: If more than one bean qualifies the criteria then an exception will be thrown.

Once you have your configuration class ready then you can create `META-INF/spring.factories` descriptor in resources folder
which will automatically load the configuration once the JAR is on classpath.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020 Vladimir Orany.
*
* Licensed 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 com.agorapulse.micronaut.grails.domain

import grails.compiler.GrailsCompileStatic
import grails.gorm.annotation.Entity

/**
* Test entity.
*/
@Entity
@GrailsCompileStatic
@SuppressWarnings([
'GrailsDomainHasEquals',
'GrailsDomainHasToString',
'FieldTypeRequired',
])
class Manager {

static constraints = {
name maxSize: 255
}

String name

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020 Vladimir Orany.
*
* Licensed 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.
*/
apply plugin:"groovy"

sourceSets {
main {
groovy {
srcDir 'grails-app/domain'
}
}
}

dependencies {
compile 'org.grails:grails-datastore-gorm-hibernate5'

compileOnly "io.micronaut:micronaut-inject-groovy"

compile 'io.micronaut.configuration:micronaut-hibernate-gorm'

compileOnly "org.grails:grails-core:$grailsVersion"

compile "org.grails:grails-bootstrap:$grailsVersion"

annotationProcessor "io.micronaut:micronaut-inject-java"
compile "io.micronaut:micronaut-runtime-groovy"
compile "io.micronaut:micronaut-validation"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020 Vladimir Orany.
*
* Licensed 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 com.agorapulse.micronaut.grails.domain

import grails.gorm.services.Service
import groovy.transform.CompileStatic

/**
* Test data service.
*/
@CompileStatic
@Service(Manager)
abstract class ManagerService {

abstract Manager get(Long id)
abstract List<Manager> findAllByName(String name)

}
23 changes: 23 additions & 0 deletions examples/micronaut-grails-example/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# SPDX-License-Identifier: Apache-2.0
#
# Copyright 2020 Vladimir Orany.
#
# Licensed 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.
#

grailsVersion=4.0.4
gorm.version=7.0.6.RELEASE
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.jvmargs=-Dfile.encoding=UTF-8 -Xmx1024M
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading