The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Best Practice for different build options in java?

I have a complex application that has numerous interfaces with multiple concrete classes, many of them are redundant.  They are simply done differently for performance reasons, so I would like to be able to have different build options, instantiating different concrete classes depending on a flag.

One way would be to have a command line switch and a case statement where those classes are created.  Ant also has regexp capabilities from what i've been told.  The third option I thought of, was aspecting my classloading (i've already written a framework for that sort of thing, so it wouldn't be much harder to add that sort of capability).
Vince Send private email
Thursday, May 11, 2006
Apache Ant is good at this sort of stuff. There are many ways in Ant to control build options. I used Ant on one project to automatically build two different versions of an app, one that includes more advanced options and one that doesnt't.

These two versions of the app got distributed to different audiences.
Imminent Send private email
Friday, May 12, 2006
What about using Spring (or some other IoC container)?

Specify which impl class to use (usually in an xml file) and then you can edit the xml file or switch in a different xml file specifying different impl classes.

Its NOT complex, so dont be fooled by all the people who say Spring or IoC adds complexity. It doesnt, or at least not for simple stuff like this.

ie, in the xml:
<bean id="foobar" class="com.example.DatabaseFoobarImpl">
  <property name="datasource" ref="myDs"/>

As you can see you also get the benefit of dependency injection, in this case the datasource (which for the purposes of this example we assume to be defined under the name "myDs" in the xml somewhere else...)

in your code:

ApplicationContext appCtx = new ClassPathXmlApplicationContext("context.xml");
//there are several alternative ways to create AC and store it depending on your environment
IFoobar foobar = (IFoobar)appCtx.getBean("foobar");

Later if you want to use a different impl, just edit the xml config file, or deploy it with a different xml config file. No need to recompile.

This sort of thing was the main motivation behind frameworks like Spring. Of course as we know Java frameworks have a tendency to grow like cancer and that starts putting people off, then someone creates a new framework to do the same things without all the extra nonsense, and then it too starts to grow like cancer...

Anyhow Spring has several jars so for the IoC stuff you dont have to include all of them. Just (for Spring 1.2.6):
spring-beans.jar (223Kb)
spring-context.jar (107Kb)
spring-core.jar (116Kb)

All that said, I do really think Java compilation could use a good preprocessor and macros like what C and C++ have!

Java GUI Programmer
Friday, May 12, 2006
Oh yeah. +1 for Ant if you arent already using it.
Java GUI Programmer
Friday, May 12, 2006
Go with Ant and then have different build paths/options.

For my biggest customer, we have a handful of paths:
*  One creates the jar (target: jar);
*  Anotheregments the jars into their functional units (target: jar-set);
*  Another builds the jar, precompiles the jsp's and runs tests (target: test);
*  Another does all of the above and creates the war (target: deploy);

It's pretty trivial and then you have all the tools/targets that you built to build upon.
KC Send private email
Friday, May 12, 2006
Alternatively, you could use Maven ( which allows you to achieve similar things to Ant, but with much smaller config files (and will also build your website for you - only half joking!).

There's now a freely-downloadable book on Maven 2.0 at
Darren Hague Send private email
Friday, May 12, 2006
Well, let me clarify, I am using ANT for building my application.  Its a network application, and i'm researching all sorts of different ways to achieve the best performance.  So, I have a server class that uses NIO, another that uses my own threading pool, another that buffers writes a certain way, etc.

I want to be able to build different versions (without maintaining different source trees, just having different Concrete Classes instantiated....) I realize I could do this programatically all sorts of ways but it seemed bad practice to have that sort of thing in possible 'production ready' code.  Thank for the advice so far, I'll look into all the options.
Vince Send private email
Friday, May 12, 2006

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz