Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

Thursday, April 14, 2011

"Deployment Assembly" replaces "Java EE Module Dependencies" in Eclipse Helios

Frequent Eclipse users are likely already familiar with this UI change in Eclipse Helios but it's news to me and I figured I'd write a short post about it.

First, some quick background info: I spent some time last night getting up to speed on Struts 2.  I have a decent amount of experience using Struts but it's been a while.  In fact, it's been long enough that when I last used Struts, version 1.1 was the current version.  :) Anyway, I watched a couple of Struts videos on YouTube and read a few online tutorials (link1, link 2) and was ready to get started.  I won't go into detail in this post about the basic Struts configuration of Eclipse.

Everything was going according to plan until I added my previously created Eclipse User Library for Hibernate to the build path of the Dynamic Web Project.  The following warning appeared in the Markers view in Eclipse:
Classpath entry org.eclipse.jdt.USER_LIBRARY/Hibernate-3.6.2.Final will not be exported or published. Runtime ClassNotFoundExceptions may result.
Well, I guess that makes sense.  Thinking back to the days when I regularly used Eclipse (actually I was using the IBM branded version named Rational Application Developer), I figured I probably need to configure the project's "Java EE Module Dependencies".  The only problem was I didn't see it listed in the dialog for Project > Properties.  Huh?

I did some Googling and found some forum posts about the same problem.  It turns out the "Java EE Module Dependencies" has been replaced by the new "Deployment Assembly"  in Eclipse Helios (more specifically Web Tools Platform 3.2 as described here).

I added the Hibernate User Library to the deployment package structure by doing the following:
Project Properties > Deployment Assembly > Add > Java Build Path Entries > and selecting "Hibernate-3.6.2.Final" which is the name of my User Library.

I saved the configuration change and the Warning disappeared from the Markers view in Eclipse.

Tuesday, April 12, 2011

Hibernate - Part 2- Getting Started

As mentioned in a previous post, I've been preparing for using Hibernate in my latest project by reading Harnessing Hibernate, the Getting Started Guide, and watching some tutorials on YouTube.  At this point, I was ready to do some coding.

I opted to download and play with some of the tutorials from the Harnessing Hibernate book.  However, I wouldn't be using the code as-is.  I'd be making some initial modifications to the sample code:
  1. I'd use the current version of Hibernate (v3.6.2.Final), Hibernate Tools (v3.2.4.GA) and Ant (v1.8.2) instead of the older versions discussed in the book.
  2. I decided, at least for now, not to use the Maven ant tasks for downloading and maintaining library dependencies.  Instead, I would manually download the Hibernate release bundles from SourceForge.
  3. I would run the examples through Eclipse rather than solely through the file system as described in the book.
  4. Instead of using the example "test" code as-is (Java classes with main() method), I would incorporate the test code into JUnit tests.
So far so good, right?  Well I quickly ran into some challenges getting the sample code running on my dev system.

Downloading and unpacking the Hibernate resource bundles is a no-brainer, but do I need to do anything special to Eclipse?  Of course, my project will need the Hibernate jars in its classpath (more on the later) but if I want the Eclipse IDE to include the Hibernate perspective and tooling I'll also need to install the Hibernate Tools directly into Eclipse.  Although I won't really leverage the Hibernate / Eclipse integration right now, I'd like to at least get it configured.  Rather than go step-by-step through the process, I recommend following the directions as described in the JBoss Tools 3.2 Installation From Update Site.

At this point, I created a new Eclipse Java Project and imported the Harnessing Hibernate sample code for Chapter 3.  The sample code includes an Ant build file, the Hibernate configuration and mapping files as well as test classes for inserting records and querying records.

I had compile errors in the Java classes that reference Hibernate.  It's worth noting that even though we installed the Hibernate Tools into Eclipse (and indeed Eclipse now recognizes and assigns special icons and editors to the Hibernate config and mapping files) we still need to add the Hibernate and dependent jars to the project build path.

Several of the online tutorials I read suggested using Eclipse User Libraries as a convenient way to organize the Hibernate jars in the project's build path.  The User Libraries initially seemed like a good solution and my Java classes now compiled without errors.  However, I soon realized my Ant build wasn't aware of the User Libraries I created.  I describe in detail in an earlier blog post my attempts to overcome this issue.  In the end I decided to utilize User Libraries in Eclipse and add separate classpath references for the jars in the Ant build file.  It seems like a duplication of effort but it's workable for now.

An inspection of the Ant build.xml included in the Harnessing Hibernate sample code shows Ant tasks for using the HibernateTool task to create both Java classes and Database tables.  The Hibernate Tool uses the hibernate.cfg.xml and a couple of Hibernate mapping files (.hbm) to determine what to create and where.  For these samples, we are using an HSQLDB database.

