Menu

  • Home
  • Work
    • Cloud
      • Virtualization
      • IaaS
      • PaaS
    • Java
    • Go
    • C
    • C++
    • JavaScript
    • PHP
    • Python
    • Architecture
    • Others
      • Assembly
      • Ruby
      • Perl
      • Lua
      • Rust
      • XML
      • Network
      • IoT
      • GIS
      • Algorithm
      • AI
      • Math
      • RE
      • Graphic
    • OS
      • Linux
      • Windows
      • Mac OS X
    • BigData
    • Database
      • MySQL
      • Oracle
    • Mobile
      • Android
      • IOS
    • Web
      • HTML
      • CSS
  • Life
    • Cooking
    • Travel
    • Gardening
  • Gallery
  • Video
  • Music
  • Essay
  • Home
  • Work
    • Cloud
      • Virtualization
      • IaaS
      • PaaS
    • Java
    • Go
    • C
    • C++
    • JavaScript
    • PHP
    • Python
    • Architecture
    • Others
      • Assembly
      • Ruby
      • Perl
      • Lua
      • Rust
      • XML
      • Network
      • IoT
      • GIS
      • Algorithm
      • AI
      • Math
      • RE
      • Graphic
    • OS
      • Linux
      • Windows
      • Mac OS X
    • BigData
    • Database
      • MySQL
      • Oracle
    • Mobile
      • Android
      • IOS
    • Web
      • HTML
      • CSS
  • Life
    • Cooking
    • Travel
    • Gardening
  • Gallery
  • Video
  • Music
  • Essay

基于Eclipse和Maven的Groovy开发

12
Jun
2014

基于Eclipse和Maven的Groovy开发

By Alex
/ in Java
/ tags Groovy
0 Comments
Eclipse和Maven插件
插件  说明 
Groovy Eclipse

Update Site:http://dist.springsource.org/release/GRECLIPSE/e4.3/ 安装全部组件

该插件自带了一组Groovy编译器,其优势是不需要生成Groovy类的存根

m2e Configurator for Groovy Eclipse

此插件包含在上面的Update Site中,与m2eclipse集成,可以方便在Eclipse中,以Maven为中心管理Groovy工程。

该插件与AspectJ、Scala不兼容,因此你不能在同一工程中使用这些语言

groovy-eclipse-compiler

一个Maven插件,允许使用Groovy Eclipse编译器来编译Maven工程中的Groovy代码,并将工程配置为Groovy Project。该插件需要上述的m2e Configurator才能正常工作。

gmavenplus-plugin

即GMavenPlus,一个Maven插件,是GMaven的代替者,包括了比groovy-eclipse-compiler更丰富的功能:

  1. 支持GroovyDoc
  2. 支持Groovy Mojos
  3. 可以在POM中定义需要执行的Groovy脚本
  4. 支持InvokeDynamic特性
  5. 支持Groovy Android 
  6. 支持Configuration scripts 
  7. 支持交互式的Shell/Console

该插件目前缺乏对应的m2e connector,无法在Eclipse中正常使用

Groovy Eclipse配置

定位到Window ⇨ Preferences -Groovy进行设置:

  1. Groovy Content Assist options... :可以让代码风格更加Groovy化,根据需要设置
  2. Compiler子菜单:可以切换Groovy编译器版本,必须和Groovy运行时依赖的版本保持兼容
  3. Formatter子菜单:设置代码格式化规则,可以设置的内容很少:
    1. Position of the opening braces:起始括号是否换行
    2. Remove unnecessary semicolons:移除不必要的分号
groovy-eclipse-compiler示例
pom.xml
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<dependencies>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.3.7</version>
    </dependency>
    <!-- 如果要支持Project Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
       <version>1.14.8</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <!-- maven-compiler-plugin 实际不做任何编译工作,它总是把工作委派给真正的编译器,例如javac/groovyc-->
                <compilerId>groovy-eclipse-compiler</compilerId>
                <!-- 如果要支持Project Lombok,添加: -->
                <verbose>true</verbose>
                <compilerArguments>
                    <javaAgentClass>lombok.core.Agent</javaAgentClass>
                </compilerArguments>
                <fork>true</fork>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-compiler</artifactId>
                    <version>2.9.1-01</version>
                </dependency>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-batch</artifactId>
                    <version>2.3.7-01</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <!-- 为Groovy源文件分配单独的存放目录 -->
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/groovy</source>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>add-test-source</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/test/groovy</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

