ChatGPT解决这个技术问题 Extra ChatGPT

Build project into a JAR automatically in Eclipse

I have an Eclipse project where I want to keep my Java project built into a JAR automatically. I know I have an option to export the project into a JAR; if I do a right click; but what I am really looking for is, that like Eclipse automatically builds a project's .class files and put them in target folder; it should also build a JAR automatically and copy the latest JAR at some or a specific location.

Is there a option to configure Eclipse in such a way, to build JARs automatically?

Just to make it clear for guys, patient enough to answer my question; I am not looking at ANT as solution; as I already use it, but what I would like it something that gets initiated automatically either with a time based trigger or immediate build with change.

My personal opinion: Use m2e, Eclipse's maven integration, so it outputs a jar for you as part of the build. I recently tried to use Eclipse without Maven and can't believe how primitive the basic Java builder is.
For 2018 use a build tool instead of IDE specific functionality. Maven buys you IDE independence and command line support.
@Craig Ringer - is there a link regarding how to do that?

s
spongebob

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.


Sad that this answer didn't get more upvotes (or best answer) months ago. Oh well, by now Konrad is gone.
In Eclipse Galileo, I had to replace your step 1 with File -> Export -> Java -> JAR file.
Is there a way to automate the 'right click -> Create JAR' step?
Is there a way to automate the 'right click -> Create JAR' step or bind it to a key combo?
I use "Export... -> Java -> Runnable JAR file" to create exactly that.
T
Thomas Bratt

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>

https://i.stack.imgur.com/2hxBv.png

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.

https://i.stack.imgur.com/dcm1H.png

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


Yes, you can choose the targets for the build step as follows:After a Clean, Manual Build, Auto Build and During a Clean. Auto Build should give you what you want :)
After having tried both, this solution seems much better than Konrad's. It requires you to manually figure out some paths in your build file but it allows automatic build (instead of 2 clicks) and allow for easy customization (copy the jar after build for example). Here's how to build a jar file when having the build file in the root and having multiple nested levels of packages: <jar jarfile="dist/mypackage.jar" basedir="bin/" includes="**/*.class" />
+1 How isn't this the accepted answer and not even the most up voted one?
Thank you very much. I would recomment to not to have build.xml in src folder, but the rest is working fine, amazing !
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. Will remove my +1 until then because the answer doesn't usually work as it stands.
佚名

Check out Apache Ant

It's possible to use Ant for automatic builds with eclipse, here's how


I am already using Apache ant to do the builds and make Jar. But what I was really looking for was a way around apache ant, and directly using eclipse.
Apologies, I didn't go through the article in link earlier. It indeed talks about what I need. Thanks.
Answer by Konrad more closely matches what the OP was looking for.
In July 2011, the article referenced by @Peter doesn't talk about how to build a jar file at all.
And now it's gone... That's why you should repeat the essential parts of the information from the linked resource in your answer.
M
Michael Borgwardt

This is possible by defining a custom Builder in eclipse (see the link in Peter's answer). However, unless your project is very small, it may slow down your workspace unacceptably. Autobuild for class files happens incrementally, i.e. only those classes affected by a change are recompiled, but the JAR file will have to be rebuilt and copied completely, every time you save a change.


C
Community

Regarding to Peter's answer and Micheal's addition to it you may find How Do I Automatically Generate A .jar File In An Eclipse Java Project useful. Because even you have "*.jardesc" file on your project you have to run it manually. It may cools down your "eclipse click hassle" a bit.


H
Hugo Leote

Using Thomas Bratt's answer above, just make sure your build.xml is configured properly :

<?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="bin/" includes="**/*.class" />
  </target>
</project>

(Notice the double asterisk - it will tell build to look for .class files in all sub-directories.)


A
Anand Bansal

Creating a builder launcher is an issue since 2 projects cannot have the same external tool build name. Each name has to be unique. I am currently facing this issue to automate my build and copy the JAR to an external location.

I am using IBM's Zip Builder, but that is just a help but not doing the real.

People can try using IBM ZIP Creation plugin. http://www.ibm.com/developerworks/websphere/library/techarticles/0112_deboer/deboer2.html#download