Thursday, October 7, 2010

More on Groovy-Eclipse and Maven

I've had a few requests for the source code for the Groovy-Eclipse integration for maven, as well as a sample project that uses it. You can get both the sample project and groovy-eclipse compiler plugin for maven.

They are packaged as two m2eclipse projects, and it is recommended (although not necessary) to import them into Eclipse to use them.

The groovy-eclipse-compiler project contains the compiler integration. It is a single Java class that calls into the Groovy-enhanced JDT compiler. This maven plugin uses plexus to hook into maven's compiler plugin.

The groovy-eclipse-maven-tests project is a very simple maven project that has a few Groovy and Java classes that interact with each other. If you want to create your own project using the Groovy-Eclipse maven integration, I would recommend starting with this sample project.

Please let me know if you have any problems.

28 comments:

  1. Hi Andrew,

    Thanks so much for the example projects.

    As I was importing them I realized that I need to add the SpringSource snapshots plugins repository to the groovy-eclipse-compiler project. Otherwise I get a missing dependency for the groovy-eclipse-batch jar.

    ReplyDelete
  2. To clean up the build-helper-maven-plugin config a bit, one should probably use this config:


    org.codehaus.mojo
    build-helper-maven-plugin
    1.5


    add-source
    generate-sources

    add-source



    src/main/groovy




    add-test-source
    generate-test-sources

    add-test-source



    src/test/groovy






    Stefan

    ReplyDelete
  3. the xml fragment was destroyed, sorry. Hopefully you see what i mean...

    Stefan

    ReplyDelete
  4. Stefan, if you send me the xml directly, I'll clean it up and post it.

    andrew at eisenberg dot as

    ReplyDelete
  5. Thanks. Finally got around to cleaning this up.

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>1.5</version>
      <executions>
        <execution>
          <id>add-source</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>add-source</goal>
          </goals>
          <configuration>
            <sources>
              <source>src/main/groovy</source>
            </sources>
          </configuration>
        </execution>
        <execution>
          <id>add-test-source</id>
          <phase>generate-test-sources</phase>
          <goals>
            <goal>add-test-source</goal>
          </goals>
          <configuration>
            <sources>
              <source>src/test/groovy</source>
            </sources>
          </configuration>
        </execution>
      </executions>
    </plugin>

    I'm looking to see if I can post your archetype on the springsource maven repo.

    ReplyDelete
  6. A couple of questions:

    1. With m2eclipse, is it possible to get eclipse to add a classpathentry with src/test/groovy so that you can select "Update Project Configuration" and this will be performed? I noticed that build-helper-maven-plugin will help from the cli, but not inside Eclipse. Also, your example project doesn't have a src/test/groovy folder.

    2. How will this project effect GMaven? Is it intended to replace it?

    3. Are you planning on releasing this plugin with future versions of Groovy, eg., 1.7.5?

    Thanks for your great work on this!

    ReplyDelete
  7. Apologies for not getting back to you sooner...

    1. This *should* work. I haven't had a chance to try this out recently. Here is a blog post that may be useful:
    http://www.sonatype.com/people/2008/05/adding-additional-source-folders-to-your-maven-build/#more-615

    I'll see if I can get this working, though.

    In general, I think it would be better to simply use the src/main/java and src/test/java for your groovy code since that would simplify your pom.xml configuration.

    2. I am not involved with the gmaven project. GMaven does provide some benefit that this approach does not yet provide. For example, with GMaven, you can choose your own compiler level.

    3. Yes. I have been busy with other projects, but I plan on doing this soon.

    ReplyDelete
  8. Nice!

    Why is http://maven.springframework.org/snapshot not browsable, via Firefox for example?

    Also, it looks like that repository has release artifacts (ie: groovy-eclipse-batch) as well as snapshot artifacts (ie: groovy-eclipse-compiler), yet the URL includes the word "snapshot".

    Hope this helps.

    ReplyDelete
  9. @Adam,

    Thanks. This repository is hosted on Amazon S3, which does not allow directory browsing. There's not much I can do about that.

    And regarding the messed up repository folders...I am learning my way around maven as I do this project (and previous commenters have already helped me out).

    I just uploaded a new snapshot with a new version, which will hopefully make things easier to maintain from here.

    The new version is 0.5.1-SNAPSHOT. Updating your pom.xml to use this plugin dependency should work and fix some problems with AST transforms not being applied:



    <dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-eclipse-compiler</artifactId>
    <version>0.5.1-SNAPSHOT</version>
    </dependency>

    ReplyDelete
  10. http://maven.springframework.org/snapshot/org/codehaus/groovy/groovy-eclipse-compiler/0.5.1-SNAPSHOT/groovy-eclipse-compiler-0.5.1-SNAPSHOT.pom

    has a bad checksum:

    Downloading: http://maven.springframework.org/snapshot/org/codehaus/groovy/groovy-eclipse-compiler/0.5.1-SNAPSHOT/groovy-eclipse-compiler-0.5.1-SNAPSHOT.pom
    1K downloaded (groovy-eclipse-compiler-0.5.1-SNAPSHOT.pom)
    [WARNING] *** CHECKSUM FAILED - Error retrieving checksum file for org/codehaus/groovy/groovy-eclipse-compiler/0.5.1-SNAPSHOT/groovy-eclipse-compiler-0.5.1-SNAPSHOT.pom - IGNORING

    ReplyDelete
  11. I have not included any checksums with the artifacts. Is this lack preventing you from doing your work?

    ReplyDelete
  12. Nope, just thought I would give you a heads up.

    Also, how can one know what version of groovy the plugin is built with on an ongoing basis?

    Thanks again!

    ReplyDelete
  13. Thanks for the heads up. It is definitely something that we plan on fixing. At this point, we are unsure how we will release this maven plugin officially.

    When we do, the release versions will likely be in step with Groovy-Eclipse (currently at 2.1.0 and using Groovy 1.7.5, which is the version that the current release is based on).

    ReplyDelete
  14. Any news on release plans for that plugin?

    Tried with maven 3.0.1 and it works like a charm :-)

    cant wait for a release ..

    ReplyDelete
  15. Glad that it is working for you. I've been stalled with other projects for a bit and I hope to be able to return to this soon.

    ReplyDelete
  16. Is the source for v0.5.1-SNAPSHOT of the plugin available?

    Is there any way to configure the groovy-eclipse-compiler in the pom? e.g. to not display warning messages etc.

    ReplyDelete
  17. Are you looking for the source of the compiler code, the source of the other libraries that it uses, or the source of the groovy compiler that it uses internally? And are you looking to have the source code available through your maven project, or do you just want to have a look at it on the side?

    I'd be happy to make all the source code available (or tell you where you can find it) since everything is open source. But, making the source available as a maven artifact would be technically difficult.

    As for configuring, all of the standard JDT compiler options are recognized for compiling Java code. You can see a summary here:
    http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_compile.htm

    The groovy compilation will respect some of these options, but not others (I'm not exactly sure which, but a little bit of trial and error would tell you).

    If there is an option that you'd like to have, but don't see, I can probably do something about that.

    ReplyDelete
  18. At this point i am just trying to pass the "-nowarn" arg to the compiler.

    When i cracked open the source for the 0.0.1-SNAPSHOT of the GroovyEclipseCompiler I saw the full list of options the compiler supports -- thanks for the javadoc!

    However am having a little trouble passing the "-nowarn" arg to the groovy-eclipse compiler.

    When i do the compiler appears to error out and at some point prints out "unrecognized option : --nowarn". I am not sure what if the extra "-" is an artifact of the message or if something is appending the additional "-" before the compile can execute. I wanted to try and hunt it down in the source.

    I found the compiler source at https://svn.codehaus.org/groovy/eclipse.

    What version of the compiler does groovy-eclipse-batch 0.5.1-SNAPSHOT use? Is it just a repacked snapshot of https://svn.codehaus.org/groovy/eclipse trunk?

    ReplyDelete
  19. Yes, the compiler version is just a packaged snapshot of the groovy-eclipse compiler. I forgot what version it is, but it is a couple of months old.

    Can you try passing nowarn without a preceeding '-'?

    ReplyDelete
  20. Unfortunately that results in:

    Unrecognized option : "null"

    ReplyDelete
  21. BTW - i am working under the assumption that this is user error.

    ReplyDelete
  22. It may not be a user error. I'm probably not properly propagating all of the compiler options to the compiler. I'm trying to see if there is a workaround.

    ReplyDelete
  23. Can you post the snippet of your pom.xml where you use -nowarn?

    ReplyDelete
  24. I have to run out to pick up my children ... but it looks like it is the following lines from the compiler plugin:

    args.add("-" + entry.getKey());
    args.add("\"" + entry.getValue() + "\"");

    maven is already passing in the -nowarn option with a "-". the first line adds another "-" - turning "-nowarn" into "--nowarn". the second line then adds a String to args because the "-nowarn" option doesn't have a corresponding value.

    Fixing this in the 0.0.1-SNAPSHOT source cleared up the issue.

    If you could point me to the svn repo for the plugin I will submit a patch for v0.5.1-SNAPSHOT of the groovy-eclipse-plugin.

    THANKS FOR YOUR HELP!

    ReplyDelete
  25. I haven't checked this code into svn yet, but you can send the patch over to me and I'll include it and get a new snapshot out in the next few days.

    Thanks for looking into this.

    ReplyDelete
  26. http://docs.codehaus.org/download/attachments/182255869/groovy-eclipse-for-maven.zip link is 404

    ReplyDelete
  27. Where did that link come from? If you are looking to use the groovy-eclipse compiler for maven, see the docs here: http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven

    ReplyDelete