Before we run the Ant tasks and actually use Hibernate for the first time, let's take a moment to review what we've done so far:
  1. Download and unpack the release bundles for Hibernate, Hibernate Took and Ant.
  2. Install the Hibernate Tools into Eclipse using Help > Install Software
  3. Create a new Eclipse Java Project and import the sample code from Harnessing Hibernate (Chapter 3)
  4. Create Eclipse User Libraries for Hibernate
  5. Update the Ant build.xml classpath with references to the Hibernate and Hibernate tool jars.
At this point, I ran the HibernateTool tasks in the Ant build file for creating the Java entity classes and database tables.  Ant executed without any stack traces and ended with BUILD SUCCESSFUL.  Awesome!  I confirmed the new Java classes were created but when I fired up the HSQL Database Manager I couldn't see the new database table.  What gives?  Why did Ant finish successfully but not create the table?

This caused a lot of confusion on my part and began a period of tweaking my classpath and log4j logging levels in an effort to understand the problem.  The first thing I noticed was my updated log4j level (now DEBUG) wasn't printing anything to the console.  I was only seeing INFO level logging.  Hmmm.

Remember, up to now I've been running the Ant build directly from Eclipse.  At this point I tried running Ant directly from a Command Prompt and voila!  The output showed DEBUG log4j logging and most importantly the database table was now created in HSQL.

After some more trial and error, I modified the JRE setting in Eclipse for the Ant build file (Right click on build.xml > Run As > Ant build...) and changed the Runtime JRE from "Run in the same JRE as the workspace" to "Separate JRE".  The JRE specified in the "Separate JRE" was the EXACT same JRE that Eclipse was using.

I then reran Ant tasks from within Eclipse and this time it worked!  I don't understand why it works in a separate JRE but not within the same JRE.  I posted to the Tools forum in the Hibernate Community.  One of the guys from the Hibernate Team posted some responses but in the end suggested keeping the "Separate JRE" setting which is what I've done.  I also made a similar post on the Stack Overflow boards.

Friday, April 8, 2011

Using Eclipse "User Libraries" with Ant

An Eclipse User Library is simply a set of JAR files defined within the IDE.  It provides a convenient way to organize and include jars in your project's build path without having to actually import the jars into your project and having them get deployed with your project.  User Libraries can be reused across multiple projects.  I haven't tried it myself, but it appears the User Libraries can be shared across machines with minimal hoop jumping.

So far so good.  But how will Ant access these User Libraries?  As far as I can determine, there's no out-of-the-box way for Ant to reference an Eclipse's User Library in it's classpath.  Here's a couple of relevant forum posts on the topic:
So what's the solution? Here's the options I've seen so far:
  1. Use the Eclipse User Library as-is then configure the Ant build file separately for the jars you need in your classpath.  I consider this option the "hard way" and of course it's what I'm doing for now.
  2. Use Apache Maven instead of Ant for build and project dependencies.  I'd like to give this a try when I have time. 
  3. I've seen a few people suggest using Ant4Eclipse.  I haven't had time to try it myself yet but it sounds like a potential solution.
  4. Don't utilize Eclipse User Libraries and copy your required jars to a /lib folder in your project.  Of course, this is just ignoring the issue isn't it. :)
I'm interested in other approaches and options for this problem.  Please feel free to comment.  Thanks.

Thursday, April 7, 2011

Getting started with JUnit and Eclipse

Over the years I've done load testing and user acceptance testing but this is the first time I've done any serious unit testing. I'm be using JUnit 4.x and trying my best to stick test-driven development.

Everyone learns in different ways, some are book-learners and others like to dive right in and get their hands dirty.  I'd like to think I take a hybrid approach but I'm always thankful for a nice tutorial.  I definitely recommend the Eclipse and Java for Total Beginners video tutorial.  Sure it's geared for true Java and Eclipse beginners but it covers JUnit and test-driven development in some clear and concise detail.

The tutorial doesn't include using Ant to run your JUnit tests.  Here's the code I am currently using in my Ant build.xml for invoking the JUnit tests in my project.

<target name="junit" depends="compile, jar">
    <junit showOutput="true" printsummary="on" fork="true" haltonfailure="true" dir="${basedir}">
        <classpath refid="project.classpath" />
        <formatter type="xml" />
        <batchtest todir="${output.dir}">
            <fileset dir="${test.dir}">
                <include name="**/*Test*.java" />
            </fileset>
        </batchtest>
    </junit>
</target>

Wednesday, April 6, 2011

Next steps in setting up my development environment...

Baby steps folks, baby steps...

In my previous post I installed Java SDK 1.6.0_24 and Eclipse EE IDE 3.6.2.  When everything goes right, as it did for me, these two installs are a no brainer.

Next in line for installs are MySQL, MySQL Workbench, the MySQL JDBC driver, Apache Tomcat, and Apache Ant

Let's start with MySQL, the MySQL Workbench and JDBC driver.
Prior to the install I watched a free webinar titled "MySQL Essentials Series - Part 1: Building, Installing and Configuring MySQL" which covers in some detail a number of installation and configurations options available for MySQL.  I thought the video was worth my time.

