How to remove commented properties from properties file using ant build?












0














I want to remove commented properties from properties file through ant build. For security purpose, I don't want to expose my production properties on sandbox servers.



Properties file:



#production properties
#redis.master.url=redis.prod.master.compny.com
#redis.slave.url=redis.prod.slave.compny.com

#sandboxproperties
redis.master.url=redis.sandbox.master.compny.com
redis.slave.url=redis.sandbox.slave.compny.com


so, my war package should have following properties file:



redis.master.url=redis.sandbox.master.compny.com
redis.slave.url=redis.sandbox.slave.compny.com









share|improve this question






















  • There are better ways to do this. I would make two property files, and have different Ant targets like <target name="sandbox"> and <target name="production">, which each copy the desired property file into the build under the name expected by the application.
    – VGR
    Dec 27 at 15:57










  • sounds good; so the production properties would not be packaged in the archive if set the target to sandbox ?
    – being_ethereal
    Dec 27 at 16:51












  • Yes, that is correct.
    – VGR
    Dec 27 at 16:56
















0














I want to remove commented properties from properties file through ant build. For security purpose, I don't want to expose my production properties on sandbox servers.



Properties file:



#production properties
#redis.master.url=redis.prod.master.compny.com
#redis.slave.url=redis.prod.slave.compny.com

#sandboxproperties
redis.master.url=redis.sandbox.master.compny.com
redis.slave.url=redis.sandbox.slave.compny.com


so, my war package should have following properties file:



redis.master.url=redis.sandbox.master.compny.com
redis.slave.url=redis.sandbox.slave.compny.com









share|improve this question






















  • There are better ways to do this. I would make two property files, and have different Ant targets like <target name="sandbox"> and <target name="production">, which each copy the desired property file into the build under the name expected by the application.
    – VGR
    Dec 27 at 15:57










  • sounds good; so the production properties would not be packaged in the archive if set the target to sandbox ?
    – being_ethereal
    Dec 27 at 16:51












  • Yes, that is correct.
    – VGR
    Dec 27 at 16:56














0












0








0







I want to remove commented properties from properties file through ant build. For security purpose, I don't want to expose my production properties on sandbox servers.



Properties file:



#production properties
#redis.master.url=redis.prod.master.compny.com
#redis.slave.url=redis.prod.slave.compny.com

#sandboxproperties
redis.master.url=redis.sandbox.master.compny.com
redis.slave.url=redis.sandbox.slave.compny.com


so, my war package should have following properties file:



redis.master.url=redis.sandbox.master.compny.com
redis.slave.url=redis.sandbox.slave.compny.com









share|improve this question













I want to remove commented properties from properties file through ant build. For security purpose, I don't want to expose my production properties on sandbox servers.



Properties file:



#production properties
#redis.master.url=redis.prod.master.compny.com
#redis.slave.url=redis.prod.slave.compny.com

#sandboxproperties
redis.master.url=redis.sandbox.master.compny.com
redis.slave.url=redis.sandbox.slave.compny.com


so, my war package should have following properties file:



redis.master.url=redis.sandbox.master.compny.com
redis.slave.url=redis.sandbox.slave.compny.com






java ant properties-file






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 27 at 14:21









being_ethereal

357212




357212












  • There are better ways to do this. I would make two property files, and have different Ant targets like <target name="sandbox"> and <target name="production">, which each copy the desired property file into the build under the name expected by the application.
    – VGR
    Dec 27 at 15:57










  • sounds good; so the production properties would not be packaged in the archive if set the target to sandbox ?
    – being_ethereal
    Dec 27 at 16:51












  • Yes, that is correct.
    – VGR
    Dec 27 at 16:56


















  • There are better ways to do this. I would make two property files, and have different Ant targets like <target name="sandbox"> and <target name="production">, which each copy the desired property file into the build under the name expected by the application.
    – VGR
    Dec 27 at 15:57










  • sounds good; so the production properties would not be packaged in the archive if set the target to sandbox ?
    – being_ethereal
    Dec 27 at 16:51












  • Yes, that is correct.
    – VGR
    Dec 27 at 16:56
















There are better ways to do this. I would make two property files, and have different Ant targets like <target name="sandbox"> and <target name="production">, which each copy the desired property file into the build under the name expected by the application.
– VGR
Dec 27 at 15:57




