You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Frederich, Eric P21322" <er...@siemens.com> on 2007/12/10 19:19:56 UTC

Setting final strings at compile time

Hello,

Is there an easy way to set final Java Strings at compile time?
I know you can set properties at runtime with -Dproperty=value but could
I do this at compile time?
What I want to do is have one target which makes a program called "Java
Program XYZ - production" and another which is called "Java Program XYZ
- development".
Also, it would be useful to somehow get the date and time at which it
was compiled into the program.

I figured this would be common, hopefully it is.  Perhaps I just don't
know the right words to search on Google.

Thanks in advance,
~Eric

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Setting final strings at compile time

Posted by Dmitri Farafonov <fa...@gmail.com>.
I do not think you want to set this information into a java class, but 
into your final jar file using manifest file.
http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html

Hope it helps,
Dmitri.

Frederich, Eric P21322 wrote:
> Hello,
>
> Is there an easy way to set final Java Strings at compile time?
> I know you can set properties at runtime with -Dproperty=value but could
> I do this at compile time?
> What I want to do is have one target which makes a program called "Java
> Program XYZ - production" and another which is called "Java Program XYZ
> - development".
> Also, it would be useful to somehow get the date and time at which it
> was compiled into the program.
>
> I figured this would be common, hopefully it is.  Perhaps I just don't
> know the right words to search on Google.
>
> Thanks in advance,
> ~Eric
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>
>   

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Setting final strings at compile time

Posted by Robin Chaddock <ro...@iplay.com>.
Surely that is what a jars manifest is for?
  ----- Original Message ----- 
  From: Wayne Cannon 
  To: Ant Users List 
  Sent: Thursday, December 13, 2007 1:11 AM
  Subject: Re: Setting final strings at compile time


  Eric,

  We create an almost-Java source file, e.g., Version.txt, containing 
  replacement targets.  We extract version information from Subversion (or 
  ClearCase, etc.) into a property with Ant, which we then <replace> in an 
  Ant <copy> from Version.txt to Version.java.  The advantages of this 
  approach are (1) the ability to use version control for Version.txt, (2) 
  the ability to extract version information of various types (build date, 
  product version number, build number, and version-control version 
  number) for the compiled version, and (3) independence from the build 
  system allowing you to use Ant, make, clearmake, etc., and switch 
  systems if your process demands.  The disadvantage of this approach is 
  that Version.txt is not automatically recognized as a Java source file 
  by IDEs, such as Eclipse, based solely on its suffix.

  Steve's approach seems very similar, except that he prefers to use Java 
  properties files read by Java classes at run-time, instead of Java 
  source files themselves.  I prefer the Java source file in order to keep 
  all of the version-related information in individual Java classes, and 
  not in a separable, easily editable, property file that is read at 
  run-time.  For some approaches, there is definitely a benefit to the 
  flexibility of an editable property file that is read at run-time.  I 
  use my Version base class, and its derivatives, for version 
  compatibility test methods across both sides of interfaces, as well as 
  returning a string for version reporting, "Help | About" information, etc.

  Regards,
  Wayne

  from Version.txt:
      public static final String DATE_STRING = "@date@";
      public static final String MAJOR_VERSION = "@major@";
      public static final String MINOR_VERSION = "@minor@";
      public static final String PATCH_VERSION = "@patch@";
      public static final String BUILD_NUMBER = "@build@";

  resuting Version.java:
      public static final String DATE_STRING = "Tue Nov 06 00:08:51 PST 2007";
      public static final String MAJOR_VERSION = "02";
      public static final String MINOR_VERSION = "00";
      public static final String PATCH_VERSION = "00";
      public static final String BUILD_NUMBER = "143";


  Frederich, Eric P21322 wrote:
  > Thanks for the reply and a possible solution but this seems more like a
  > hack.
  > Yeah, right now I have something like
  > Public static final String programName = "Java Program XYZ";
  > I'd like to be able to make one where that string is "Java Program XYZ -
  > Development Version" automagically using ant.
  >
  > If the javac command could set properties the way the java command does
  > it would be nice.
  > If this isn't the case I guess I'll be stuck with a "hack" like this.
  > Just seems to be a pain to explicitly copy the original to a tmp
  > directory, do the replacement on the file, compile it and then copy the
  > original file back.
  >   


  ---------------------------------------------------------------------
  To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
  For additional commands, e-mail: user-help@ant.apache.org