My needs should be simple so I opted for the MySQL Community Server 5.5.11 using the MSI Installer for 32-bit Windows.  I installed to "C:\Program Files\MySQL\MySQL Server 5.5" and chose to store the MySQL data in my dev directory, "C:\dev\MySQL Datafiles".

I opted to install MySQL as a Windows Service and later configured the service from Automatic startup to Manual.  As I've said before, my dev machine is under-powered and I need to conserve RAM usage.  I also changed the password for the root user.

I installed MySQL Workbench 5.2.33 alongside MySQL in "C:\Program Files".  After installation, I fired it up and created a new connection to my local instance of MySQL running on the default port 3306.  I confirmed the workbench can connect to the running MySQL database.

Lastly, I downloaded MySQL Connector/J which is the JDBC driver for MySQL.  I unzipped the file and copied the "mysql-connector-java-5.1.15-bin.jar" to my "C:\dev\plugins" directory.

After downloading the driver and placing it in your "plugins" directory (or wherever you are storing it), go ahead and create a Driver Definition for your MySQL database in Eclipse.  Reminder: you need the EE version of Eclipse.

Eclipse  > Open Perspective > Database Development
Database Connections > New > MySQL > MySQL JDBC Driver 5.1
Enter the values for you MySQL installation, including the location of the JDBC Driver, Database Name, User ID and Password.

I'm looking forward to blogging more about developing against the MySQL database.  I used it only once before when testing a Media Wiki instance.  Most of my previous database experience has been with Oracle and SQL Server.

Next install was Apache Tomcat.
I downloaded the current stable release, version 7.0.11, which implements implements the Servlet 3.0, JSP 2.2 and EL 2.2 specifications.  I chose the zip of the core distribution.  As I reflect on this, I'm not sure why I didn't just choose the .exe version which automatically installs Tomcat as a Windows service.  I kind of like the zip version because I can simply unzip it anywhere and it will run using the startup.bat found in the Tomcat "bin" directory.  Also, why install yet another Windows service that I'm going to change from Automatic startup to Manual on my resource starved dev machine? I'd be interested in any comments or thoughts on which Tomcat install is best.

Confirm your Windows Environment Variable "JAVA_HOME" is pointing to your JDK install.  I had to update my "Java_Home" because it was set to a previous JDK install.

I ran startup.bat, watched the console output for errors and confirmed a successful server startup by navigating to http://localhost:8080/ in my browser (which these days is Firefox).

Next, we need to tell Eclipse about Tomcat.  In lieu of me explaining step-by-step I'll suggest you read this very good tutorial from coreservlets.com: Tutorial: Installing Tomcat 7 and Using it with Eclipse

Install Ant
The current version of Apache Ant is 1.8.2 which I downloaded and unzipped to "C:\dev\Ant".

Create a new Windows Environment Variable "ANT_HOME" and point it to your Ant install.  In my case "ANT_HOME=C:\dev\Ant\apache-ant-1.8.2".

Well, that's it for now.  Truth be told, I did these installs about a week ago, before I decided to keep a running blog of my progress.  Going forward, my posts should be more detailed (and more accurate?) particularly as I begin configuring Eclipse to use Hibernate and using Ant to build my project.

Installing the Eclipse IDE for Java EE

My first experience with a Java IDE was VisualAge for Java back in 2000.  Before long, my company's development team moved on to WebSphere Studio Application Developer (WSAD) which before long was rebranded as Rational Application Developer (RAD).

It's funny now but after all these years, this will be the first time I have used the regular Eclipse IDE and not the IBM branded version.  What's the difference anyway?  My understanding is that IBM's version had additional support for integration with other IBM products in the IBM and Rational product suite.  I guess I don't really know all the differences. Anyone have any additional details they can add?  Please feel free to add comments to this post.

I downloaded Eclipse IDE for Java EE Developers (Helios 3.6.2) and unzipped it to a folder near the root of my workstation: C:\dev\eclipse\eclipse_EE_3.6.2.  That's the install in a nutshell.  Nothing to it.

Documentation for installing and configuring Eclipse can be found in the Getting Started section of the The Official Eclipse FAQs.

So why install Java EE version instead of the standard Java version?  The Compare Packages page details the differences between the two versions.  The EE includes packages needed for web development.  Namely the Web Tools and the Data Tools.  These tools will be necessary as I develop using Hibernate persistence in MySQL and deploy to Tomcat.

I guess now is a good time to mention that I'm using an Dell XPS/Dimension 400/9150 circa 2006 with a single dual core 2.79 GHz CPU with only 1 GB of RAM.  This old boy is still running Windows XP.  Is this sufficient power for running a development environment with Eclipse, Tomcat, MySQL?  I hope so.  For the time being, I've splurged and ordered another 1 GB of RAM.  Hopefully, 2GB is noticeably better than 1 GB of RAM.

Prior to the Eclipse installation, I installed JDK 1.6.0_24.