能否将Grails 3开发的插件作为依赖在Grails 2项目中运行?
Short answer: No, this won't work reliably—even if you match Groovy versions. Here's why:
Core Framework API Differences
Grails 3 is a complete architectural overhaul built on Spring Boot, while Grails 2 uses the older, standalone Grails runtime. Plugins written for Grails 3 rely on Grails 3-specific classes, annotations, and service patterns that don't exist in Grails 2. For example:- Grails 3 uses
@Serviceannotations and Spring Boot-style dependency injection, which Grails 2 doesn't recognize. - Core classes like
grails.core.GrailsApplicationhave different method signatures and behavior across versions.
You'll immediately hitClassNotFoundExceptionorNoSuchMethodErrorexceptions when trying to load the plugin.
- Grails 3 uses
Plugin Packaging & Loading Logic
Grails 3 plugins are packaged as Gradle-built JARs with a specific structure (including aMETA-INF/grails-plugin.xmlformat tailored for Spring Boot integration). Grails 2's plugin loader expects the older Maven/Grails 2 packaging structure and configuration format—it can't parse or initialize a Grails 3 plugin correctly.Dependency Tree Conflicts
Even with matching Groovy versions, Grails 3 depends on much newer versions of underlying libraries like Spring, Hibernate, and Apache Commons than Grails 2. Adding a Grails 3 plugin will pull in these newer libraries, creating version conflicts that break core Grails 2 functionality. For example, Grails 2 uses Spring 4.x, while Grails 3 uses Spring 5.x+—mixing these leads to endless class cast and method resolution errors.
A Workaround for Reusing Code
If you need to share functionality between a Grails 3 plugin and a Grails 2 project, extract the Grails-agnostic core logic into a separate Groovy library (no Grails API dependencies). Then:
- In your Grails 3 plugin, add this library as a dependency and wrap it with Grails 3-specific integration code.
- In your Grails 2 project, add the same library as a dependency and write Grails 2-specific adapters or services to use it.
内容的提问来源于stack exchange,提问作者mannuk

