简体   繁体   中英

How do I build and distribute my Java project?

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!

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

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 .

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 (ie completely separated from your IDE). This is a very good idea these days, where often IDEs have such in-built support for certain technologies (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM