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

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

问题描述

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

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

而不是使用:

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

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

谢谢!

解决方案

正如其他人已经提到的,不建议更改标准 ant 属性的值.
此外,一旦设置的属性在 ant 中设计为不可变的,并且有充分的理由.
应该明智地并且很少使用覆盖属性.

属性 ant.project.name 通常通过 project
=> 的 name 属性设置 但它不是强制性的,意思是
... </project> 足以使您的 xml 成为有效的antscript.

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

如果在项目标签中设置了属性,则可以通过脚本任务覆盖该值a>,使用内置javascript引擎和ant api,fe:

<property name="bla" value="foobar"/><回声>1.$${ant.project.name} =>${ant.project.name}</echo><脚本语言="javascript">project.setUserProperty('ant.project.name', project.getProperty('bla'));<回声>2.$${ant.project.name} =>${ant.project.name}</echo></项目>

输出:

[echo] 1. ${ant.project.name} =>富[回声] 2. ${ant.project.name} =>食物吧

注意: 由于 ant.project.name 不是正常"属性(通过 ant 脚本中的属性任务声明的那些属性),您必须使用方法 project.setUserProperty(String, String) 而不是 project.setProperty(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天全站免登陆