简体   繁体   English

Java:如何使用build.xml或项目的.classpath文件添加要构建的jar文件?

[英]Java : How to add jar file to build, using build.xml or project's .classpath file?

This may be a very basic question but I am really confused. 这可能是一个非常基本的问题,但我真的很困惑。

I want to add a few jar files to the build , so that when the build is generated using ant , the jars will be correctly included. 我想向构建中添加一些jar文件 ,以便在使用ant生成构建时,将正确包含jar。

In my IDE eclipse , I have added the jar file to build path but have not selected it for export . 在我的IDE eclipse ,我添加了jar文件来构建路径,尚未选择要导出的文件 Now I want to make sure the jar is exported when build is generated. 现在,我要确保在生成生成时将jar导出。 If I go to build path > order and export and select the jars to be added for export , I can see that its entry is added in .classpath file of the project. 如果我转到build path > order and export然后选择要添加的jar进行导出 ,则可以看到其条目已添加到项目的.classpath文件中。 ie if the project was X then X.classpath file will be modified . 例如, 如果项目是X,则将修改X.classpath文件

My question is, is doing what I have described above enough to export a jar? 我的问题是,做上面描述的事情足以出口罐子吗? Or Am I supposed to change the build.xml file of the project? 还是应该更改项目的build.xml文件?

Should I change both the .classpath file & build.xml file or just one of them? 我应该同时更改.classpath文件和build.xml文件, 还是仅更改其中之一? Whats the difference between these two files? 这两个文件有什么区别?

The difference is related to the way you want to generate your build with the included Jar. 差异与要使用随附的Jar生成内部版本的方式有关。 If you want to use Ant , as you described in the question, then the file to be updated is the build.xml . 如果要使用Ant (如问题中所述), build.xml更新的文件是build.xml This is the XML file that Ant reads to build your project. 这是Ant用来构建项目的XML文件。 The .classpath file has nothing to do with Ant . .classpath文件与Ant无关。 It is a "meta" file that the Eclipse JDT plugin/feature reads to build your project. 这是Eclipse JDT插件/功能读取的“元”文件,以构建您的项目。 The .classpath file contains the path to your source files, the location of your binary (where to compile your classes), and any classpath dependencies (eg external Jars, other projects referenced in your IDE). .classpath文件包含源文件的路径,二进制文件的位置(用于编译类的位置)以及任何类路径依赖项(例如,外部Jar,IDE中引用的其他项目)。

When you update the project's build path, Eclipse will automatically update the project's .classpath by adding an entry in the classpath corresponding to the Jar you added. 当您更新项目的构建路径时,Eclipse将通过在类路径中添加与您添加的Jar对应的条目来自动更新项目的.classpath Then when you build your Jar using the "Export" feature of Eclipse, this Jar will be included in the exported project's jarfile. 然后,当您使用Eclipse的“导出”功能构建Jar时,此Jar将包含在导出项目的jarfile中。

In contrast, when you build using Ant, you need to manually edit the build.xml to include the Jar in your build. 相反,在使用Ant进行构建时,需要手动编辑build.xml以将Jar包含在构建中。 This post may help in doing it. 这篇文章可能有助于做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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