Groovy And Maven2 In Action


I wanted to publish all of the emails I shared on the Groovy mailing list on this site and eventually I hope to get around to that. However, time is precious so I'm fast fowarding to what I've done so far with Groovy Maven. (I'll be referring to the use of Groovy and Maven2 collectively as Groovy Maven because Maven is Groovy. I don't mean that the two are one in the same but that one gets a Groovy feeling when using Maven, so Maven then becomes a groovy technology. That's not to say that the groovy feeling one experiences when using Maven results in the complete enveloping of the Maven community by the Groovy community but that Maven starts to feel real groovy and… aw shoot! I'm off topic and lets just get on with the flipping story already!) I mentioned my desire to write a GUI (…that's pronounced gooey for all of you non-technical people and it referrs to a Graphical User Interface. My apologies but I have to occasionally simplify things as my family and possibly my neighbor may be reading along since I've been bragging about this new blog thing around town…) that can run an arbitrary XSL transform over an XML file using Groovy. (Hey non-techies! The XSL/XML stuff are programming dialects that have absolutely nothing to do with the discussion here and I'm just trying to sound smart while simultaneously confusing the heck outta you thereby bolstering my self esteem above my percieved measure of your intellectual abilities. Are you still with me? Good! Let's move on…) The end result is what I have so far today. I've completed a Groovy project managed by Maven that displays a window where you can select two files from your hard drive (one being an XSL file and one being an XML file) and run a conversion. It's really basic stuff.

The challenge:

