Search
Calendar
September 2017
S M T W T F S
« Sep    
 12
3456789
10111213141516
17181920212223
24252627282930
Your widget title
Archives

Posts Tagged ‘Groovy’

PostHeaderIcon How to compile both Java classes and Groovy scripts with Maven?

Case

Your project includes both Java classes and Groovy scripts. You would like to build all of them at the same time with Maven: this must be possible, because, after all, Groovy scripts are run on a Java Virtual Machine.

Solution

In your pom.xml, configure the maven-compiler-plugin as follows:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <compilerId>groovy-eclipse-compiler</compilerId>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>2.8.0-01</version>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>2.1.5-03</version>
                    </dependency>
                </dependencies>
            </plugin>

With such setup, default compiler (which cannot compile Groovy scripts parallelly of Java sources) will be replaced with Eclipse’s one (which can).

PostHeaderIcon (long tweet) Error injecting: org.codehaus.groovy.eclipse.compiler.GroovyEclipseCompiler

Case

On building a Groovy project with Maven, I got the following error:

Error injecting: org.codehaus.groovy.eclipse.compiler.GroovyEclipseCompiler
java.lang.NoClassDefFoundError: org/codehaus/plexus/compiler/CompilerResult

Quick fix

Downgrade maven-compiler-plugin to version 3.0 instead of branch 2.3.X.

PostHeaderIcon How to call external shell in Groovy?

Going on learning Groovy, I faced the following case: how to call an external shell (or MS-DOS) command from a Groovy script?

It is very easy. The following example show how to display the Subversion status of the current folder:

def proc = &quot;svn st&quot;
def sb = new StringBuffer()
proc.consumeProcessErrorStream(sb)

println proc.text + &quot;\n&quot; +  sb.toString()

PostHeaderIcon Error grabbing Grapes — [unresolved dependency: com.oracle#ojdbc14;10.2.0.4.0: not found]

I have learnt Groovy since the beginning of the week.
I had to import some classes from Oracle JDBC driver. As a newbie, I wrote the following annotation:

@Grab(group = 'com.oracle', module = 'ojdbc14', version = '10.2.0.4.0')

I got the following error:

Error grabbing Grapes -- [unresolved dependency: com.oracle#ojdbc14;10.2.0.4.0: not found]

To sum up, adding a dependency to Oracle driver raises two issues:

  • adding any external dependency
  • adding a dependency to any JDBC driver

Indeed, Grab requires a kind of Maven repository, and you can catch the link between the @Grab annotation below and classic Maven 2 <dependency> tag. As I understood, Grab instructions are closer to Ivy than to Maven system. In my situation, having no “Grab repo” (I don’t know how to call that), I had to download the jars from a Maven repo. To fix it, two operations are needed: hint at Maven (or Ivy) repo, and allow Groovy to access the network.

Regarding the second issue, it is answered by added a @GrabConfig annotation.
Therefore, to fix the original issue:

  • replace
    @Grab(group = 'com.oracle', module = 'ojdbc14', version = '10.2.0.4.0')

    with

    @GrabConfig(systemClassLoader=true)
    @GrabResolver(name='nameOfYourMavenRepo, root='http://url-of-your-maven-repo:port')
    @Grab(group = 'com.oracle', module = 'ojdbc14', version = '10.2.0.4.0')
    
  • at first run, if needed, hint at the proxy to allow Grab to download the Jars, eg:
    groovy -Dhttp.proxyHost=myProxyHost -Dhttp.proxyPort=8080

You may notice the needed JAR will be written in the $USER_HOME/.groovy/grapes folder.

PostHeaderIcon Filter too large files in Mule ESB

Case

You use a filter connector in Mule ESB 2.2.1, to perform any operation (let’s say: to send them on JMS, but this does not matter). You would like to filter files owing to their size, ie stop the handling of files larger than 10,000,000 bytes for instance.

We assume the files you handle are text files (not binary).

Solution

In your Mule XML config file, add the following block, using a short Groovy script:

<message-properties-transformer name="add-properties">
     <add-message-property key="fileSizeOK"
             value="#1" />
 </message-properties-transformer>

In the tag <file:inbound-endpoint>, add the attribute: transformer-refs="add-properties"
Now, add a tag <message-property-filter pattern="fileSizeOK=true"/>. Your outbound should then be similar to:

			<outbound>
                <filtering-router>
                    <jms:outbound-endpoint queue="lalou.jonathan.jms.queue"
						connector-ref="jmsConnector"/>
                    <message-property-filter pattern="fileSizeOK=true"/>
                </filtering-router>
                <forwarding-catch-all-strategy>
                    <file:outbound-endpoint path="/my/folder/"
                        connector-ref="fileConnector" outputPattern="error/#[header:originalFilename].too_big.#[function:dateStamp:yyyyMMdd_HHmmss_SSSSS].KO" />
                </forwarding-catch-all-strategy>
			</outbound>

Many thanks to Vincent N. for his help.