________________________________________________________________________
E-mail is an informal method of communication and may be subject to data corruption, interception and unauthorised amendment for which I-play, a trading name of Digital Bridges Ltd will accept no liability. Therefore, it will normally be inappropriate to rely on information contained on e-mail without obtaining written confirmation.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

(C) 2005. I-play is a trademark and trading name of Digital Bridges Limited. All Rights Reserved.
________________________________________________________________________
This message has been checked for all known viruses by the 
MessageLabs Virus Scanning Service. For further information visit
http://www.messagelabs.com/stats.asp

Re: Setting final strings at compile time

Posted by Wayne Cannon <wc...@sonic.net>.
Eric,

We create an almost-Java source file, e.g., Version.txt, containing 
replacement targets.  We extract version information from Subversion (or 
ClearCase, etc.) into a property with Ant, which we then <replace> in an 
Ant <copy> from Version.txt to Version.java.  The advantages of this 
approach are (1) the ability to use version control for Version.txt, (2) 
the ability to extract version information of various types (build date, 
product version number, build number, and version-control version 
number) for the compiled version, and (3) independence from the build 
system allowing you to use Ant, make, clearmake, etc., and switch 
systems if your process demands.  The disadvantage of this approach is 
that Version.txt is not automatically recognized as a Java source file 
by IDEs, such as Eclipse, based solely on its suffix.

Steve's approach seems very similar, except that he prefers to use Java 
properties files read by Java classes at run-time, instead of Java 
source files themselves.  I prefer the Java source file in order to keep 
all of the version-related information in individual Java classes, and 
not in a separable, easily editable, property file that is read at 
run-time.  For some approaches, there is definitely a benefit to the 
flexibility of an editable property file that is read at run-time.  I 
use my Version base class, and its derivatives, for version 
compatibility test methods across both sides of interfaces, as well as 
returning a string for version reporting, "Help | About" information, etc.

Regards,
Wayne

from Version.txt:
    public static final String DATE_STRING = "@date@";
    public static final String MAJOR_VERSION = "@major@";
    public static final String MINOR_VERSION = "@minor@";
    public static final String PATCH_VERSION = "@patch@";
    public static final String BUILD_NUMBER = "@build@";

resuting Version.java:
    public static final String DATE_STRING = "Tue Nov 06 00:08:51 PST 2007";
    public static final String MAJOR_VERSION = "02";
    public static final String MINOR_VERSION = "00";
    public static final String PATCH_VERSION = "00";
    public static final String BUILD_NUMBER = "143";


Frederich, Eric P21322 wrote:
> Thanks for the reply and a possible solution but this seems more like a
> hack.
> Yeah, right now I have something like
> Public static final String programName = "Java Program XYZ";
> I'd like to be able to make one where that string is "Java Program XYZ -
> Development Version" automagically using ant.
>
> If the javac command could set properties the way the java command does
> it would be nice.
> If this isn't the case I guess I'll be stuck with a "hack" like this.
> Just seems to be a pain to explicitly copy the original to a tmp
> directory, do the replacement on the file, compile it and then copy the
> original file back.
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Setting final strings at compile time

Posted by "Scot P. Floess" <fl...@mindspring.com>.
Sorry that's not completely right...

I mean

<javac srcdir = "/shared/projects/generated:/shared/projects/src/java" .../>

