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

`groovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types: (java.lang.String) values: [https://maven.aliyun.com/repository/google]` 错误表明在 Groovy 代码里尝试调用 `java.lang.String` 类中不存在的 `call` 方法,且传递了一个 `java.lang.String` 类型的参数。以下是可能的解决办法: ### 检查代码中调用的位置 要保证在代码里没有错误地对字符串调用 `call` 方法。比如: ```groovy def str = "https://maven.aliyun.com/repository/google" // 错误调用 // str.call("https://maven.aliyun.com/repository/google") // 正确使用字符串的方式 println(str) ``` ### 确认变量类型 要确保变量的类型符合预期,防止意外将字符串当作可调用对象。例如,要是代码里有闭包和字符串混淆的情况: ```groovy // 正确使用闭包 def closure = { param -> println("Called with: $param") } closure("https://maven.aliyun.com/repository/google") // 字符串不能这样调用 def str = "https://maven.aliyun.com/repository/google" // str("https://maven.aliyun.com/repository/google") // 会报错 ``` ### 检查第三方库或框架 有时候,第三方库或者框架可能会对字符串进行一些扩展,从而引发这个错误。要检查是否有这样的情况,并确保使用这些库时遵循其正确的使用方式。 ### 调试输出变量类型 在代码里添加调试信息,输出变量的类型,从而确认变量类型是否符合预期: ```groovy def someVar = "https://maven.aliyun.com/repository/google" println(someVar.getClass()) // 输出变量类型,确保是预期的类型 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值