Java 在 Eclipse 中自动将项目构建到 JAR 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1062941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Build project into a JAR automatically in Eclipse
提问by Priyank
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.
我有一个 Eclipse 项目,我想在其中自动将 Java 项目内置到 JAR 中。我知道我可以选择将项目导出到 JAR 中;如果我右键单击;但我真正想要的是,像 Eclipse 一样自动构建项目的.class
文件并将它们放在目标文件夹中;它还应该自动构建一个 JAR 并在某些或特定位置复制最新的 JAR。
Is there a option to configure Eclipse in such a way, to build JARs automatically?
是否可以选择以这种方式配置 Eclipse 以自动构建 JAR?
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.
只是为了让伙计们清楚,足够耐心地回答我的问题;我不是将 ANT 视为解决方案;因为我已经在使用它,但是我希望它可以通过基于时间的触发器或通过更改立即构建自动启动。
采纳答案by Priyank
Check out Apache Ant
It's possible to use Ant for automatic builds with eclipse, here's how
可以使用 Ant 进行 Eclipse 自动构建,方法如下
回答by 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.
这可以通过在 eclipse 中定义自定义 Builder 来实现(请参阅 Peter 回答中的链接)。但是,除非您的项目非常小,否则它可能会不可接受地减慢您的工作空间。类文件的自动构建以增量方式发生,即只重新编译受更改影响的那些类,但每次保存更改时都必须重新构建和完整复制 JAR 文件。
回答by Konrad
You want a .jardesc
file. They do not kick off automatically, but it's within 2 clicks.
你想要一个.jardesc
文件。它们不会自动启动,但只需点击 2 次即可。
- 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
- 右键单击您的项目
- 选择
Export > Java > JAR file
- 选择包含的文件并命名输出 JAR,然后单击
Next
- 选中“在工作区中保存此 JAR 的描述”并为新
.jardesc
文件选择一个名称
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.
现在,您所要做的就是右键单击您的.jardesc
文件并选择Create JAR
,它将在同一位置导出它。
回答by underscore
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 Projectuseful. 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.
关于彼得的回答和迈克尔的补充,您可能会发现如何在 Eclipse Java 项目中自动生成 .jar 文件很有用。因为即使您的项目中有“*.jardesc”文件,您也必须手动运行它。它可能会稍微降低您的“日食点击麻烦”。
回答by 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.
创建一个 Ant 文件并告诉 Eclipse 来构建它。只有两个步骤,每个步骤都很容易,按照下面的分步说明进行。
Step 1Create a build.xml file and add to package explorer:
步骤 1创建 build.xml 文件并添加到包资源管理器:
<?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 应该类似于下面的屏幕截图。请注意 build.xml 上的 Ant 图标。
Step 2Right-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 binfolder.
步骤 2右键单击项目中的根节点。- 选择 Properties - 选择 Builders - 选择 New - 选择 Ant Build - 在 Main 选项卡中,完成bin文件夹中build.xml 文件的路径。
Check the Output
检查输出
The Eclipse output window (named Console) should show the following after a build:
构建后,Eclipse 输出窗口(名为 Console)应显示以下内容:
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 和@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
@yeoman 我认为正确的包含应该是 /.class,而不是 *.class,因为大多数人使用包,因此递归搜索类文件比平面包含更有意义
@betlista 我建议不要在 src 文件夹中有 build.xml
回答by Hugo Leote
Using Thomas Bratt's answer above, just make sure your build.xml is configured properly :
使用上面的Thomas Bratt的回答,只需确保您的 build.xml 配置正确:
<?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.)
(注意双星号- 它会告诉 build 在所有子目录中查找 .class 文件。)
回答by 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.
创建构建器启动器是一个问题,因为 2 个项目不能具有相同的外部工具构建名称。每个名称都必须是唯一的。我目前正面临这个问题来自动化我的构建并将 JAR 复制到外部位置。
I am using IBM's Zip Builder, but that is just a help but not doing the real.
我正在使用 IBM 的 Zip Builder,但这只是帮助而不是真正的。
People can try using IBM ZIP Creation plugin. http://www.ibm.com/developerworks/websphere/library/techarticles/0112_deboer/deboer2.html#download
人们可以尝试使用 IBM ZIP Creation 插件。 http://www.ibm.com/developerworks/websphere/library/techarticles/0112_deboer/deboer2.html#download