将上述内容添加到一个Maven工程的POM中以后,Update Project,该工程自动变为 Groovy Project,工程结构如下:

groovy-project

gmavenplus-plugin详解
常用Goals
Goal  说明 
gplus:addSources 把Groovy源码加到工程main源码清单
gplus:addTestSources 把Groovy测试源码加到工程test源码清单
gplus:compile 编译main源代码,如果要支持InvokeDynamic,必须使用1.5.0+或者 2.0.0-beta-3+的Groovy
gplus:console 开启一个关联到当前工程的Console窗口
gplus:shell 开启一个关联到当前工程的Shell
gplus:execute 执行关联到当前工程的Groovy脚本(包括POM或者外部的)
gplus:generateStubs

为main Groovy源码生成存根,并添加到Maven源码目录,供Java编译器发现

之所以需要存根,是因为Java编译器无法识别Groovy代码,如果存在Java对Groovy的调用,需要生成被调用类的空壳(不含方法实现),以便Java编译的通过

gplus:groovydoc 为main 代码生成GroovyDoc
gplus:removeStubs 从工程中移除Groovy存根(Stub)
gplus:removeTestStubs
用法示例
执行Groovy脚本

可以把executie目录绑定到任何阶段,如果不绑定,可以运行命令 mvn gplus:execute 手工执行。

使用下面的POM配置可以包含Groovy脚本到pom.xml中:

pom.xml
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<plugin>
    <groupId>org.codehaus.gmavenplus</groupId>
    <artifactId>gmavenplus-plugin</artifactId>
    <version>1.5</version>
    <executions>
        <execution>
            <goals>
                <goal>execute</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <scripts>
            <script>
                <![CDATA[
                    //Script content
                ]]>
            </script>
            <!-- 亦可外链脚本文件 -->
            <script>file:///${project.basedir}/src/main/resources/scr.groovy</script>
        </scripts>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.3.7</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</plugin>
编译Groovy源码

目标compile附加到Maven声明周期的相应阶段,只需要执行默认的 mvn compile ,就可以编译Groovy源码。

下面是一个典型的POM配置:

pom.xml
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <goals>
                        <!-- 添加Groovy源码到Maven的main源码清单,打包source jar时有用 -->
                        <goal>addSources</goal>
                        <!-- 添加Groovy源码到Maven的test源码清单,打包source jar时有用 -->
                        <goal>addTestSources</goal>
                        <!-- 生成main Groovy代码的Java Stub,这些存根随即被加到main源码清单,以便Java编译器可以使用之 -->
                        <!-- 如果不进行Java/Groovy混合编程,可以忽略这一目标 -->
                        <goal>generateStubs</goal>
                        <!-- 在编译Groovy main源码前,Java main代码会和Stub一起被编译 -->
                        <!-- 编译Groovy main源码 -->
                        <goal>compile</goal>
                        <!-- 移除main存根,因为存根只在编译Java代码时用到,如果不移除,导致souce jar中包含存根代码 -->
                        <goal>removeStubs</goal>
                        <!-- 生成test源码的存根 -->
                        <goal>testGenerateStubs</goal>
                        <!-- 编译test源码,与main源码类似 -->
                        <goal>testCompile</goal>
                        <!-- 移除test存根 -->
                        <goal>removeTestStubs</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <!-- 使用该目标,防止Groovy源码和Java Stub都被包含到source jar -->
                        <goal>jar-no-fork</goal>
                        <goal>test-jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- 添加下面的配置以支持JDK 1.7+的InvokeDynamic特性,但是目标JVM必须为JDK 1.7+-->
                <invokeDynamic>true</invokeDynamic>
                <!-- 可以使用Groovy配置脚本(configuration script)-->
                <configScript>config.groovy</configScript>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.3.7</version>
    </dependency>
</dependencies>

如果是纯粹的Groovy工程,不包含Java代码,可以这样配置Goal:

XHTML
1
2
3
4
5
6
<goals>
    <goal>addSources</goal>
    <goal>addTestSources</goal>
    <goal>compile</goal>
    <goal>testCompile</goal>
</goals>

要为Android平台编译Groovy,只需要指定适用于该平台的Groovy的JAR即可:

XHTML
1
2
3
4
5
6
<dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy</artifactId>
      <version>2.4.4</version>
      <classifier>grooid</classifier>
