Groovy + Java 混合编程方案:GMaven
Hava a look
先看效果。在项目中随便创建一个 SayHiUtil.groovy
,然后在main
里面添加个执行打印一下
package com.cmb.lr20.zxb.dialog.utils
class SayHIUtil {
static def sayHi() {
println "Hi groovy"
}
}
public class DialogApplication {
public static void main(String[] args) {
SayHIUtil.sayHi();
SpringApplication.run(DialogApplication.class, args);
}
}
DO IT!
使用GMavenPlus
,只需加个 plugin 和 dependency 即可:
<dependencies>
<!-- Groovy -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>3.0.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.13.1</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>compileTests</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<!-- if including source jars, use the no-fork goals
otherwise both the Groovy sources and Java stub sources
will get included in your jar -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<!-- source plugin \> = 2.1 is required to use the no-fork goals -->
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
<goal>test-jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
如果遇到编译问题,应该是source目录的问题。有两个解决方法:
- 将groovy文件放到
src/main/groovy
下 - 在plugin里面自定义source directory,具体详见GMavenPlugin的wiki
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。