Build project into a JAR automatically in Eclipse
Check out Apache Ant
It's possible to use Ant for automatic builds with eclipse, here's how
You want a .jardesc
file. They do not kick off automatically, but it's within 2 clicks.
- Right click on your project
- Choose
Export > Java > JAR file
- Choose included files and name output JAR, then click
Next
- Check "Save the description of this JAR in the workspace" and choose a name for the new
.jardesc
file
Now, all you have to do is right click on your .jardesc
file and choose Create JAR
and it will export it in the same spot.
Create an Ant file and tell Eclipse to build it. There are only two steps and each is easy with the step-by-step instructions below.
Step 1 Create a build.xml file and add to package explorer:
<?xml version="1.0" ?>
<!-- Configuration of the Ant build system to generate a Jar file -->
<project name="TestMain" default="CreateJar">
<target name="CreateJar" description="Create Jar file">
<jar jarfile="Test.jar" basedir="." includes="*.class" />
</target>
</project>
Eclipse should looks something like the screenshot below. Note the Ant icon on build.xml.
Step 2 Right-click on the root node in the project. - Select Properties - Select Builders - Select New - Select Ant Build - In the Main tab, complete the path to the build.xml file in the bin folder.
Check the Output
The Eclipse output window (named Console) should show the following after a build:
Buildfile: /home/<user>/src/Test/build.xml
CreateJar:
[jar] Building jar: /home/<user>/src/Test/Test.jar
BUILD SUCCESSFUL
Total time: 152 milliseconds
EDIT: Some helpful comments by @yeoman and @betlista
@yeoman I think the correct include would be /.class, not *.class, as most people use packages and thus recursive search for class files makes more sense than flat inclusion
@betlista I would recomment to not to have build.xml in src folder