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.


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. Eclipse Project中的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.

    Ant构建器配置构建步骤 - 目标选项卡

    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

    链接地址: http://www.djcxy.com/p/65834.html

    上一篇: 我如何在Android项目中使用外部JAR?

    下一篇: 在Eclipse中自动将项目构建到JAR中