There are better ways to do this. I would make two property files, and have different Ant targets like <target name="sandbox"> and <target name="production">, which each copy the desired property file into the build under the name expected by the application.
– VGR
Dec 27 at 15:57












sounds good; so the production properties would not be packaged in the archive if set the target to sandbox ?
– being_ethereal
Dec 27 at 16:51






sounds good; so the production properties would not be packaged in the archive if set the target to sandbox ?
– being_ethereal
Dec 27 at 16:51














Yes, that is correct.
– VGR
Dec 27 at 16:56




Yes, that is correct.
– VGR
Dec 27 at 16:56












3 Answers
3






active

oldest

votes


















0














As per Ant docs:




Apache Ant provides an optional task for editing property files. This is very useful when wanting to make unattended modifications to configuration files for application servers and applications. Currently, the task maintains a working property file with the ability to add properties or make changes to existing ones. Since Ant 1.8.0 comments and layout of the original properties file are preserved.




So depending on which version of ant build you are using you may be able to remove comments from properties file.






share|improve this answer





















  • thanks, I'll look into it.
    – being_ethereal
    Dec 27 at 16:54



















0














You could do it using a script in ant:



<macrodef name="remove-properties-comments">
<attribute name="inFile" />
<attribute name="outFile" />
<sequential>
<script language="javascript">
<![CDATA[
// get the arguments
var inFile = "@{inFile}"
var outFile = "@{outFile}"

// or get properties from the ant environment
// eg: <property name="property.from.ant.project" value="value" />
// var antProp = project.getProperty("property.from.ant.project");

// load Java types
var File = Java.type("java.io.File")
var PrintWriter = Java.type("java.io.PrintWriter")
var Scanner = Java.type("java.util.Scanner")

// init reader and writer
var reader = new Scanner(new File(inFile))
var writer = new PrintWriter(outFile)

// if previous line ended in '' then it is a muliline property
// so the following line should always be included
var multiline = false
while (reader.hasNextLine()) {
var line = reader.nextLine();
// you could exclude blank lines too if you want
if (multiline || !(line.startsWith("#") || line.startsWith("!"))) {
writer.println(line);
}
multiline = line.endsWith("\");
}
]]>
</script>
</sequential>
</macrodef>

<target name="test">
<remove-properties-comments inFile="path/to/inFile.properties" outFile="path/to/outFile.properties" />
</target>





share|improve this answer





















  • thanks xtratic, I found a way(posted my answer) that will work for every file in the provided directory.
    – being_ethereal
    2 days ago



















0














I figured this out simply by using replaceregexp.



<target>
<replaceregexp match="n#(.*)" replace="" flags="g" byline="false">
<fileset dir="${build.home}/WEB-INF/classes" includes="**/*.properties" />
</replaceregexp>
</target>


Here n#(.*) matches the <newline>(n) followed by # followed by any set of characters(*).






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53946542%2fhow-to-remove-commented-properties-from-properties-file-using-ant-build%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    As per Ant docs:




    Apache Ant provides an optional task for editing property files. This is very useful when wanting to make unattended modifications to configuration files for application servers and applications. Currently, the task maintains a working property file with the ability to add properties or make changes to existing ones. Since Ant 1.8.0 comments and layout of the original properties file are preserved.




    So depending on which version of ant build you are using you may be able to remove comments from properties file.






    share|improve this answer





















    • thanks, I'll look into it.
      – being_ethereal
      Dec 27 at 16:54
















    0














    As per Ant docs:




    Apache Ant provides an optional task for editing property files. This is very useful when wanting to make unattended modifications to configuration files for application servers and applications. Currently, the task maintains a working property file with the ability to add properties or make changes to existing ones. Since Ant 1.8.0 comments and layout of the original properties file are preserved.




    So depending on which version of ant build you are using you may be able to remove comments from properties file.






    share|improve this answer





















    • thanks, I'll look into it.
      – being_ethereal
      Dec 27 at 16:54














    0












    0








    0






    As per Ant docs:




    Apache Ant provides an optional task for editing property files. This is very useful when wanting to make unattended modifications to configuration files for application servers and applications. Currently, the task maintains a working property file with the ability to add properties or make changes to existing ones. Since Ant 1.8.0 comments and layout of the original properties file are preserved.




    So depending on which version of ant build you are using you may be able to remove comments from properties file.






    share|improve this answer












    As per Ant docs:




    Apache Ant provides an optional task for editing property files. This is very useful when wanting to make unattended modifications to configuration files for application servers and applications. Currently, the task maintains a working property file with the ability to add properties or make changes to existing ones. Since Ant 1.8.0 comments and layout of the original properties file are preserved.




    So depending on which version of ant build you are using you may be able to remove comments from properties file.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 27 at 14:57









    Yug Singh

    1,2872725




    1,2872725












    • thanks, I'll look into it.
      – being_ethereal
      Dec 27 at 16:54


















    • thanks, I'll look into it.
      – being_ethereal
      Dec 27 at 16:54
















    thanks, I'll look into it.
    – being_ethereal
    Dec 27 at 16:54




    thanks, I'll look into it.
    – being_ethereal
    Dec 27 at 16:54













    0














    You could do it using a script in ant:



    <macrodef name="remove-properties-comments">
    <attribute name="inFile" />
    <attribute name="outFile" />
    <sequential>
    <script language="javascript">
    <![CDATA[
    // get the arguments
    var inFile = "@{inFile}"
    var outFile = "@{outFile}"

    // or get properties from the ant environment
    // eg: <property name="property.from.ant.project" value="value" />
    // var antProp = project.getProperty("property.from.ant.project");

    // load Java types
    var File = Java.type("java.io.File")
    var PrintWriter = Java.type("java.io.PrintWriter")
    var Scanner = Java.type("java.util.Scanner")

    // init reader and writer
    var reader = new Scanner(new File(inFile))
    var writer = new PrintWriter(outFile)

    // if previous line ended in '' then it is a muliline property
    // so the following line should always be included
    var multiline = false
    while (reader.hasNextLine()) {
    var line = reader.nextLine();
    // you could exclude blank lines too if you want
    if (multiline || !(line.startsWith("#") || line.startsWith("!"))) {
    writer.println(line);
    }
    multiline = line.endsWith("\");
    }
    ]]>
    </script>
    </sequential>
    </macrodef>

    <target name="test">
    <remove-properties-comments inFile="path/to/inFile.properties" outFile="path/to/outFile.properties" />
    </target>





    share|improve this answer





















    • thanks xtratic, I found a way(posted my answer) that will work for every file in the provided directory.
      – being_ethereal
      2 days ago
















    0














    You could do it using a script in ant:



    <macrodef name="remove-properties-comments">
    <attribute name="inFile" />
    <attribute name="outFile" />
    <sequential>
    <script language="javascript">
    <![CDATA[
    // get the arguments
    var inFile = "@{inFile}"
    var outFile = "@{outFile}"

    // or get properties from the ant environment
    // eg: <property name="property.from.ant.project" value="value" />
    // var antProp = project.getProperty("property.from.ant.project");

    // load Java types
    var File = Java.type("java.io.File")
    var PrintWriter = Java.type("java.io.PrintWriter")
    var Scanner = Java.type("java.util.Scanner")

    // init reader and writer
    var reader = new Scanner(new File(inFile))
    var writer = new PrintWriter(outFile)

    // if previous line ended in '' then it is a muliline property
    // so the following line should always be included
    var multiline = false
    while (reader.hasNextLine()) {
    var line = reader.nextLine();
    // you could exclude blank lines too if you want
    if (multiline || !(line.startsWith("#") || line.startsWith("!"))) {
    writer.println(line);
    }
    multiline = line.endsWith("\");
    }
    ]]>
    </script>
    </sequential>
    </macrodef>

    <target name="test">
    <remove-properties-comments inFile="path/to/inFile.properties" outFile="path/to/outFile.properties" />
    </target>





    share|improve this answer





















    • thanks xtratic, I found a way(posted my answer) that will work for every file in the provided directory.
      – being_ethereal
      2 days ago














    0












    0








    0






    You could do it using a script in ant:



    <macrodef name="remove-properties-comments">
    <attribute name="inFile" />
    <attribute name="outFile" />
    <sequential>
    <script language="javascript">
    <![CDATA[
    // get the arguments
    var inFile = "@{inFile}"
    var outFile = "@{outFile}"

    // or get properties from the ant environment
    // eg: <property name="property.from.ant.project" value="value" />
    // var antProp = project.getProperty("property.from.ant.project");

    // load Java types
    var File = Java.type("java.io.File")
    var PrintWriter = Java.type("java.io.PrintWriter")
    var Scanner = Java.type("java.util.Scanner")

    // init reader and writer
    var reader = new Scanner(new File(inFile))
    var writer = new PrintWriter(outFile)

    // if previous line ended in '' then it is a muliline property
    // so the following line should always be included
    var multiline = false
    while (reader.hasNextLine()) {
    var line = reader.nextLine();
    // you could exclude blank lines too if you want
    if (multiline || !(line.startsWith("#") || line.startsWith("!"))) {
    writer.println(line);
    }
    multiline = line.endsWith("\");
    }
    ]]>
    </script>
    </sequential>
    </macrodef>

    <target name="test">
    <remove-properties-comments inFile="path/to/inFile.properties" outFile="path/to/outFile.properties" />
    </target>





    share|improve this answer












    You could do it using a script in ant:



    <macrodef name="remove-properties-comments">
    <attribute name="inFile" />
    <attribute name="outFile" />
    <sequential>
    <script language="javascript">
    <![CDATA[
    // get the arguments
    var inFile = "@{inFile}"
    var outFile = "@{outFile}"

    // or get properties from the ant environment
    // eg: <property name="property.from.ant.project" value="value" />
    // var antProp = project.getProperty("property.from.ant.project");

    // load Java types
    var File = Java.type("java.io.File")
    var PrintWriter = Java.type("java.io.PrintWriter")
    var Scanner = Java.type("java.util.Scanner")

    // init reader and writer
    var reader = new Scanner(new File(inFile))
    var writer = new PrintWriter(outFile)

    // if previous line ended in '' then it is a muliline property
    // so the following line should always be included
    var multiline = false
    while (reader.hasNextLine()) {
    var line = reader.nextLine();
    // you could exclude blank lines too if you want
    if (multiline || !(line.startsWith("#") || line.startsWith("!"))) {
    writer.println(line);
    }
    multiline = line.endsWith("\");
    }
    ]]>
    </script>
    </sequential>
    </macrodef>

    <target name="test">
    <remove-properties-comments inFile="path/to/inFile.properties" outFile="path/to/outFile.properties" />
    </target>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 27 at 18:26









    xtratic

    2,3911822




    2,3911822












    • thanks xtratic, I found a way(posted my answer) that will work for every file in the provided directory.
      – being_ethereal
      2 days ago


















    • thanks xtratic, I found a way(posted my answer) that will work for every file in the provided directory.
      – being_ethereal
      2 days ago
















    thanks xtratic, I found a way(posted my answer) that will work for every file in the provided directory.
    – being_ethereal
    2 days ago




    thanks xtratic, I found a way(posted my answer) that will work for every file in the provided directory.
    – being_ethereal
    2 days ago











    0














    I figured this out simply by using replaceregexp.



    <target>
    <replaceregexp match="n#(.*)" replace="" flags="g" byline="false">
    <fileset dir="${build.home}/WEB-INF/classes" includes="**/*.properties" />
    </replaceregexp>
    </target>


    Here n#(.*) matches the <newline>(n) followed by # followed by any set of characters(*).






    share|improve this answer




























      0














      I figured this out simply by using replaceregexp.



      <target>
      <replaceregexp match="n#(.*)" replace="" flags="g" byline="false">
      <fileset dir="${build.home}/WEB-INF/classes" includes="**/*.properties" />
      </replaceregexp>
      </target>


      Here n#(.*) matches the <newline>(n) followed by # followed by any set of characters(*).






      share|improve this answer


























        0












        0








        0






        I figured this out simply by using replaceregexp.



        <target>
        <replaceregexp match="n#(.*)" replace="" flags="g" byline="false">
        <fileset dir="${build.home}/WEB-INF/classes" includes="**/*.properties" />
        </replaceregexp>
        </target>


        Here n#(.*) matches the <newline>(n) followed by # followed by any set of characters(*).






        share|improve this answer














        I figured this out simply by using replaceregexp.



        <target>
        <replaceregexp match="n#(.*)" replace="" flags="g" byline="false">
        <fileset dir="${build.home}/WEB-INF/classes" includes="**/*.properties" />
        </replaceregexp>
        </target>


        Here n#(.*) matches the <newline>(n) followed by # followed by any set of characters(*).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        being_ethereal

        357212




        357212






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53946542%2fhow-to-remove-commented-properties-from-properties-file-using-ant-build%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas