środa, 22 maja 2013

SetUp Groovy development with Maven

Groovy is an object-oriented programming language for Java Virtual Machine(JVM), provides dynamic programming features similar to Python, Ruby, Smalltalk, ... . The moust commont use of Groovy is scripting language and interacts freely with existing Java code and libraries.

Groovy is defined as an agile and dynamic programming/scripting language build for Java Virtual Machine. Groovy for Java developer it promises an almost-zero learning curve. Groovy is based on JVM and that is why it interoperates with existing Java infrastructure, libraries and framwork.

This is why in the past few years, Groovy has become popular for scripting and DSLs(Domain Specific Languages) in addition to its use in application development. The next advantage from using Groovy is that connected with Java, enhances developer productivity by reducing scaffolding code while providing built-in capability for unit and mock testing.

To start you adventure with Groovy lets generate Groovy simple project using Maven:

mvn archetype:generate / 
-DarchetypeGroupId=org.codehaus.groovy.maven.archetypes / 
-DarchetypeArtifactId=gmaven-archetype-basic

The completion of this operation has created a Groovy project with simple "Hello World!" Groovy application: file Example.groovy:

class Example
{
    def show() {
        println 'Hello World'
    }

  
Now look to POM file. It is configured in way that integrated the build lifecycle with the Maven Groovy plugin:

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



mvn compile - compile project
mvn test - run tests


The next think you can find in POM file are dependencies:
    
     <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy.maven.runtime</groupId>
            <artifactId>gmaven-runtime-1.6</artifactId>
            <version>1.0</version>
        </dependency>
       
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


The second you should already know:). The first listed here is the GMaven runtime, that is gmaven-runtime-1.6. 
With the help of the GMaven plugin, these two dependencies are integrated with Maven build lifecycle and the build workflow for the developer is achieved through Apache Maven.

It is one more think which is worth to mention. Groovy Shell provides the developer with a very convenient command-line shell to execute Groovy commands. The GMaven plugin provides for command-line access to the Groovy shell with the following command:

mvn groovy:shell 

If you like a GUI access to the shell run 

mvn groovy:console 
 

Brak komentarzy:

Prześlij komentarz