Java 如何将类路径变量回显到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9724327/
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 echo class path variable to a file
提问by IAmYourFaja
I'm trying to get all the contents on my main classpath to get written to a file by my Ant buildscript:
我正在尝试获取主类路径上的所有内容,以便通过 Ant 构建脚本将其写入文件:
<path id="main.class.path">
<fileset dir="${lib.main.dir}">
<include name="**/*.*"/>
</fileset>
</path>
When I hover over main.class.path
, Ant/Eclipse launches a tooltip that shows the items on that classpath:
当我将鼠标悬停在 上时main.class.path
,Ant/Eclipse 会启动一个工具提示,显示该类路径上的项目:
C:\Users\myUser\workbench\eclipse\workspace\myProj\lib\main\ant-junit-1.6.5.jar
C:\Users\myUser\workbench\eclipse\workspace\myProj\lib\main\ant-junit-1.6.5.jar
etc. (The actual list has about 30 JARs on it.)
等等(实际列表上大约有 30 个 JAR。)
I want this list written to a file called deps.txt under my dist/
directory.
我想将此列表写入我的dist/
目录下名为 deps.txt 的文件中。
I'm stuck because I can't figure out how to make main.class.path
an Ant variable, or how to at least access it in the <echo>
task:
我被卡住了,因为我不知道如何创建main.class.path
Ant 变量,或者至少如何在<echo>
任务中访问它:
<echo file="${dist.dir}/deps.txt" message="${???}"/>
Am I way-off base here, or even remotely close?!?
我是不是在这里远离基地,甚至是远离基地?!?
And for those of you out there that, instead of answering this question, would just comment Why would you want to do this?, my answer is simple: I just want a small text file in my JAR that serves as a visual reminder (for my future me) for what its dependencies are.
对于你们中的那些,而不是回答这个问题,只会评论你为什么要这样做?,我的答案很简单:我只想在我的 JAR 中有一个小文本文件,作为它的依赖项的视觉提醒(对于我未来的我)。
采纳答案by hmjd
Try this:
尝试这个:
<pathconvert property="expanded.main.class.path" refid="main.class.path"/>
<target name="everything">
<echo message="${expanded.main.class.path}"
file="${dist.dir}/deps.txt"/>
</target>
回答by Rebse
Straightforward via :
直接通过:
<echo file="${dist.dir}/deps.txt">${ant.refid:main.class.path}</echo>
<!-- or -->
<echo file="${dist.dir}/deps.txt">${toString:main.class.path}</echo>
${ant.refid:main.class.path} or ${toString:main.class.path} is a csv property which holds all items from your path with its nested fileset(s) (resourcecollectionsin general) separated with ';'
see Ant Manual Properties and PropertyHelpers
If you want another separator you need to use pathconvertwhich has a pathsep attribute
, i.e. for a new line after each file in your deps.txt use pathsep="${line.separator}"
${ant.refid:main.class.path} 或 ${toString:main.class.path} 是一个 csv 属性,它保存路径中的所有项目及其嵌套文件集(通常是资源集合),以 '; 分隔。 '
请参阅 Ant 手册属性和 PropertyHelpers
如果你想要另一个分隔符,你需要使用pathconvert,它有一个pathsep attribute
,即在你的 deps.txt 中的每个文件之后使用一个新行pathsep="${line.separator}"