如何使用 build.xml 文件将 Eclipse 项目的“Java 构建路径”部分中引用的 jar 包含在使用 Ant 生成的 jar 文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15709951/
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
How to include the jars referenced in "Java Build Path" section of an Eclipse project in a jar file generated with Ant using a build.xml file
提问by Leo
basically that's my requirement , if someone adds or deletes a jar reference in the section "Java Build Path" , then that jar should be included ( or not included in case of deletion) when the project is built and a jar of the project is generated with a build.xml ant file. I was expecting to reference a property or variable in the xml file. something like:
基本上这就是我的要求,如果有人在“Java 构建路径”部分添加或删除了 jar 引用,那么在构建项目并生成项目的 jar 时,应该包含该 jar(或者在删除的情况下不包含)使用 build.xml ant 文件。我希望在 xml 文件中引用一个属性或变量。就像是:
<!-- copy the JARs that you need to "target" directory -->
<copy todir="target">
<fileset dir="${buildPath}" includes="*.jar" />
</copy>
where ${buildPath} is pointing to "Java Build Path". That is just an example and doesn't work
其中 ${buildPath} 指向“Java 构建路径”。这只是一个例子,不起作用
Notice that I don't want to put every jar individually in the build.xml , but to use something like includes="*.jar" , so if someone changes "Java Build Path" , I don't have to change the build.xml
请注意,我不想将每个 jar 单独放在 build.xml 中,而是使用 includes="*.jar" 之类的东西,因此如果有人更改了 "Java Build Path" ,我不必更改构建.xml
采纳答案by Lucas
The Java Build Pathin eclipse is actually backed by the .classpath
file in your eclipse project. So if you look in that file you will see all of the paths used by eclipse. You can parse them out of there with ant. For example, I have a simple project that has a Java Build Paththat points the the standard JRE libraries and a link to a jar from another project (dataset-tools). This is my .classpath
:
eclipse 中的Java Build Path实际上是由.classpath
eclipse 项目中的文件支持的。因此,如果您查看该文件,您将看到 eclipse 使用的所有路径。你可以用 ant 解析它们。例如,我有一个简单的项目,它有一个指向标准 JRE 库的Java 构建路径和一个指向另一个项目(数据集工具)中的 jar 的链接。这是我的.classpath
:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="/dataset-tools/target/dataset-tools.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
So, as you can see, if you add a new jar to your Java Build Pathit is reflected in the .classpath
file thusly:
因此,如您所见,如果您向Java 构建路径添加一个新的 jar,它会.classpath
因此反映在文件中:
<classpathentry kind="lib" path="/dataset-tools/target/dataset-tools.jar"/>
You could use ant to parse that file looking for these classpathentry
elements with kind lib
. This is the same answer presented here.
您可以使用 ant 来解析该文件,classpathentry
以使用 kind查找这些元素lib
。这与此处提供的答案相同。
回答by Ian Roberts
The Eclipse build path information is stored in the .classpath
file, and you can probably parse out the simple bits from this (entries that point to single JAR files either within the project workspace or externally with absolute paths) but this will only get you so far - there are other types of classpath entries such as those based on "classpath variables", references to other projects in the workspace, or other types of containers provided by Eclipse plugins. For example, I have the GGTS plugins (for Groovy and Grails) and my Grails project's .classpath
has no explicitly listed JARs at all, it simply says
Eclipse 构建路径信息存储在.classpath
文件中,您可能可以从中解析出简单的位(指向项目工作区内或外部具有绝对路径的单个 JAR 文件的条目),但这只会让您到此为止-还有其他类型的类路径条目,例如基于“类路径变量”的条目、对工作区中其他项目的引用或 Eclipse 插件提供的其他类型的容器。例如,我有 GGTS 插件(用于 Groovy 和 Grails),而我的 Grails 项目.classpath
根本没有明确列出 JAR,它只是说
<classpathentry kind="con" path="org.grails.ide.eclipse.core.CLASSPATH_CONTAINER"/>
There is an Ant4Eclipseproject which claims to help with this, but the most recent release ("milestone 4") is from mid-2010 and I've no idea how well it works with more recent Eclipses.
有一个Ant4Eclipse项目声称可以帮助解决这个问题,但最新版本(“里程碑 4”)是 2010 年年中的,我不知道它与最近的 Eclipse 的配合情况如何。
A better approach may be to manage your dependencies declaratively using Ivy, and have both the Ant build andthe Eclipse project (using IvyDE) take their libraries from there. When you want to add or remove a dependency you just edit the ivy.xml
file and both Ant and Eclipse will pick up the change on their next build.
更好的方法可能是使用Ivy以声明方式管理您的依赖项,并让 Ant 构建和Eclipse 项目(使用IvyDE)从那里获取它们的库。当您想要添加或删除依赖项时,您只需编辑该ivy.xml
文件,Ant 和 Eclipse 都会在下一次构建时获取更改。
回答by scott
Use the xmlproperty task to read the .classpath into property values.
使用 xmlproperty 任务将 .classpath 读入属性值。
<xmlproperty file=".classpath" collapseAttributes="true" delimiter=";" />
Then set that value in the path
然后在路径中设置该值
<path id="eclipse.classpath">
<pathelement path="${classpath.classpathentry.path}"/>
</path>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" updatedProperty="compiled">
<classpath refid="eclipse.classpath"/>
</javac>
</target>