Thursday, 19 January 2012

Creating a dummy groovy/maven project and make it executable


Problem:
I like groovy.
The company uses maven and java.
The live servers don't have groovy installed.
So I downloaded the Intellij Idea community edition and got to work.

So here is a contrived solution that yields an executable jar file.
First go to some project folder (I was testing with Idea CE 11 so I used IdeaProjects):

bandit:IdeaProjects user$ pwd
/Users/user/IdeaProjects

Now use maven to create an example project:

bandit:IdeaProjects user$ mvn archetype:generate -DarchetypeGroupId=org.codehaus.gmaven.archetypes -DarchetypeArtifactId=gmaven-archetype-basic
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Interactive mode
[INFO] Archetype [org.codehaus.gmaven.archetypes:gmaven-archetype-basic:1.4] found in catalog remote
Define value for property 'groupId': : com.company
Define value for property 'artifactId': : example
Define value for property 'version': 1.0-SNAPSHOT: 
Define value for property 'package': com.company: 
[INFO] Using property: name = Example Project
Confirm properties configuration:
groupId: com.company
artifactId: example
version: 1.0-SNAPSHOT
package: com.company
name: Example Project
Y: y
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20 seconds
[INFO] Finished at: Thu Jan 19 16:31:24 EST 2012
[INFO] Final Memory: 12M/81M
[INFO] ------------------------------------------------------------------------

This will create a project called example:

bandit:IdeaProjects user$ cd example
bandit:example user$ find . -print
.
./pom.xml
./src
./src/main
./src/main/groovy
./src/main/groovy/com
./src/main/groovy/com/company
./src/main/groovy/com/company/Example.groovy
./src/main/groovy/com/company/Helper.java
./src/test
./src/test/groovy
./src/test/groovy/com
./src/test/groovy/com/company
./src/test/groovy/com/company/ExampleTest.groovy
./src/test/groovy/com/company/HelperTest.groovy

Ok. Now create a new project in Idea CE and do this:
1. use import project from external model and click next
2. choose maven and click next
3. choose the correct root directory and check import Maven projects automatically and click next
4. choose the com.company:example:1.0-SNAPSHOT maven project and click next
5. ignore the next panel as it's fine and click finish

Now you have the project open, edit the pom.xml.
It will look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Generated from archetype; please customize.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company</groupId>
    <artifactId>example</artifactId>
    <name>example project</name>
    <version>1.0-SNAPSHOT</version>

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

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

</project>

What we need to do is add some extras.

In the dependencies add this:

        <dependency>
            <groupId>org.codehaus.gmaven.runtime</groupId>
            <artifactId>gmaven-runtime-1.8</artifactId>
            <version>1.4</version>
        </dependency>

And in the plugins add this:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

Now open the Example.groovy in src/main/groovy/com.company.
It should look like this:

//
// Generated from archetype; please customize.
//

package com.company

/**
 * Example Groovy class.
 */
class Example {
    def show() {
        println 'Hello World'
    }
}

Add a main method:

//
// Generated from archetype; please customize.
//

package com.company

/**
 * Example Groovy class.
 */
class Example {
    public static void main(String[] args) {
        System.out.println("Working!");
    }
    def show() {
        println 'Hello World'
    }
}

Now go back to the command line and run a package:

bandit:example user$ mvn clean package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building example project
[INFO]    task-segment: [clean, package]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory /Users/user/IdeaProjects/example/target
[INFO] [groovy:generateStubs {execution: default}]
[INFO] Generated 1 Java stub
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/user/IdeaProjects/example/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /Users/user/IdeaProjects/example/target/classes
[INFO] [groovy:compile {execution: default}]
[INFO] Compiled 1 Groovy class
[INFO] [groovy:generateTestStubs {execution: default}]
[INFO] Generated 2 Java stubs
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/user/IdeaProjects/example/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /Users/user/IdeaProjects/example/target/test-classes
[INFO] [groovy:testCompile {execution: default}]
[INFO] Compiled 2 Groovy classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /Users/user/IdeaProjects/example/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.company.ExampleTest
Hello World
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.411 sec
Running com.company.HelperTest
Hello World
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.013 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: /Users/user/IdeaProjects/example/target/example-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Thu Jan 19 16:47:12 EST 2012
[INFO] Final Memory: 26M/81M
[INFO] ------------------------------------------------------------------------

And now we can run it:

bandit:example user$ java -cp target/example-1.0-SNAPSHOT.jar:/opt/local/share/java/groovy/lib/* com.company.Example
Working!

I should have set UTF-8 instead of MacRoman and also obviously I could have used maven to put the Main-Class in the manifest, but this is a contrived example.
Also obviously I have to ensure the groovy 1.8 jars are on the server...
Or...
I could jars-in-a-jar solution...

No comments:

Post a Comment