</dependency>
指定额外的源码目录

如果你的Groovy源码不使用标准的.groovy扩展名,或者不存放在src/[main|test]/groovy目录下,必须通知GMavenPlus:

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<configuration>
    <sources>
        <source>
            <directory>${project.basedir}/src/main/groovy</directory>
            <includes>
                <include>**/*.groovy</include>
            </includes>
        </source>
        <source>
            <directory>${project.basedir}/src/additional</directory>
            <includes>
                <include>groovy/**/*.groovy</include>
                <include>gvy/**/*.gvy</include>
            </includes>
        </source>
    </sources>
    <testSources>
        <testSource>
            <directory>${project.basedir}/src/test/groovy</directory>
            <includes>
                <include>**/*.groovy</include>
            </includes>
        </testSource>
    </testSources>
</configuration>
常见问题 
无法调试Groovy脚本

如果可以调试正规化的Groovy类(与Java类一样,遵循存储路径与类全限定名称的对应关系),但是不能调试脚本,可能是因为当前脚本不在Classpath下面。

解决办法:编写脚本时,要进行包声明,例如: package cc.gmem.study.groovy 

 

← Java8新特性
JVM参数与性能调优 →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Related Posts

  • Groovy学习笔记
  • 基于nGrinder进行负载测试
  • ProGuard学习笔记
  • Log4J2学习笔记
  • Bazel学习笔记

Recent Posts

  • Investigating and Solving the Issue of Failed Certificate Request with ZeroSSL and Cert-Manager
  • A Comprehensive Study of Kotlin for Java Developers
  • 背诵营笔记
  • 利用LangChain和语言模型交互
  • 享学营笔记
ABOUT ME

汪震 | Alex Wong

江苏淮安人,现居北京。目前供职于腾讯云,专注容器方向。

GitHub:gmemcc

Git:git.gmem.cc

Email:gmemjunk@gmem.cc@me.com

ABOUT GMEM

绿色记忆是我的个人网站,域名gmem.cc中G是Green的简写,MEM是Memory的简写,CC则是我的小天使彩彩名字的简写。

我在这里记录自己的工作与生活,同时和大家分享一些编程方面的知识。

GMEM HISTORY
v2.00:微风
v1.03:单车旅行
v1.02:夏日版
v1.01:未完成
v0.10:彩虹天堂
v0.01:阳光海岸
MIRROR INFO
Meta
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
Recent Posts
  • Investigating and Solving the Issue of Failed Certificate Request with ZeroSSL and Cert-Manager
    In this blog post, I will walk ...
  • A Comprehensive Study of Kotlin for Java Developers
    Introduction Purpose of the Study Understanding the Mo ...
  • 背诵营笔记
    Day 1 Find Your Greatness 原文 Greatness. It’s just ...
  • 利用LangChain和语言模型交互
    LangChain是什么 从名字上可以看出来,LangChain可以用来构建自然语言处理能力的链条。它是一个库 ...
  • 享学营笔记
    Unit 1 At home Lesson 1 In the ...
  • K8S集群跨云迁移
    要将K8S集群从一个云服务商迁移到另外一个,需要解决以下问题: 各种K8S资源的迁移 工作负载所挂载的数 ...
  • Terraform快速参考
    简介 Terraform用于实现基础设施即代码(infrastructure as code)—— 通过代码( ...
  • 草缸2021
    经过四个多月的努力,我的小小荷兰景到达极致了状态。

  • 编写Kubernetes风格的APIServer
    背景 前段时间接到一个需求做一个工具,工具将在K8S中运行。需求很适合用控制器模式实现,很自然的就基于kube ...
  • 记录一次KeyDB缓慢的定位过程
    环境说明 运行环境 这个问题出现在一套搭建在虚拟机上的Kubernetes 1.18集群上。集群有三个节点: ...
  • eBPF学习笔记
    简介 BPF,即Berkeley Packet Filter,是一个古老的网络封包过滤机制。它允许从用户空间注 ...
  • IPVS模式下ClusterIP泄露宿主机端口的问题
    问题 在一个启用了IPVS模式kube-proxy的K8S集群中,运行着一个Docker Registry服务 ...
  • 念爷爷
      今天是爷爷的头七,十二月七日、阴历十月廿三中午,老人家与世长辞。   九月初,回家看望刚动完手术的爸爸,发

  • 6 杨梅坑

  • liuhuashan
    深圳人才公园的网红景点 —— 流花山

  • 1 2020年10月拈花湾

  • 内核缺陷触发的NodePort服务63秒延迟问题
    现象 我们有一个新创建的TKE 1.3.0集群,使用基于Galaxy + Flannel(VXLAN模式)的容 ...
  • Galaxy学习笔记
    简介 Galaxy是TKEStack的一个网络组件,支持为TKE集群提供Overlay/Underlay容器网 ...
TOPLINKS
  • Zitahli's blue 91 people like this
  • 梦中的婚礼 64 people like this
  • 汪静好 61 people like this
  • 那年我一岁 36 people like this
  • 为了爱 28 people like this
  • 小绿彩 26 people like this
  • 杨梅坑 6 people like this
  • 亚龙湾之旅 1 people like this
  • 汪昌博 people like this
  • 彩虹姐姐的笑脸 24 people like this
  • 2013年11月香山 10 people like this
  • 2013年7月秦皇岛 6 people like this
  • 2013年6月蓟县盘山 5 people like this
  • 2013年2月梅花山 2 people like this
  • 2013年淮阴自贡迎春灯会 3 people like this
  • 2012年镇江金山游 1 people like this
  • 2012年徽杭古道 9 people like this
  • 2011年清明节后扬州行 1 people like this
  • 2008年十一云龙公园 5 people like this
  • 2008年之秋忆 7 people like this
  • 老照片 13 people like this
  • 火一样的六月 16 people like this
  • 发黄的相片 3 people like this
  • Cesium学习笔记 90 people like this
  • IntelliJ IDEA知识集锦 59 people like this
  • 基于Kurento搭建WebRTC服务器 38 people like this
  • Bazel学习笔记 37 people like this
  • PhoneGap学习笔记 32 people like this
  • NaCl学习笔记 32 people like this
  • 使用Oracle Java Mission Control监控JVM运行状态 29 people like this
  • 基于Calico的CNI 27 people like this
  • Ceph学习笔记 27 people like this
  • Three.js学习笔记 24 people like this
Tag Cloud
ActiveMQ AspectJ CDT Ceph Chrome CNI Command Cordova Coroutine CXF Cygwin DNS Docker eBPF Eclipse ExtJS F7 FAQ Groovy Hibernate HTTP IntelliJ IO编程 IPVS JacksonJSON JMS JSON JVM K8S kernel LB libvirt Linux知识 Linux编程 LOG Maven MinGW Mock Monitoring Multimedia MVC MySQL netfs Netty Nginx NIO Node.js NoSQL Oracle PDT PHP Redis RPC Scheduler ServiceMesh SNMP Spring SSL svn Tomcat TSDB Ubuntu WebGL WebRTC WebService WebSocket wxWidgets XDebug XML XPath XRM ZooKeeper 亚龙湾 单元测试 学习笔记 实时处理 并发编程 彩姐 性能剖析 性能调优 文本处理 新特性 架构模式 系统编程 网络编程 视频监控 设计模式 远程调试 配置文件 齐塔莉
Recent Comments
  • qg on Istio中的透明代理问题
  • heao on 基于本地gRPC的Go插件系统
  • 黄豆豆 on Ginkgo学习笔记
  • cloud on OpenStack学习笔记
  • 5dragoncon on Cilium学习笔记
  • Archeb on 重温iptables
  • C/C++编程:WebSocketpp(Linux + Clion + boostAsio) – 源码巴士 on 基于C/C++的WebSocket库
  • jerbin on eBPF学习笔记
  • point on Istio中的透明代理问题
  • G on Istio中的透明代理问题
  • 绿色记忆:Go语言单元测试和仿冒 on Ginkgo学习笔记
  • point on Istio中的透明代理问题
  • 【Maven】maven插件开发实战 – IT汇 on Maven插件开发
  • chenlx on eBPF学习笔记
  • Alex on eBPF学习笔记
  • CFC4N on eBPF学习笔记
  • 李运田 on 念爷爷
  • yongman on 记录一次KeyDB缓慢的定位过程
  • Alex on Istio中的透明代理问题
  • will on Istio中的透明代理问题
  • will on Istio中的透明代理问题
  • haolipeng on 基于本地gRPC的Go插件系统
  • 吴杰 on 基于C/C++的WebSocket库
©2005-2025 Gmem.cc | Powered by WordPress | 京ICP备18007345号-2