Thursday, February 14, 2008

Maven Archetype Upgraded

Finally, it is here. An upgraded of the Maven plugin, archetype, was just recently released. Well, according to JIRA, it was actually released last week, but just recently have I taken notice :).

One of the slams that Maven receives is related to the verbosity necessary in order to use the Archetype plugin to create new projects. In order to create a new simple java project using the older version of the Maven Archetype plugin, you had to do something similar to the following:

mvn archetype:create "-DgroupId=com.sagetech.example" "-DartifactId=dummyProject"

The above would create a simple project with the jar packaging type. If you wanted to do something a little more complex, like create a project of a different packaging type, (ejb, ear, web module), you had to specify even more properties to the archetype plugin, something similar to...

mvn archetype:create "-DgroupId=com.sagetech.example" "-DartifactId=dummyProjectWeb"
"-DarchetypeArtifactId="

Although the above approaches work without any issues, it still seemed to be a cold way for a developer to get started with an application. And it appears that I am not the only one who thought so.

With the recent release of the Archetype plugin, you don't have to specify any properties at all. Simply type the command:

mvn archetype:generate

...and during the course of the execution of the plugin, it will prompt you for the following information:

1.) Choose the archetype
2.) Choose the groupId
3.) Define the artifactId
4.) Define the version
5.) Define the package

A friend of mine has always given me a hard time about how verbose you have to be in order to create a java application using the Maven Archetype Plugin. I cannot wait until he reads this post...Dave Brondsema, this one is for you.... :)

Using Groovy...

In one of my earlier posts, I mentioned that I have really enjoyed my journey into Groovy, because I have been able to leverage it's power practically on the job. For this post, I wanted to give you a practical example of how I have been able to do just that.

I currently deal with large quantities of data. So much, in fact, that our project has stored procedures that will truncate tables because there is not enough tablespace in order to handle delete queries. This works fine if I want to delete all the records in a table, but what if I only want to delete a subset, let's say 50,000 of the multiple million records? With groovy, and it's built in SQL functionality, this becomes very easy to accomplish. Here is the complete program:


import groovy.sql.Sql

def sql = Sql.newInstance("${jdbc.url}", "${jdbc.userId}", "${jdbc.password}", "${jdbc.driver})
def query = "select customer_id from customer where customer_group_id = 11020"
sql.eachRow(query as String, {row -> sql.execute("delete from customer where customer_id = ?", [row.CUSTOMER_ID])})


The Sql object represents the connection to the database. The first thing we do is open the connection to the database using the newInstance method which takes in a valid jdbc url, db user id, db password, and jdbc driver.

Note: The JDBC Driver must exist on the classpath when executing this script.

The query variable holds the sql statement that will return me all of the id's of the customers that I would like to delete from the customer table.

Using the eachRow method on the Sql object, as each row is returned, I execute the anonymous in line closure...


{row -> sql.execute("delete from customer where customer_id = ?", [row.CUSTOMER_ID])}


Which will execute a delete statement for each record returned from the original query.

Running the application is just as simple as well...

groovy -cp .\lib\<> deleteCustomers.groovy


This is one of the many uses that I have found for this simple, yet powerful dynamic language. I hope to share more with you in the future.

Enjoy.

Tuesday, February 12, 2008

This year will be Groovy

One of the things that I wanted to accomplish this year, was to learn a new programming language. There are so many different languages that now are able to run on the JVM, that I didn't know exactly where I would focus my attention.

Originally, I started out with the idea that I wanted to learn Scala. One of the main reasons why I wanted to learn Scala is because it is a functional language. I have never worked with a functional language before. I was interested to see if solving problems in a functional manner, would inspire me to approach problems in Java differently. I downloaded it and started to go to the different tutorials online. To be honest, I was a little intimidated. The syntax is very different, and I don't think that I would be able to spend the amount of time necessary in order to get up to speed with this language.

The next language that I considered learning was JRuby. Of course, everyone has heard all of the hype around Ruby. However, I was more interested in learning more about JRuby because I have a ton of Java code / experience that I didn't want to lay waste. With JRuby, it is possible to use all of the fun Ruby syntax, but not have to give up your Java code / experience. Ultimately, I decided not to spend time looking into this language either. I think that I am still interested in picking this up in the future, but now I am even more interested in....

Groovy. I like Groovy. I like Groovy, a lot. One of the main reasons why I like Groovy so much, is because I have just started using it, and already I have been able to apply it to solve real customer requirements. We had a need to generate XML files based off of a Database query. Now, you can imagine how much Java code would be necessary in order to accomplish such a feat. By leveraging Groovy's integrated Sql features together with their Template approach, I was able to solve the problem very easily.

Another reason why I enjoy this language so much is because it was built with integration with Java in mind. If you cannot think of a Groovy way to get a task done, you can always import a Java class and use it. I didn't know the best way to format a Date in Groovy. However, in Java, I know that I can use the SimpleDateFormat. So that is what I did. I imported it at the top of my Groovy script, and used it. Simple.

What are you learning new this year?

Sunday, February 3, 2008

Groovy - Dynamic Language for Java Developers

Every Software Developer should be able to program in both a static and dynamically typed language. There are some situations that call for the flexibility of a dynamically typed language, and others where it makes sense to depend upon a statically typed one. I am more comfortable writing large scale enterprise applications in a statically typed language. However, when it comes to quick little scripts that I need to develop, and would probably never see production, it would make sense in those situations to write these programs in a more dynamically typed language. As a Java Developer, I am very interested in the Groovy language.

Groovy is a very interesting language because it was designed and developed to be compatible with Java. From many of the articles that I have read, you can change many of your *.java files and make it a *.groovy file and *presto* you have a groovy script. It also has the capability of leveraging your existing Java code base. Using Groovy, you can import any of your Java code, and leverage it in your Groovy script.

If you are interested in learning more, Guillaume Laforge has started a series of articles under the title "From Java to Groovy". Currently, there are two installments:

For something a little more advanced, please refer to John Carnell "Lightweight meets Heavyweight: Spring, Groovy and the Enterprise". In this article he demonstrates how to build a JMS message publisher and consumer using Groovy and Spring.

Enjoy.