Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 242623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:51:11+00:00 2026-05-11T20:51:11+00:00

My Java app displays its version number in a few places, so I store

  • 0

My Java app displays its version number in a few places, so I store that as a public final variable in the app’s main class. Is there any way to access that in my Ant build tasks? In the spirit of automating the build process, I’d like Ant to automatically name the zipped distributable with the current version number, but I’m not sure how. I’m guessing it would look something like…

<target name="createZip" depends="build">
    <zip destfile="../dist/MyApp_${MyApp.version}.zip">
        <fileset dir="../dist">
            <include name="**/MyApp"/>
        </fileset>
    </zip>
</target>

I’m just not sure what to put in the place of ${MyApp.version}. I realize I could put this in the build properties file, but it would be a lot more convenient to be able to pull it straight from the class file I’m already storing it in.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:51:11+00:00Added an answer on May 11, 2026 at 8:51 pm

    I don’t think there are any built in Ant tasks that do what you want. However, you could roll your own, taking advantage of the extensible nature of Ant.

    I’ve hacked up a really (and I do mean really) dirty example you can use as a spring board to a proper Task.

    package q1015732;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.lang.reflect.Field;
    
    import org.apache.tools.ant.BuildException;
    import org.apache.tools.ant.Task;
    
    /**
     * Custom ant task that binds a property name to a static member of a class file.
     * 
     * @author Montrose
     */
    public class BindPropertyTask extends Task{
        private String _classFile = null;
        private String _fieldName = null;
        private String _propName = null;
    
        /**
         * Set the field name of the class.
         * 
         * @param fieldName
         */
        public void setFieldName(String fieldName){
            _fieldName = fieldName;
        }
    
        /**
         * The class file.
         * @param classFile
         */
        public void setClassFile(String classFile){
            _classFile = classFile;
        }
    
        /**
         * The property name to bind
         * @param propName
         */
        public void setProperty(String propName)
        {
            _propName = propName;
        }
    
        /**
         * Load the class file, and grab the value of the field.
         * 
         * Throws exceptions if classfile, fieldname, or property have not been set.
         * 
         * Throws more execeptions if classfile does not exist, the field does not exist, or the field is not static.
         */
        public void execute() throws BuildException{
            if(_classFile == null) throw new BuildException("ClassFile is a required attribute");
            if(_fieldName == null) throw new BuildException("FieldName is a required attribute");
            if(_propName == null)  throw new BuildException("Property is  required attribute");
    
            CustomLoader loader = new CustomLoader();
            Class toInspect = null;
            Field toBind = null;
            Object value = null;
    
            try {
                toInspect = loader.loadClass(new FileInputStream(_classFile));
            } catch (Exception e) {
                throw new BuildException("Couldn't load class ["+e.getMessage()+"], in ["+(new File(_classFile).getAbsolutePath())+"]");
            }
    
            try{
                toBind = toInspect.getField(_fieldName);
            }catch(NoSuchFieldException e){
                throw new BuildException("Couldn't find field, '"+_fieldName+"'");
            }
    
            //Let us bind to private/protected/package-private fields
            toBind.setAccessible(true);
    
            try{
                value = toBind.get(null);
            }catch(NullPointerException e){
                throw new BuildException("Field is not static");
            } catch (Exception e) {
                throw new BuildException("Unable to access field ["+e.getMessage()+"]");
            }
    
            if(value != null)
                this.getProject().setProperty(_propName, value.toString());
            else
                this.getProject().setProperty(_propName, null);
        }
    
        /**
         * Custom class loader, for loading a class directly from a file.
         * 
         * This is hacky and relies on deprecated methods, be wary.
         * 
         * @author Montrose
         */
        class CustomLoader extends ClassLoader{
            public CustomLoader(){
                super(ClassLoader.getSystemClassLoader());
            }
    
            /**
             * Warning, here be (deprecated) dragons.
             * @param in
             * @return
             * @throws Exception
             */
            @SuppressWarnings("deprecation")
            public Class loadClass(InputStream in) throws Exception{
                byte[] classData = loadData(in);
                return this.defineClass(classData, 0, classData.length);
            }
    
            private byte[] loadData(InputStream in) throws Exception{
                byte[] buffer = new byte[1024];
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int i;
    
    
                while((i = in.read(buffer)) != -1){
                    out.write(buffer, 0, i);
                }
    
                return out.toByteArray();
            }
        }
    }
    

    An example build file using this task:

    <project name="q1015732">
    
    <target name="test">
    <taskdef name="static_bind" classname="q1015732.BindPropertyTask" />
    
    <static_bind fieldname="StringBind" classfile="C:\Users\Montrose\workspace\StackOverflow Questions\q1015732\test\DummyMain.class" property="string.value" />
    <static_bind fieldname="IntBind"    classfile="C:\Users\Montrose\workspace\StackOverflow Questions\q1015732\test\DummyMain.class" property="int.value" />
    <static_bind fieldname="DoubleBind" classfile="C:\Users\Montrose\workspace\StackOverflow Questions\q1015732\test\DummyMain.class" property="double.value" />
    
    <echo message="StringBind: ${string.value}" />
    <echo message="IntBind:    ${int.value}" />
    <echo message="DoubleBind: ${double.value}" />
    
    </target>
    
    </project>
    

    DummyMain.java:

    package q1015732.test;
    
    public class DummyMain {
        public static String StringBind = "I'm a String!";
        public static int    IntBind    = 1024;
        public static double DoubleBind = 3.14159;
    }
    

    The result of a build using the Ant file:

    test:
    [echo] StringBind: I’m a String!
    [echo] IntBind: 1024
    [echo] DoubleBind: 3.14159
    BUILD SUCCESSFUL

    There are a number of random problems with this task: it relies on deprecated methods, it takes files instead of class names, and the error reporting leaves a bit to be desired. However, you should get the gist of what is needed for a custom task that solves your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I've got a code that lists the running application on a win32 box, and
I am new to web programming, coming from a video game development background (c++),
Whenever I try to write graphical programs (whether a game or really any GUI
If we are to separate our web server and app server, would we need

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.