Java ant、jar 文件和 Class-Path 哦,我的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1190646/
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
ant, jar files, and Class-Path oh my
提问by Jason S
I am trying to rearchitect my build technique for creating Java jar files which depend on common 3rd party jar files. (GlazedLists, Apache Commons, etc.)
我正在尝试重新构建我的构建技术,以创建依赖于常见 3rd 方 jar 文件的 Java jar 文件。(GlazedLists、Apache Commons 等)
I had been chucking them all into {Java JRE dir}/lib/ext so they would automatically be seen by the JRE, but that led to problems like not remembering that I need to distribute certain jar files, so I'd like to learn to be more explicit.
我一直将它们全部放入 {Java JRE dir}/lib/ext 中,以便 JRE 自动看到它们,但这导致了一些问题,例如不记得我需要分发某些 jar 文件,所以我想学习更明确。
So I moved them all into c:\appl\java\common\, added them to the Eclipse build path, aand defined this in my ant file:
所以我将它们全部移到 c:\appl\java\common\,将它们添加到 Eclipse 构建路径,并在我的 ant 文件中定义:
<path id="javac_classpath">
<fileset dir="${libDir}">
<include name="*.jar"/>
</fileset>
<fileset dir="c:/appl/java/common">
<include name="*.jar"/>
</fileset>
</path>
I have my Class-Path manifest header set to "." in my jar
task but that doesn't seem to work even if I put the relevant jar files into the same directory as my application jar file. I can add them all manually one-by-one to the Class-Path header, but I'm wondering, is there an easier way to get the Class-Path header setup properly?
我的 Class-Path 清单标头设置为“.” 在我的jar
任务中,但即使我将相关的 jar 文件放入与我的应用程序 jar 文件相同的目录中,这似乎也不起作用。我可以将它们全部手动一一添加到 Class-Path 标头中,但我想知道是否有更简单的方法来正确设置 Class-Path 标头?
采纳答案by rob
This is all you need:
这就是你所需要的:
<path id="build-classpath">
<fileset dir="${dist}/lib">
<include name="*.jar"/>
</fileset>
</path>
<manifestclasspath property="lib.list" jarfile="${dist}/lib/myprog.jar">
<classpath refid="build-classpath"/>
</manifestclasspath>
<jar jarfile="${dist}/lib/myprog.jar"
basedir="${build}"
includes="com/my/prog/**" >
<manifest>
<attribute name="Main-Class" value="com.my.prog.MyProg"/>
<attribute name="Class-Path" value="${lib.list}"/>
</manifest>
</jar>
As you can probably see, it assumes you've compiled your Java classes and output them to ${build}
. It also assumes you've copied your jarfiles to ${dist}/lib
.
正如您可能看到的,它假定您已经编译了 Java 类并将它们输出到${build}
. 它还假设您已将 jarfile 复制到${dist}/lib
.
That said, it would be worth looking into other build systems which have built-in support for dependencies, such as Maven and Gradle. These other build systems have already thought through many common project structures and build operations, so you don't have to script everything down to the last detail.
也就是说,值得研究具有内置依赖项支持的其他构建系统,例如 Maven 和 Gradle。这些其他构建系统已经考虑了许多常见的项目结构和构建操作,因此您不必将所有内容都编写到最后的细节中。
回答by aperkins
Classpath directories do not, by default, include Jar files for security reasons. Someone could place a jar file in there, and it could be loaded up, overriding your code, without your knowledge. I do know that there was discussion about adding another flag or ending token to allow Jar files to be added to the classpath, but, if my memory serves, that is slated for version 7. However, I could be wrong on the last part.
出于安全原因,类路径目录默认不包含 Jar 文件。有人可以在其中放置一个 jar 文件,并且可以在您不知情的情况下加载它,覆盖您的代码。我确实知道有人讨论过添加另一个标志或结束标记以允许将 Jar 文件添加到类路径中,但是,如果我没记错的话,那是为第 7 版准备的。但是,我在最后一部分可能是错误的。
回答by Dave DiFranco
The Class-Path manifest header for jar files requires you to explicitly list the jar files you want to include. Listing a directory in the classpath means java will look for .class files in that directory, not jarfiles.
jar 文件的类路径清单标头要求您明确列出要包含的 jar 文件。在类路径中列出一个目录意味着 java 将在该目录中查找 .class 文件,而不是 jarfiles。
回答by Tim Bender
What you want is a mapper. Flattenmapper is probably the easiest, but essentially you need to create a path element which specifies your classpath, then use a pathconvert on it to turn it into a string that can be included in your manifest.
你想要的是一个映射器。Flattenmapper 可能是最简单的,但本质上您需要创建一个路径元素来指定您的类路径,然后对其使用 pathconvert 将其转换为可以包含在清单中的字符串。
Edit: you can use include rules to build the path element, so everything in a directory ending with jar would be **/*.jar
编辑:您可以使用包含规则来构建路径元素,因此以 jar 结尾的目录中的所有内容都是 **/*.jar
回答by toluju
Since you're in Ant, you probably don't want to switch, but Maven is pretty good at identifying all your dependencies, and has several facilities for copying or combining your lib jars together for distribution purposes. For my purposes I have a Maven plugin that creates a run script during build, which in turn sets the classpath for me so I don't need to worry about it.
由于您在 Ant 中,您可能不想切换,但 Maven 非常擅长识别您的所有依赖项,并且有多种工具可以将您的 lib jar 复制或组合在一起以进行分发。为了我的目的,我有一个 Maven 插件,它在构建期间创建一个运行脚本,它反过来为我设置类路径,所以我不需要担心它。
回答by Peter Jamieson
With ant 1.7 and after, the manifestclasspath task is the way forward. If you are using a version of ant prior to 1.7, the manifestclasspath task does not exist.
对于 ant 1.7 及之后的版本,manifestclasspath 任务是前进的方向。如果您使用的是 1.7 之前的 ant 版本,则 manifestclasspath 任务不存在。
The following gives the same result: -
以下给出了相同的结果: -
<path id="build.classpath">
<fileset dir="${dist}/lib">
<include name="*.jar" />
<exclude name="myprog.jar" />
</fileset>
</path>
<pathconvert property="lib.list" pathsep=" ">
<path refid="build.classpath" />
<mapper>
<chainedmapper>
<flattenmapper />
</chainedmapper>
</mapper>
</pathconvert>
<target name="generate.jar">
<jar destfile="${dist}/lib/myprog.jar">
<manifest>
<attribute name="Main-Class"
value="com.mypackage.MyClass" />
<attribute name="Class-Path" value="${manifest.classpath}" />
</manifest>
</jar>
</target>