<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Groovy &#8211; J O H N R A . M E</title>
	<atom:link href="/category/java/groovy/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>...yet another musings of a techie</description>
	<lastBuildDate>Thu, 11 Apr 2024 23:14:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>
	<item>
		<title>Gradle Plug-in: WSDL to Java Code Generation</title>
		<link>/2014/03/08/gradle-plug-in-wsdl-to-java-code-generation/</link>
		
		<dc:creator><![CDATA[John Ra]]></dc:creator>
		<pubDate>Sat, 08 Mar 2014 14:39:38 +0000</pubDate>
				<category><![CDATA[Gradle]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Development]]></category>
		<guid isPermaLink="false">http://blog.jstrgames.com/?p=373</guid>

					<description><![CDATA[It didn&#8217;t take long for me to fall in love with Gradle. Once I was introduced to it, I never looked back. It was the same with Ant as I transitioned from Make. But, Gradle is different. I always felt building was necessary evil. It was often done way after first code was ever written. This isn&#8217;t true with Gradle. Before any code is written, I start every project by setting up my Gradle build. I actually look forward to adding more features to my build. How crazy is that?! WSDL to JavaRecently, I published one of my old SharePoint&#8230;]]></description>
										<content:encoded><![CDATA[<p>It didn&#8217;t take long for me to fall in love with <a href="http://www.gradle.org/">Gradle</a>. Once I was introduced to it, I never looked back. It was the same with <a href="http://ant.apache.org/">Ant</a> as I transitioned from <a href="https://www.gnu.org/software/make/">Make</a>. But, Gradle is different. I always felt building was necessary evil. It was often done way after first code was ever written. This isn&#8217;t true with Gradle. Before any code is written, I start every project by setting up my Gradle build. I actually look forward to adding more features to my build. How crazy is that?!</p>
<p><strong>WSDL to Java</strong><br />Recently, I published one of my old <a href="https://github.com/johnra74/sharepoint-api">SharePoint API</a> to GitHub. Originally, I had written the build script using Ant for this project. One of the task I had was to take the SharePoint List WSDL and generate the corresponding java soap client classes. This is fairly trivial task in Ant &#8211; in fact, axis already provides <a href="http://axis.apache.org/axis/java/ant/ant.html">an ant task</a>. Sure, I could google for Gradle plug-in for WSDL to Java but I wanted to use this opportunity to learn to write my own plug-in. I&#8217;ve also been looking to <a href="/2013/07/25/thats-groovy/">write more Groovy code</a>.</p>
<p><strong>Creating a Custom Gradle Task</strong><br /><a href="/wp-content/uploads/2014/03/gradle1.png"><img decoding="async" class="alignright size-thumbnail wp-image-375" src="/wp-content/uploads/2014/03/gradle1-150x150.png" alt="gradle buildSrc" width="150" height="150" /></a>All custom Gradle tasks resides under buildSrc at the root of your project. If you are familiar with Gradle multiproject then you will notice that buildSrc is structured like a subproject. In fact, it is a subproject. Since I will be writing my custom task using Groovy, my source file will reside under src/main/groovy. If you wanted to write your custom task in Java, you would create it under src/main/java.</p>
<p>Since my custom task is merely a wrapper for Axis2 class org.apache.axis2.wsdl.WSDL2Java, the build.gradle file will contain the Axis2 dependencies. Note, if your custom task does not require any dependencies besides core java, then you do not need to define the build.gradle file. Here&#8217;s my build.gradle file:</p>


<pre class="wp-block-code"><code>repositories {
  mavenCentral()
}

dependencies {
  compile 'org.apache.axis2:axis2:1.6.2'
  compile 'org.apache.axis2:axis2-xmlbeans:1.6.2'
  compile 'org.apache.xmlbeans:xmlbeans:2.6.0'
}</code></pre>



<p>Once the build.gradle has been defined, I create my custom task in Groovy &#8211; WSDL2Java.groovy. The below Groovy class is a simple implementation. As you can see, there are no validation checks. It assumes all properties will be set.</p>



<pre class="wp-block-code"><code>import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
// since this groovy class has the same name as the axis2 class, alias axis2 class to CodeGen
import org.apache.axis2.wsdl.WSDL2Java as CodeGen

class WSDL2Java extends DefaultTask {
  // source location of the WSDL file
  String wsdlfilename
  // specify the binding type - xmlbeans, adb, or jibx
  String databindingName
  // target location where java classes should be created under
  String targetSourceFolderLocation
  // base package name
  String packageName

  @TaskAction
  def generateJava() {
    String&#91;] args =
      &#91;
        "-o", targetSourceFolderLocation,
        "-p", packageName,
        "-d", databindingName,
        "-or",
        "-uri", wsdlfilename
      ]
    CodeGen.main(args)
  }
}</code></pre>



<p>Here&#8217;s how this custom task is called within the project. First, I define a task called &#8220;genJavaFromWSDL&#8221; with type WSDL2Java &#8211; the Groovy class name. Within this custom task, I set all properties. Then, I update the dependency of java compile task to this custom task. This will ensure the java soap client is created before build is trigger.</p>



<pre class="wp-block-code"><code>// define the WSDL2Java task
task genJavaFromWSDL(type: WSDL2Java) {
  wsdlfilename= "$buildDir/../src/main/wsdl/Lists.SP2013.wsdl"
  databindingName='xmlbeans'
  targetSourceFolderLocation= "$buildDir/generated/wsdl/main"
  packageName='com.microsoft.sharepoint'
}

// add prior hook before compile
compileJava.dependsOn genJavaFromWSDL

// add generated wsdl java classes to source dir
sourceSets.main.java.srcDir "$buildDir/generated/wsdl/main/src"
sourceSets.main.java.srcDir "$buildDir/generated/wsdl/main/resources"</code></pre>



<div class="wp-block-image"><figure class="alignleft"><a href="/wp-content/uploads/2014/03/gradle2.png"><img decoding="async" src="/wp-content/uploads/2014/03/gradle2.png" alt="Gradle Project" class="wp-image-378"/></a></figure></div>



<p>Finally, I add the generated source and resources to the Groovy sourceSet so that the generated sources are properly compiled. It also allows them to appear under Eclipse.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>That&#8217;s Groovy!</title>
		<link>/2013/07/25/thats-groovy/</link>
		
		<dc:creator><![CDATA[John Ra]]></dc:creator>
		<pubDate>Thu, 25 Jul 2013 21:51:45 +0000</pubDate>
				<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Scripting]]></category>
		<guid isPermaLink="false">http://blog.jstrgames.com/?p=323</guid>

					<description><![CDATA[Ask me a month ago regarding my choice of scripting language, my answer would have been Perl or, if it&#8217;s a trivial use case, Bash. That&#8217;s no longer the case. Now, I am eager to flex my Groovy knowledge. Why Groovy? Consider the use case of executing a system command and redirecting the output to be processed by your program. With Perl, this is a trivial task: just use a backquote to perform your system command. Perl will assign the result of your standard out to the left-hand variable. In Groovy, it&#8217;s just as easy. Now, imagine having to perform&#8230;]]></description>
										<content:encoded><![CDATA[<p>Ask me a month ago regarding my choice of scripting language, my answer would have been Perl or, if it&#8217;s a trivial use case, Bash. That&#8217;s no longer the case. Now, I am eager to flex my Groovy knowledge.</p>
<p><strong>Why Groovy?</strong></p>
<p>Consider the use case of executing a system command and redirecting the output to be processed by your program. With Perl, this is a trivial task: just use a backquote to perform your system command. Perl will assign the result of your standard out to the left-hand variable.</p>


<pre class="wp-block-code"><code>$output = `ls -l`;
print $output, "\n";</code></pre>



<p>In Groovy, it&#8217;s just as easy.</p>



<pre class="wp-block-code"><code>def process = "ls -l".execute()
println "${process.text}"</code></pre>



<p>Now, imagine having to perform this in Java:</p>



<pre class="wp-block-code"><code>try {
  StringBuffer stdout = new StringBuffer();
  Process p = Runtime.getRuntime().exec( "ls -l" );

  BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()) );
  while ((line = in.readLine()) != null) {
    stdout.append(line);
  }
  in.close();
}
catch (Exception e) {
  // ...
}</code></pre>



<p>Did I get your attention? I hope so. Let&#8217;s consider list. I think Groovy is better at list than Perl. Take the use case of sorting a list of numbers. In Perl, it would look akin to this.</p>



<pre class="wp-block-code"><code>@list = (1, 23, 4, 6, 12, 5, 36);
@sortedlist = sort {$a &lt;=> $b} @list;
print "@sortedlist ";</code></pre>



<p>In Groovy, it&#8217;s even shorter</p>



<pre class="wp-block-code"><code>def list = &#91;1, 23, 4, 6, 12, 5, 26]
println list.sort()</code></pre>



<p>Lists are easier to work with in Groovy. If you want to print a sub-range, it&#8217;s &#8220;println list[1..3]&#8221;. If you want the last 3 items in the array in reverse order, it&#8217;s &#8220;println list[-3..-1]&#8221;. Negative index tell Groovy to retrieve the item from tail and work its way back.</p>



<p>If you&#8217;re a Java developer, how can you not love Groovy.</p>



<p><strong>Like Java, Love Groovy</strong></p>



<p>Groovy is built on Java. It brings dynamic typing and the benefits of dynamic language to the world of Java but still provides static typing when needed. Because it&#8217;s built on Java, Groovy has access to all the Java API available in your classpath. By default, the following classes are exposed to Groovy:</p>



<ul><li>java.io.*</li><li>java.lang.*</li><li>java.math.BigDecimal</li><li>java.math.BigInteger</li><li>java.net.*</li><li>java.util.*</li></ul>



<p>If you need a class not imported by default, if it&#8217;s in the classpath, Groovy can use them. You can import them in or use the fully qualifying classname:</p>



<pre class="wp-block-code"><code>println java.sql.Date.valueOf("2013-12-31").getTime()</code></pre>



<p>As stated earlier, all of Java libraries are at your disposal. Short of sounding like an infomercial, that&#8217;s not all! You can compile Groovy program and it will become accessible from Java. Consider having to write a simple POJO. Sure, you can use most IDEs to generate your getters and setters, but with Groovy, this can be simplified.</p>



<pre class="wp-block-code"><code>package com.jstrgames.groovy.example

class Book {
  String title
  String author

  String toString() { "${title} ${author}" }
}</code></pre>



<p>Once compiled by Groovy compiler, com.jstrgames.groovy.example.Book can be accessed from any Java program. It will also automatically create all your getters and setters for each property: getTitle(), setTitle(), getAuthor(), and setAuthor(). If you want to use this class in Groovy, you can directly access the properties:</p>



<pre class="wp-block-code"><code>def book = new Book( title: "Groovy by Action", author: "Dierk Koenig" )
println book

def myBook = new Book()
myBook.title = "Tech Genre"
myBook.author = "John Ra"
print myBook</code></pre>



<p>You can clearly see an increased productivity building modules/application/APIs with Groovy. If you want to learn more, I highly recommend reading <em>Groovy by Action</em> by Dierk Koenig.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