I started having fun at first because with Groovy (being an interpreted language and all) I didn't have to run a compiler (one of those things that turn programming laguage code into machine code, ones and zeros) after each edit. I would make a change and re-run the source code. To do this I set up what amounts to a macro in Idea. (Idea is my development enviornment of choice, the thing I use to compose my programming logic.) In Idea's external tools setting I created a command to run "groovy" (the program that interprets Groovy source code) and give it the name of the selected file as well as the classpath of the project (…bye bye non-techies! at this point things are going to get really thick and I can't continue to expound each term without losing my entire audience, all three of my readers…). I also give it the source path of the selected file. Why? Because, as I understand it, the groovy classloader considers both .groovy and .class files when resolving references. What that means is it will load any .groovy file as it needs as if the file had been compiled. It's kinda neat-o. (Note to self: "neat-o" is the dumbest word you could use in the prior sentence, remove when you get a minute.) That's where my trouble began. I could not get that feature to work. As soon as I add a second .groovy file to my project referenced by the first one I started getting "Cannot resolve symbol…" errors. I jumped, at this point, into the Maven ship for salvation.

If you can't run it, compile it…

After seeing enough "symbol not resolvable" errors I prepared my Maven ".pom" file for processing by the M2 engines. It looks like this:


<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.gbg.neware.tools.xsl.runner</groupId>
    <artifactId>xsl-runner</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Neware XSL invoker</name>
    <url>http://www.gbg.com</url&gt;
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>groovy</groupId>
            <artifactId>groovy-all-1.0-jsr</artifactId>
            <version>05</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <configuration>
                            <tasks>
                                <taskdef name="groovyc"
                                    classname="org.codehaus.groovy.ant.Groovyc">
                                    <classpath refid="maven.compile.classpath"/>
                                </taskdef>
                                <groovyc destdir="${project.build.outputDirectory}"
                                    srcdir="${basedir}/src/main/groovy/" listfiles="true">
                                    <classpath refid="maven.compile.classpath"/>
                                </groovyc>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <configuration>
                            <tasks>
                                <taskdef name="groovyc"
                                    classname="org.codehaus.groovy.ant.Groovyc">
                                    <classpath refid="maven.compile.classpath"/>
                                </taskdef>
                                <mkdir dir="${project.build.testOutputDirectory}"/>
                                <groovyc destdir="${project.build.testOutputDirectory}"
                                    srcdir="${basedir}/src/test/groovy/" listfiles="true">
                                    <classpath refid="maven.test.classpath"/>
                                </groovyc>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Using the Antrun plugin I could do anything that I could do in an Ant build. In this case I chose to invoke groovyc at both the compile and test-compile build phases. What are build phases? Well I don't want to re-iterate too much Maven documentation but I'll summarize. Maven2, being a build system and all, operates on a "build lifecycle. This is the series of steps required to acheive a certain goal. In Ant terms it would be the equivalent of a predefined target with predefined dependencies. Maven does the work of figuring out which steps need to be invoked when by defining standard build life cycles. (To be more accurate, someone else who may or may not be part of the M2 project does this work for you, either way the work is done so why don't you appreciate it already!) The "life-cycle" is broken into phases (goals, or Ant-like targets) where work is done. Alls you haff to do (don't you just hate when some techie beings a sentence with, "alls you haff tuh do…"? ) is insert extra stuff to happen at any one of those phases. It's called binding. So to compile brand new Foo language with the Bar compiler you find or create a Bar plugin and tell it to execute it's work at the compile phase and when you run your build normally the work is automagically done at the right time.

How do we build with M2?

This brings me to my next important topic. How do you build with Maven2 (A.K.A. M2)? Maven is really slick about doing things for you. there typically is no build logic in some XML file. Alls you haff tuh do is tell Maven what you're trying to do and it will figure it out for you. If you just want to run your unit test you run "mvn test" on the command line. It will do everything it needs to do, (source code generation, copying properites files and filtering them, compiling source and test source, etc.) prior to get into a state where tests can be run then it will run them for you. If you're finished with your project and wish to depoly it you run "mvn deploy" and it will run everything it needs to get your compiled project out the door. So I started to use "mvn install" (which installs your project into your local maven repository located on your hard drive) just so I can generate a Jar file that I could run. That's when I learned that I didn't know how to generate a manifest. After asking on the Maven mailing list someone took pity and helped me out. My resulting pom file looks like this:


<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.gbg.neware.tools.xsl.runner</groupId>
    <artifactId>xsl-runner</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Neware XSL invoker</name>
    <url>http://www.gbg.com</url&gt;
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>groovy</groupId>
            <artifactId>groovy-all-1.0-jsr</artifactId>
            <version>05</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <configuration>
                            <tasks>
                                <taskdef name="groovyc"
                                    classname="org.codehaus.groovy.ant.Groovyc">
                                    <classpath refid="maven.compile.classpath"/>
                                </taskdef>
                                <groovyc destdir="${project.build.outputDirectory}"
                                    srcdir="${basedir}/src/main/groovy/" listfiles="true">
                                    <classpath refid="maven.compile.classpath"/>
                                </groovyc>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <configuration>
                            <tasks>
                                <taskdef name="groovyc"
                                    classname="org.codehaus.groovy.ant.Groovyc">
                                    <classpath refid="maven.compile.classpath"/>
                                </taskdef>
                                <mkdir dir="${project.build.testOutputDirectory}"/>
                                <groovyc destdir="${project.build.testOutputDirectory}"
                                    srcdir="${basedir}/src/test/groovy/" listfiles="true">
                                    <classpath refid="maven.test.classpath"/>
                                </groovyc>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.gbg.neware.tools.xsl.runner.Gui</mainClass>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

And now I can build and install my little GUI then run it from the command line. Stay tuned and I'll fill in more of the gory details of how I coded the GUI in the first place.

Thanx for reading and I'll holla' at'cha… (holla is ebonics representing the act of hollering at a friend, family member or co-worker. Hollering is a friendly way of greeting someone. It can also be used as a salutation in some contexts.)

22 Comments

  1. Mel

    Hi Cliff,

    I’ve just found your very nice blog and tried to integrate groovy into maven,too.
    The compilation works well, but the surefire plugin ignores all my Groovy-Tests unter /target/test-classes.
    How did you solve this problem?

    Cheers

  2. The surefire plugin looks for files under src/main/java/ that either start with “Test” or end with “Test.java” by default and it uses that to create a test suite. Since your test classes do not go with the naming convention, then you must configure the surefire plugin to include your tests. Also considering that all test patterns are relative to the default Java source folder you have to tell Maven to look in a different base folder to find Groovy test source. Considering your Groovy test source is under src/main/groovy you pom would look like this:


      <build>
        <testSourceDirectory>src/main/groovy</testSourceDirectory>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <includes>
                <include>**Test.groovy</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build>

    Now, to be honest, I’ve never tried this but it should work. I’ve only done a couple of Groovy-Maven2 projects and I have not even considered using Surefire to run my tests. In other words my tests are not integrated into my build. (Naughty naughty!) I typically use my IDE to run my unit tests in bulk (in my two projects I have a net total of 3-4 unit tests) and use Maven to build and distribute. Obviously that is not good practice so I’ll tell you what. I’ll post another topic on complete Groovy-Maven2 integration and make sure I cover all of the good stuff. Thanx for the comment! I love it when people show me the points I’ve missed.

    I’ll holla…

  3. Mel

    Hi Cliff,

    thanks for your very fast response.

    I set the file pattern to “**Test.groovy”, but it doesn’t work. Probably the problem is that the surefire plugin shouldn’t look for source- but class-files. In maven1 there is the maven.test.search.classdir property to change this behavior. Unfortunately it does not seem exist in maven2.

    I’m looking forward to Groovy-Maven2 integration blog.

  4. Fast response? I’ve been behind on my blog for some time now. Your welcome just the same. Did you add your Groovy test source as a test source directory? I’ll be working on a project today where I’ll try the same thing. I’ll let you know how it turns out.

  5. Peter

    The Maven 2 surefire plugin seems to look automatically in the test classes dir, so setting the test source directory to “src/test/groovy” should have no effect. I got this working for one of my groovy unit tests by replacing the “**Test.groovy” pattern with “**/*Test.class”. This is because surefire automatically replaces “*.java” patterns with “*.class” before scanning the test classes directory – it doesn’t do the same for “*.groovy” patterns!

    It’s an ugly hack, but it seems to work.

  6. Peter

    Ooops – forgot to mention that your test methods must be declared with return type “void” – any “def test…()” methods will be ignored.

  7. helloy

  8. Andreas Guther

    For building Groovy with Maven besides using the very verbose antrun plug-in have a look at http://mojo.codehaus.org/groovy/

  9. centers
    centers

  10. buy
    buy

  11. sell
    sell

  12. Sorry, I’m busy at the moment netherlands preteen pics =-[[[

  13. Do you have any exams coming up? index of files pedo stories
    :-PP

  14. Go travelling Nn Models Young
    8-((

  15. What’s your number? http://sibuasyfe.de.tl tiny nymphet pics
    Great clip..super hot chick with huge tits, very into the sex. and huge cocks that shoot great loads on her hot face.

  16. I work here http://ojatykapuboci.de.tl hin models any ladies that would let me eat them out after getting filled up please let me know i travel alot and always have a room

  17. Is this a temporary or permanent position? http://fagufeecyjym.de.tl little teen movies Could be the fantasy of the girl like Lautorberg suggests. Also the fantasy of many a guy to see it

  18. I’d like to take the job http://ohyjykeak.de.tl nude bbs pics great vid. were pretty picky about our porn and we have to say this is the best weve seen in a while. the guy was sexy and knew what he was doing so he didnt look like a jerk, the girl was hot as all get out and the MILF took control of the situation keeping the positions fresh and the action hot. the three in the vid had GREAT chemistry and really seem to enjoy their work! favs list for sure!

  19. I’ve got a full-time job http://earetiyp.de.tl pettiet teen model she is so hot i cummed everywhere any girls wanna have some fun inbox me x x x x x x x x x

  20. Best Site good looking http://ojyedamekub.de.tl image imgboard preteen NOW THAT WAS WORTH WATCHING WITH OUT FAST FORWARDING..DAMN YOU GUYS SHOULD BE AT MY SWINGERS PARTIES IN MIAMI BEACH..

  21. agregoraslittey741

    wow! super!

Trackbacks

  1. Can’t see nothing but the source code » Blog Archive » And the answer was JUnit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 250 other followers

%d bloggers like this: