设置Ant项目名称 [英] Set Ant project name

查看:141
本文介绍了设置Ant项目名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在项目声明后更改ant.project.name属性.我知道这是不可取的,但这是我可以解决项目中大问题的唯一方法. 我发现了一些有趣的帖子,像这样:

Ant-如何设置$ { ant.project.name}更改为项目文件夹名称?

代替使用:

<project basedir="." default="package">
    <basename property="ant.project.name"
        file="${basedir}"/>
</project>

我想使用值"而不是属性文件"直接设置ant.project.name. 你有什么想法或建议吗?还是其他方式?

谢谢!

解决方案

正如其他人已经提到的,不建议更改标准ant属性的值.
此外,一旦设置,属性在设计上就不会改变,这是有充分理由的.
应当明智且很少使用覆盖属性.

ant.project.name属性通常是通过project
=>的name属性设置的 <project name="whatever">但不是强制性的,这意味着
<project> ... </project>足以使您的xml成为有效的脚本.

在您的情况下,<echo>${ant.project.name}</echo>会回显${ant.project.name}
,因为未设置属性,因此您可以在脚本中使用属性任务来创建它:
<property name="ant.project.name" value="whatever"/>.
但是使用通常是用于蚂蚁内部构件"的似乎不是最佳选择.

如果在项目标记中设置了属性,则可以通过脚本任务,使用内置的javascript引擎和ant api,fe :

<project name="foo">

 <property name="bla" value="foobar"/>

 <echo>1. $${ant.project.name} => ${ant.project.name}</echo>

 <script language="javascript">
  project.setUserProperty('ant.project.name', project.getProperty('bla'));
 </script>

 <echo>2. $${ant.project.name} => ${ant.project.name}</echo>

</project>

输出:

[echo] 1. ${ant.project.name} => foo   
[echo] 2. ${ant.project.name} => foobar

注意:,因为ant.project.name不是常规"属性(通过ant脚本中的属性任务声明的那些属性),因此必须使用方法project.setUserProperty(String, String)而不是.用户属性是通过-Dkey = value命令行参数定义的属性,并享有特殊保护.

Ant还提供了一堆内置属性

I am trying to change the ant.project.name property after the project declaration. I know this is not advisable but it's the only way I can fix a big problem with my project. I found some interesting posts like this one:

Ant - How to set ${ant.project.name} to project folder name?

Instead of using:

<project basedir="." default="package">
    <basename property="ant.project.name"
        file="${basedir}"/>
</project>

I'd like to directly set the ant.project.name using a "value" instead of a property "file". Do you have any ideas or suggestions? Or alternative ways?

Thank you!

解决方案

As others already mentioned, it's not recommended to change values of standard ant properties.
Also properties once set are immutable in ant by design and for good reasons.
Overriding properties should be used wisely and rarely.

The property ant.project.name is usually set via name attribute of project
=> <project name="whatever"> but it's not mandatory, means
<project> ... </project> is sufficient to make your xml a valid antscript.

In your case <echo>${ant.project.name}</echo> would echo ${ant.project.name},
as property is not set, so you may create it with property task in your script :
<property name="ant.project.name" value="whatever"/>.
But using a propertyname that is normally used for 'ant internals' seems not the best choice.

If property is set within project tag it's possible to overwrite the value via script task, using builtin javascript engine and ant api, f.e. :

<project name="foo">

 <property name="bla" value="foobar"/>

 <echo>1. $${ant.project.name} => ${ant.project.name}</echo>

 <script language="javascript">
  project.setUserProperty('ant.project.name', project.getProperty('bla'));
 </script>

 <echo>2. $${ant.project.name} => ${ant.project.name}</echo>

</project>

output :

[echo] 1. ${ant.project.name} => foo   
[echo] 2. ${ant.project.name} => foobar

Notice : as ant.project.name is not a 'normal' property (those properties declared via property task within ant script), you have to use the method project.setUserProperty(String, String) instead of project.setProperty(String, String). Userproperties are properties defined via -Dkey=value commandline argument and enjoy a special protection.

Ant also provides a bunch of builtin properties

这篇关于设置Ant项目名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