Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
817 views
in Technique[技术] by (71.8m points)

properties file - Set Ant project name

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...