views:

161

answers:

3

I used Eclipse to build a pretty simple Java project. It builds and runs in the IDE. I have a few unit tests I wrote using JUnit. They all build and pass in the IDE.

My project is in the following path:

/home/vg1890/workspace/project/

The main source is in:

/home/vg1890/workspace/project/src

And the tests are in:

/home/vg1890/workspace/project/tests

The package name is com.vg1890.stuff.

When I type: echo $CLASSPATH at the command line, nothing is returned (ubuntu).

  1. How can I build the entire project from outside the IDE?
  2. How do make it so that when I distribute the source to another computer that it will build and run (including the unit tests)?

Thanks!

+8  A: 

This is exactly the kind of thing that Apache Ant is usually used for. Eclipse supports ant scripts quite well.

Michael Borgwardt
Does the destination machine need to have Apache Ant as well in order to build the project?
vg1890
Yes - but it will be present on pretty much any machine used for Java development. Building from source or running unit tests isn't typically something non-developers do.
Michael Borgwardt
A: 

Further to Michael's answer, note that many of the IDEs (IDEA, NetBeans, Eclipse) allow you to integrate their build mechanisms very tightly with your own Ant build scripts, and can give you a hand starting them.

This can be useful when starting out. However, it is useful later to consider implementing your build mechanism so that it works cleanly (i.e. completely separated from your IDE). This is a very good idea these days, where often IDEs have such in-built support for certain technologies (e.g. Spring, Hibernate). It is otherwise too easy to release some JAR file which is missing some vital library, or end up with a dependency that you didn't know existed.

oxbow_lakes
+3  A: 

I'd suggest to use Apache Maven to manage your build and distribute your sources (maven is widely used and, like Ant, lost of Java users/programmers have it installed on their computer).

It's hard to define maven in one line (maven is much more than just a "build tool") so I've pasted below a more complete definition taken from the first chapter of this great book: Maven: the Definitive Guide.

Maven is a project management tool which encompasses a project object model, a set of standards, a project lifecycle, a dependency management system, and logic for executing plugin goals at defined phases in a lifecycle. When you use Maven, you describe your project using a well-defined project object model, Maven can then apply cross-cutting logic from a set of shared (or custom) plugins.

Once maven installed, putting a complete build in place for your project (compilation of java sources and unit tests, execution of tests, packaging of the compiled classes) is a matter of 10 seconds (really).

To get started with Eclipse, have a look at the Guide to using Eclipse with Maven 2.X.

Pascal Thivent
really, really *not*. OP already has a project structure that does not follow Maven's convention, so putting a complete build in place is a matter of restructuring his project or diving into Maven's configuration - so forget about 10 seconds. Additionally, the strengths of Maven probably won't really manifest with a very simple project.
Michael Borgwardt
How much time do you need to move files from one directory to another one? I don't need more than 5 seconds to do it so 10 seconds for main sources and tests seem fair.
Pascal Thivent
I agree that it's easy to move an existing (simple) project into maven. Simple create a new java project with the maven tool and drag in your sources. BUT, you will need to install the Eclipse integration first (there are two options - m2eclipse or IAM) AND both are not entirely bug free at this point.
Chris Nava