| Subcribe via RSS or via Email

A ‘LegUp’ for new Java projects

October 7th, 2009 | 9 Comments | Posted in Java

Nowadays, even a simple Java web system is going to require a web framework, some sort of persistence layer and probably some sort of dependency injection.  But how long would it take you to create such a project from scratch?  1 day? 1 hour maybe if you knew all of the technologies involved.  With LegUp you can be up and running in 15 seconds*.

LegUp is a collection of free, open source (Apache 2 licensed) Maven 2 archetypes, and a web based configuration tool provided by jWeekend.  It allows you to choose from different combinations of popular Java technologies (Apache Wicket, Spring, Google Guice, JPA, Hibernate, Warp Persist) to allow you to get up and running quickly and easily.  Up and running means that will have a simple application running within a minute of starting.

Currently the following archetypes are available:

  • Spring 2.5 with a JDBC database layer
  • Spring 2.5 with a JPA 1.0 database layer
  • Apache Wicket 1.4 and Google Guice 1.0
  • Apache Wicket 1.4, Google Guice 1.0, Warp Persist 1.0 and Hibernate 3
  • Apache Wicket 1.4, Google Guice 1.0, Warp Persist 1.0 and JPA 1.0
  • Apache Wicket 1.4, Spring 2.5 and JPA 1.0
The projects using JPA include multiple JPA providers.  Hibernate is the default, but both OpenJPA and EclipseLink are also included.  To change the provider, comment out the hibernate dependency in the projects pom.xml and uncomment the desired provider.  Then comment out/in the appropriate lines in the persistence.xml file.

Example for a project with Wicket, Spring and JPA

  • Make sure you have Java 5+ and Maven 2 installed and working.  You can search on the internet for how to do this if you are not sure.
  • Go to the LegUp configuration page in your web browser of choice.
  • Select the Wicket, Spring, JPA archetype from the drop down list.
  • Optionally customize the Group Id, Archetype Id, Version and Package fields.  These are standard Maven properties.  I will leave the defaults for this example.
  • Click the ‘Generate Maven Command’ button.  This will populate the text area under the button.
  • Open a terminal window (Mac, Linux) or command prompt window (Win) and cd to a suitable directory.
  • Copy the command line (starts with mvn archetype:generate) and paste it into your open terminal.
  • Hit enter and wait for maven to finish processing the command.  The time this will take depends on the speed of your internet connection and how populated your local maven repository is.  It could take a few minutes if this is the first time you have used maven.
  • Cd into the directory which has been produced, this will be named the same as the Archetype Id you entered earlier.  In my case this directory is called ‘myproject’.
    cd myproject
  • Build the application and run the provided tests using this maven command:
    mvn compile test
  • Run the wicket application on jetty with this maven command.
    mvn jetty:run
  • Thats it, you now have a running Wicket Application with Spring Dependency injection and JPA using hibernate under the covers, all running with a HSQL database.

Added bonus feature

If you type the following maven command, it will generate you an eclipse project which you can import into eclipse to start editing the code straight away

mvn eclipse:eclipse

Gotchas

  • Warp Persist will need installing into your local repository manually.  Download it from the Warp Persist website and follow the instructions that maven gives you.
*15 seconds is assuming that your maven repository contains all of the required jars already.
** Disclaimer, I work for jWeekend.
Tags: , , , ,

More Warped Wicket

August 17th, 2008 | 2 Comments | Posted in Java, Wicket

Earlier this year I wrote a post on integrating wicket and warp persist, and I thought it was about time I posted an update, but this time I included working example projects.

The first project is a basic wicket / warp persist setup with hibernate and an apache derby database.  It allows you to persist the Event class used in the hibernate examples.

wicketwarp.zip

The second project is the same, but also makes use of warp-servlet for hooking up all your other servlets and filters in guicy way.

wicketwarpservlet.zip

In both cases i’ve tried to keep xml configuration down to a minimum, as I hate xml config.  So the hibernate classes are configured with hibernate annotations.  Wicket itself involves no xml configuration as always.

As warp persist and warp servlet are not in a maven repository (as far as I know) you will need to manually add them into your maven repository in the normal way.

Thoughts, Code patches, Comments, etc are welcome.

Tags: ,

Wicket-Guice and Warp-Persist

April 19th, 2008 | 8 Comments | Posted in Guice, Wicket

This week I came across Warp-Persist, which provides transactional persistence support with Google Guice and Hibernate. To be honest its quite like what Spring provides, but via Guice, which means no nasty XML config files (except for the hibernate one if desired).

I had already been looking at the wicket-guice project which provides support for Guice in Wicket.

The best place to start with integrating these components would be Peter Mularien’s blog post on integrating warp-persist and wicket (but not wicket-guice). Peter’s post, while good, does not cover all of the things that I had to do to get everything working.

  1. Add the wicket-guice jar file to your project, if you are using maven then you can easily add the dependancy to your pom.xml file, if not then I think the right jar files (you need wicket-ioc.jar too) are come with the wicket download.
  2. Also add the warp-persist jar file which can be obtained here (You don’t have to checkout and build the code yourself). I couldn’t find a maven repo with the warp-persist jar in it so If anyone can point me to one I would be grateful.
  3. You will also need the AOP alliance jar file, or you can add it to your maven pom.xml.
  4. In your WebApplication.init() method add the call to the GuiceComponentInjector:
    injector = Guice.createInjector(PersistenceService.usingHibernate()
    .across(UnitOfWork.REQUEST).transactedWith(
    TransactionStrategy.LOCAL).buildModule(),
    new MyApplicationModule());
    addComponentInstantiationListener(new GuiceComponentInjector(this, injector));

    Make sure that you pass your injector into the GuiceComponentInjector constructor, otherwise things dont work later on (this took me several hours to figure out :) )

  5. Follow the rest of Peters instructions
  6. But make sure that you put the Warp-Filter before the Wicket-Filter. Putting it after did not work for me, its possible that the warp-persist code has changed between Peter’s post and now, or that wicket-juice causes something to go wrong.
  7. Thats it. You can get a guice injected hibernate session like this:
    @Inject Provider<Session> session;
    @Transactional
    public void save(T object)
    {
    session.get().saveOrUpdate(object);
    }

    Wherever you need it.

So now you should have a nice warped guicy wicket app (sorry couldn’t resist)

Next time, integrating dynamic finders.

Tags: , , ,