Java 从 Ant 中查找工作目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1596318/
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
Find working directory from Ant
提问by JW.
Is it possible to tell which directory the user ran Ant from?
是否可以知道用户从哪个目录运行 Ant?
For example, I might want to run only the unit tests in the current working directory, rather than all tests for the entire project.
例如,我可能只想运行当前工作目录中的单元测试,而不是整个项目的所有测试。
I tried this:
我试过这个:
<property environment="env" />
<echo>${env.CWD}</echo>
but that doesn't work.
但这不起作用。
采纳答案by Asaph
The whole Properties object returned by System.getProperties()
is exposed by Ant. Try this:
返回的整个 Properties 对象System.getProperties()
由 Ant 公开。尝试这个:
<echo>${user.dir}</echo>
回答by java.is.for.desktop
${basedir}
might also be useful.
${basedir}
也可能有用。
In Netbeans under Linux, ${user.dir}
seems always to equal ${user.home}
, but ${basedir}
gives me actual directory where ant is ran from (so it gives me the project dir).
在 Linux 下的 Netbeans 中,${user.dir}
似乎总是等于${user.home}
,但${basedir}
给了我运行 ant 的实际目录(所以它给了我项目目录)。
回答by user2697803
Unless you're using Windows, this will give you the current working directory:
除非您使用的是 Windows,否则这将为您提供当前工作目录:
<exec executable="sh" outputproperty="my.cwd"><arg line="-c pwd"/></exec>
回答by culmat
and under windows
和窗户下
<exec executable="cmd" outputproperty="my.cwd"><arg line="/C echo %cd%"/></exec>