Scot P. Floess wrote:
> Eric:
>
> Unfortunately, you are compiling a static final String :(
>
> Perhaps a properties file distributed with your app could hold the 
> String instead?
>
> I wasn't proposing copying, replacing and then copying back...  What I 
> meant was this:
>
> I assume your source code is checked into some source control systems 
> like Subversion or CVS?  Therefore, you probably want to copy the file 
> elsewhere solely for the purpose of doing the text replacement...  
> Otherwise, the file will show it is in a modified state (for example 
> running "svn stat")...
>
> You can copy the file to a new location and add that dir as part of 
> the Java source path in the javac task...  It will pick up the copied 
> file over the other non-modified one...  Of course, you want to keep 
> the directory structure the same where you copy it over...  For 
> example lets say your source is in 
> /shared/projects/src/java/com/Main.java, you might want to copy to 
> /shared/projects/generated/com/Main.java and then do this:
>
> <javac srcdir = "/shared/projects/src/java:/shared/projects/generated" 
> .../>
>
> Does that make sense?
>
> Frederich, Eric P21322 wrote:
>> Thanks for the reply and a possible solution but this seems more like a
>> hack.
>> Yeah, right now I have something like
>> Public static final String programName = "Java Program XYZ";
>> I'd like to be able to make one where that string is "Java Program XYZ -
>> Development Version" automagically using ant.
>>
>> If the javac command could set properties the way the java command does
>> it would be nice.
>> If this isn't the case I guess I'll be stuck with a "hack" like this.
>> Just seems to be a pain to explicitly copy the original to a tmp
>> directory, do the replacement on the file, compile it and then copy the
>> original file back.
>>
>>
>>  
>>> -----Original Message-----
>>> From: Scot P. Floess
>>>
>>> Eric:
>>>
>>> You mention "final Java Strings at compile time"...
>>>
>>> Are you referring to actual Java as in:
>>>     public static final String FOO = "some text";
>>>
>>> If so, you may want to look into the <replace> task...
>>>
>>> You could do something as simple as:
>>>
>>> public class Main
>>> {
>>>     public static final String FOO = "%PROJECT NAME%";
>>> ...
>>> }
>>>
>>> <replace file = "Main.java"  token = "%PROJECT NAME%"  value = "Some 
>>> Value"/>
>>>
>>> Of course, you probably want to copy off Main.java to a new dir and 
>>> do the replacement there - compiling that copied version of course...
>>>
>>>
>>>
>>>
>>> Frederich, Eric P21322 wrote:
>>>    
>>>> Hello,
>>>>
>>>> Is there an easy way to set final Java Strings at compile time?
>>>> I know you can set properties at runtime with       
>>> -Dproperty=value but could
>>>    
>>>> I do this at compile time?
>>>> What I want to do is have one target which makes a program       
>>> called "Java
>>>    
>>>> Program XYZ - production" and another which is called "Java       
>>> Program XYZ
>>>    
>>>> - development".
>>>> Also, it would be useful to somehow get the date and time       
>>> at which it
>>>    
>>>> was compiled into the program.
>>>>
>>>> I figured this would be common, hopefully it is.  Perhaps I       
>>> just don't
>>>    
>>>> know the right words to search on Google.
>>>>
>>>> Thanks in advance,
>>>> ~Eric
>>>>       
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>> For additional commands, e-mail: user-help@ant.apache.org
>>
>>
>>   
>

-- 
Scot P. Floess
27 Lake Royale
Louisburg, NC  27549

252-478-8087 (Home)
919-754-4592 (Work)

Chief Architect JPlate   http://sourceforge.net/projects/jplate
Chief Architect JavaPIM  http://sourceforge.net/projects/javapim
QA Engineer OpenQabal    http://openqabal.dev.java.net


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Setting final strings at compile time

Posted by "Scot P. Floess" <fl...@mindspring.com>.
Eric:

Unfortunately, you are compiling a static final String :(

Perhaps a properties file distributed with your app could hold the 
String instead?

I wasn't proposing copying, replacing and then copying back...  What I 
meant was this:

I assume your source code is checked into some source control systems 
like Subversion or CVS?  Therefore, you probably want to copy the file 
elsewhere solely for the purpose of doing the text replacement...  
Otherwise, the file will show it is in a modified state (for example 
running "svn stat")...

You can copy the file to a new location and add that dir as part of the 
Java source path in the javac task...  It will pick up the copied file 
over the other non-modified one...  Of course, you want to keep the 
directory structure the same where you copy it over...  For example lets 
say your source is in /shared/projects/src/java/com/Main.java, you might 
want to copy to /shared/projects/generated/com/Main.java and then do this:

<javac srcdir = "/shared/projects/src/java:/shared/projects/generated" .../>

Does that make sense?

Frederich, Eric P21322 wrote:
> Thanks for the reply and a possible solution but this seems more like a
> hack.
> Yeah, right now I have something like
> Public static final String programName = "Java Program XYZ";
> I'd like to be able to make one where that string is "Java Program XYZ -
> Development Version" automagically using ant.
>
> If the javac command could set properties the way the java command does
> it would be nice.
> If this isn't the case I guess I'll be stuck with a "hack" like this.
> Just seems to be a pain to explicitly copy the original to a tmp
> directory, do the replacement on the file, compile it and then copy the
> original file back.
>
>
>   
>> -----Original Message-----
>> From: Scot P. Floess
>>
>> Eric:
>>
>> You mention "final Java Strings at compile time"...
>>
>> Are you referring to actual Java as in:
>>     public static final String FOO = "some text";
>>
>> If so, you may want to look into the <replace> task...
>>
>> You could do something as simple as:
>>
>> public class Main
>> {
>>     public static final String FOO = "%PROJECT NAME%";
>> ...
>> }
>>
>> <replace file = "Main.java"  token = "%PROJECT NAME%"  value = "Some 
>> Value"/>
>>
>> Of course, you probably want to copy off Main.java to a new 
>> dir and do 
>> the replacement there - compiling that copied version of course...
>>
>>
>>
>>
>> Frederich, Eric P21322 wrote:
>>     
>>> Hello,
>>>
>>> Is there an easy way to set final Java Strings at compile time?
>>> I know you can set properties at runtime with 
>>>       
>> -Dproperty=value but could
>>     
>>> I do this at compile time?
>>> What I want to do is have one target which makes a program 
>>>       
>> called "Java
>>     
>>> Program XYZ - production" and another which is called "Java 
>>>       
>> Program XYZ
>>     
>>> - development".
>>> Also, it would be useful to somehow get the date and time 
>>>       
>> at which it
>>     
>>> was compiled into the program.
>>>
>>> I figured this would be common, hopefully it is.  Perhaps I 
>>>       
>> just don't
>>     
>>> know the right words to search on Google.
>>>
>>> Thanks in advance,
>>> ~Eric
>>>       
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>
>   

-- 
Scot P. Floess
27 Lake Royale
Louisburg, NC  27549

252-478-8087 (Home)
919-754-4592 (Work)

Chief Architect JPlate   http://sourceforge.net/projects/jplate
Chief Architect JavaPIM  http://sourceforge.net/projects/javapim
QA Engineer OpenQabal    http://openqabal.dev.java.net


Re: Setting final strings at compile time

Posted by Steve Loughran <st...@apache.org>.
Frederich, Eric P21322 wrote:
> Thanks for the reply and a possible solution but this seems more like a
> hack.
> Yeah, right now I have something like
> Public static final String programName = "Java Program XYZ";
> I'd like to be able to make one where that string is "Java Program XYZ -
> Development Version" automagically using ant.
> 
> If the javac command could set properties the way the java command does
> it would be nice.
> If this isn't the case I guess I'll be stuck with a "hack" like this.
> Just seems to be a pain to explicitly copy the original to a tmp
> directory, do the replacement on the file, compile it and then copy the
> original file back.

That's really brittle.

what we do is create properties files on the fly and stick them in the 
JAR; then you read them in in your java source
  and print the values out
-- 
Steve Loughran                  http://www.1060.org/blogxter/publish/5
Author: Ant in Action           http://antbook.org/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: Setting final strings at compile time

Posted by "Frederich, Eric P21322" <er...@siemens.com>.
Thanks for the reply and a possible solution but this seems more like a
hack.
Yeah, right now I have something like
Public static final String programName = "Java Program XYZ";
I'd like to be able to make one where that string is "Java Program XYZ -
Development Version" automagically using ant.

If the javac command could set properties the way the java command does
it would be nice.
If this isn't the case I guess I'll be stuck with a "hack" like this.
Just seems to be a pain to explicitly copy the original to a tmp
directory, do the replacement on the file, compile it and then copy the
original file back.


> -----Original Message-----
> From: Scot P. Floess
> 
> Eric:
> 
> You mention "final Java Strings at compile time"...
> 
> Are you referring to actual Java as in:
>     public static final String FOO = "some text";
> 
> If so, you may want to look into the <replace> task...
> 
> You could do something as simple as:
> 
> public class Main
> {
>     public static final String FOO = "%PROJECT NAME%";
> ...
> }
> 
> <replace file = "Main.java"  token = "%PROJECT NAME%"  value = "Some 
> Value"/>
> 
> Of course, you probably want to copy off Main.java to a new 
> dir and do 
> the replacement there - compiling that copied version of course...
> 
> 
> 
> 
> Frederich, Eric P21322 wrote:
> > Hello,
> >
> > Is there an easy way to set final Java Strings at compile time?
> > I know you can set properties at runtime with 
> -Dproperty=value but could
> > I do this at compile time?
> > What I want to do is have one target which makes a program 
> called "Java
> > Program XYZ - production" and another which is called "Java 
> Program XYZ
> > - development".
> > Also, it would be useful to somehow get the date and time 
> at which it
> > was compiled into the program.
> >
> > I figured this would be common, hopefully it is.  Perhaps I 
> just don't
> > know the right words to search on Google.
> >
> > Thanks in advance,
> > ~Eric

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Setting final strings at compile time

Posted by "Scot P. Floess" <fl...@mindspring.com>.
Eric:

You mention "final Java Strings at compile time"...

Are you referring to actual Java as in:
    public static final String FOO = "some text";

If so, you may want to look into the <replace> task...

You could do something as simple as:

public class Main
{
    public static final String FOO = "%PROJECT NAME%";
...
}

<replace file = "Main.java"  token = "%PROJECT NAME%"  value = "Some 
Value"/>

Of course, you probably want to copy off Main.java to a new dir and do 
the replacement there - compiling that copied version of course...




Frederich, Eric P21322 wrote:
> Hello,
>
> Is there an easy way to set final Java Strings at compile time?
> I know you can set properties at runtime with -Dproperty=value but could
> I do this at compile time?
> What I want to do is have one target which makes a program called "Java
> Program XYZ - production" and another which is called "Java Program XYZ
> - development".
> Also, it would be useful to somehow get the date and time at which it
> was compiled into the program.
>
> I figured this would be common, hopefully it is.  Perhaps I just don't
> know the right words to search on Google.
>
> Thanks in advance,
> ~Eric
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>
>   

-- 
Scot P. Floess
27 Lake Royale
Louisburg, NC  27549

252-478-8087 (Home)
919-754-4592 (Work)

Chief Architect JPlate   http://sourceforge.net/projects/jplate
Chief Architect JavaPIM  http://sourceforge.net/projects/javapim
QA Engineer OpenQabal    http://openqabal.dev.java.net


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org