`

Integration of Groovy into Maven.

 
阅读更多

Building Groovy Projects

 

GMaven has kick-ass support for compiling Groovy projects with Maven!

 

Project Structure

Groovy projects are structured based on the Maven standard, notably adding the groovy sub-directory to the main and test source collections:

<project-root>/
  |
  +- pom.xml
  |
  +- src/
  |  |
  |  +- main/
  |  |  |
  |  |  +- groovy/ (source location for Groovy and optional Java sources)
  |  |  |
  |  +- test/
  |  |  |
  |  |  +- groovy/ (source location for Groovy and optional Java test sources)
  |  |  |
  ...

gmaven-archetype-basic Archetype

To help get Groovy projects started faster, you can use the gmaven-archetype-basic. This will create a new project with the basic POM configuration and some example classes to get you started quickly:

mvn archetype:generate -DarchetypeGroupId=org.codehaus.gmaven.archetypes -DarchetypeArtifactId=gmaven-archetype-basic -DarchetypeVersion=<VERSION>
TIP

Remember to specify archetypeVersion, otherwise mvn will prompt you for the archetype. To use a specific version of an archetype specify -DarchetypeVersion=<VERSION>.


TIP

If the command above does not work, it is likely that you have an outdated version of the archetype plugin in your local repository. Add "-U" to the mvn call in order to force updates.

The Maven Archetype Plugin will ask a few questions about your new project:

[INFO] [archetype:generate]
...
Define value for groupId: : org.mycompany.myproject
Define value for artifactId: : example-project
Define value for version: : 1.0-SNAPSHOT
Define value for package: : org.mycompany.myproject.example
Confirm properties configuration:
name: Example Project
groupId: org.mycompany.myproject
artifactId: example-project
version: 1.0-SNAPSHOT
package: org.mycompany.myproject.example
 Y: : y
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...
NOTE

Please ignore any ReferenceException warnings about ${...}, they are harmless.

The above example would have created the following project structure:

example-project
example-project/pom.xml
example-project/src
example-project/src/main
example-project/src/main/groovy
example-project/src/main/groovy/org
example-project/src/main/groovy/org/mycompany
example-project/src/main/groovy/org/mycompany/myproject
example-project/src/main/groovy/org/mycompany/myproject/example
example-project/src/main/groovy/org/mycompany/myproject/example/Example.groovy
example-project/src/main/groovy/org/mycompany/myproject/example/Helper.java
example-project/src/test
example-project/src/test/groovy
example-project/src/test/groovy/org
example-project/src/test/groovy/org/mycompany
example-project/src/test/groovy/org/mycompany/myproject
example-project/src/test/groovy/org/mycompany/myproject/example
example-project/src/test/groovy/org/mycompany/myproject/example/ExampleTest.groovy
example-project/src/test/groovy/org/mycompany/myproject/example/HelperTest.groovy

Compiling Sources

GMaven provides support to compile Groovy sources, which includes joint Java/Groovy compilation via stub-generation.

To enable compilation features configure the gmaven-plugin execute the stub-generation and compilation goals in your projects POM and define a dependency on the Groovy runtime your project requires (using the default version in this example:

<dependencies>
    <dependency>
        <groupId>org.codehaus.gmaven.runtime</groupId>
        <artifactId>gmaven-runtime-default</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>generateStubs</goal>
                        <goal>compile</goal>
                        <goal>generateTestStubs</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Source Selection

By default, all *.groovy files under src/main/groovy will be compiled into target/classes and all *.groovy files under src/test/groovy will be compiled into target/test-classes.

To use a more specific list of sources, specify one or more source filesets. When sources is specified the default location is not automatically included:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <sources>
                    <fileset>
                        <directory>${pom.basedir}/src/main/script</directory>
                        <includes>
                            <include>**/*.groovy</include>
                        </includes>
                    </fileset>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

Compilation Flags

Many of the options available in the Groovy compiler configuration can also be set:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <configuration>
        <debug>false</debug>
        <verbose>true</verbose>
        <stacktrace>true</stacktrace>
        <defaultScriptExtension>.groovy</defaultScriptExtension>
    </configuration>
</plugin>

For the full reference of available flags please see the plugin documentation for the groovy:compile goal.

Stub-Generation

Stub-generation creates minimal Java sources for each Groovy class preserving the class structure and Javadocs, but omitting implementation detail. This allows the standard Maven Compiler Plugin to be used to compile Java sources which have a dependency upon Groovy sources with out any additional POM configuration or magical hacks.

Stub class files are used for resolving classes at compile-time, at runtime the compiled Groovy classes will be used instead.

Stub source files are used for generating API documentation via the standard Maven Javadoc Plugin, other plugins which operate on source files (ex. Maven JXR Plugin) will also utilize these generated source files for reports.

Running Tests

Test execution works just like any normal Maven project using the Maven Surefire Plugin. Using the testCompile goal, the GMaven plugin will compile Groovy sources into Java class files, which the Surefire plugin will execute just like any other Java class.

Be sure to configure your project's to depend on the required Groovy runtime and the desired unit-testing library (using junit3 in this example):

<dependencies>
    <dependency>
        <groupId>org.codehaus.gmaven.runtime</groupId>
        <artifactId>gmaven-runtime-default</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Non-Groovy projects can still make use of Groovy for testing... which can make for less verbose test classes (smile)

groovy-jar Packaging

TODO

TODO

分享到:
评论

相关推荐

    apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本

    apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望大家多多下载,apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望大家多多下载,apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望...

    Groovy-3.0.jar

    Groovy jar包 3.0.

    groovy+maven+spring

    在IT行业中,构建高效、灵活的应用常常涉及到多种技术的整合,比如"groovy+maven+spring"这样的组合。这个组合提供了强大的动态脚本语言Groovy、项目管理工具Maven以及广泛应用的Spring框架的集成,使得开发过程更加...

    groovy-3.0.9-API文档-中文版.zip

    Maven坐标:org.codehaus.groovy:groovy:3.0.9; 标签:groovy、codehaus、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码...

    org.codehaus.groovy-2.9.0.xx-201407142235-e44-RELEASE-updatesite.zip

    标题 "org.codehaus.groovy-2.9.0.xx-201407142235-e44-RELEASE-updatesite.zip" 暗示了这是一个与Groovy编程语言相关的Eclipse插件的更新站点压缩包。这个版本号 "2.9.0" 表明这是该插件的2.9.0版,时间戳 ...

    maven配置java项目使用groovy做源码.zip

    在Java开发领域,Maven和Groovy是两个重要的工具,它们可以协同工作,提升开发效率和项目的可维护性。本项目结合了这两者,通过Maven配置,将Groovy用作Java项目的源码语言,实现了更灵活的代码编写。下面我们将深入...

    cucumber-groovy-1.1.1.zip

    总结以上信息,我们可以看到“cucumber-groovy-1.1.1.zip”提供了Cucumber的一个Groovy版本,用于BDD测试,而“sourceninja-maven.zip”是一个Maven插件,用于将项目与SourceNinja服务集成,以提升代码质量和团队...

    groovy-3.0.7.msi

    groovy

    1.MAVEN.docx

    - **Gradle**:Gradle 是一种基于 Groovy 的构建工具,特别适合 Android 开发。它结合了 Maven 和 Ant 的优点,提供了灵活的构建脚本语法以及强大的依赖管理功能。 #### 四、Maven 的安装与配置 ##### 4.1 下载 ...

    groovy-all.jar-生成JasperReport所要包含的包

    `groovy-all.jar`是Groovy库的一个集合,包含了Groovy运行时环境和所有必要的类库,使得开发者能够在Java项目中方便地使用Groovy的功能。 在Java开发中,`groovy-all.jar`是至关重要的,因为它允许你在Java应用程序...

    groovy-1.0.jar

    groovy-1.0.jar ireport jasperreport

    groovy-2.5.1-API文档-中文版.zip

    Maven坐标:org.codehaus.groovy:groovy:2.5.1; 标签:codehaus、groovy、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码...

    Packtpub.Groovy.for.Domain.Specific.Languages.Jun.2010.rar

    《Packtpub.Groovy.for.Domain.Specific.Languages.Jun.2010》这本书主要聚焦于Groovy语言在领域特定语言(DSLs)中的应用,这是2010年6月出版的一份专业资源。Groovy是一种动态、灵活的编程语言,它为Java平台带来...

    Flutter 出现Could not download groovy-all.jar (org.codehaus.groovy:groovy-all:2.4.15)

    在一次代码拉取中,出现了以下问题:Could not download groovy-all.jar (org.codehaus.groovy:groovy-all:2.4.15) 详细的报错信息如下: // 报错信息如下 Could not resolve all files for configuration ':jcore-...

    Apress.Beginning.Groovy.and.Grails.From.Novice.to.Professional.Jun.2008

    《Apress.Beginning.Groovy.and.Grails.From.Novice.to.Professional.Jun.2008》这本书深入浅出地介绍了Groovy语言和Grails框架,旨在帮助初学者快速掌握这两项技术并转化为专业人士。Groovy和Grails是Java生态中的...

    elasticsearch-lang-groovy-1.2.0.jar

    jar包,官方版本,自测可用

    apache-groovy-sdk-2.5.6.zip

    9. **Grape依赖管理**:Groovy的Grape特性允许自动下载和管理依赖,类似于Java的Maven或Gradle。 10. **Spock测试框架**:Groovy还催生了Spock,一个功能强大的BDD/TDD测试框架,其语法简洁易懂,非常适合Groovy和...

Global site tag (gtag.js) - Google Analytics