Gradle, filter resource one way for tests, another way when packaging artifact












0















I have a gradle project, with some database migration scripts.



The migration files have a variable which I replace using resource filtering. This works fine and is accomplished like this:



processResources {
filter ReplaceTokens, tokens: [
"index.refresh.period": project.property("index.refresh.period")
]
}


Now I have written a unit test that starts up an embedded database and apply the same migration scripts. However, for testing I need to substitute another value.
How can I accomplish this?



I have tried a number of different things but nothing seems to work. I should mention that I am new to Gradle so it's a trial and error approach.



Attempt #1:



test {
project.ext.setProperty('index.refresh.period', '1')
processResources
}


Attempt #2:



compileTestJava {
project.ext.setProperty('index.refresh.period', '1')
processResources
}









share|improve this question























  • resource files in src/test/resources directory help you to have different resource for test. did you tried that?

    – Rajkumar Natarajan
    Nov 27 '18 at 20:09











  • Thanks for your comment. I would like to avoid adding a copy of each database migration script to the test folder (most likely future developers will forget to do add a copy of their script to the test folder). Instead, i would like to use parameter substitution such that certain values gets substituted into the migration scripts when testing, and other values for the final artifact.

    – Hervian
    Nov 28 '18 at 12:51
















0















I have a gradle project, with some database migration scripts.



The migration files have a variable which I replace using resource filtering. This works fine and is accomplished like this:



processResources {
filter ReplaceTokens, tokens: [
"index.refresh.period": project.property("index.refresh.period")
]
}


Now I have written a unit test that starts up an embedded database and apply the same migration scripts. However, for testing I need to substitute another value.
How can I accomplish this?



I have tried a number of different things but nothing seems to work. I should mention that I am new to Gradle so it's a trial and error approach.



Attempt #1:



test {
project.ext.setProperty('index.refresh.period', '1')
processResources
}


Attempt #2:



compileTestJava {
project.ext.setProperty('index.refresh.period', '1')
processResources
}









share|improve this question























  • resource files in src/test/resources directory help you to have different resource for test. did you tried that?

    – Rajkumar Natarajan
    Nov 27 '18 at 20:09











  • Thanks for your comment. I would like to avoid adding a copy of each database migration script to the test folder (most likely future developers will forget to do add a copy of their script to the test folder). Instead, i would like to use parameter substitution such that certain values gets substituted into the migration scripts when testing, and other values for the final artifact.

    – Hervian
    Nov 28 '18 at 12:51














0












0








0








I have a gradle project, with some database migration scripts.



The migration files have a variable which I replace using resource filtering. This works fine and is accomplished like this:



processResources {
filter ReplaceTokens, tokens: [
"index.refresh.period": project.property("index.refresh.period")
]
}


Now I have written a unit test that starts up an embedded database and apply the same migration scripts. However, for testing I need to substitute another value.
How can I accomplish this?



I have tried a number of different things but nothing seems to work. I should mention that I am new to Gradle so it's a trial and error approach.



Attempt #1:



test {
project.ext.setProperty('index.refresh.period', '1')
processResources
}


Attempt #2:



compileTestJava {
project.ext.setProperty('index.refresh.period', '1')
processResources
}









share|improve this question














I have a gradle project, with some database migration scripts.



The migration files have a variable which I replace using resource filtering. This works fine and is accomplished like this:



processResources {
filter ReplaceTokens, tokens: [
"index.refresh.period": project.property("index.refresh.period")
]
}


Now I have written a unit test that starts up an embedded database and apply the same migration scripts. However, for testing I need to substitute another value.
How can I accomplish this?



I have tried a number of different things but nothing seems to work. I should mention that I am new to Gradle so it's a trial and error approach.



Attempt #1:



test {
project.ext.setProperty('index.refresh.period', '1')
processResources
}


Attempt #2:



compileTestJava {
project.ext.setProperty('index.refresh.period', '1')
processResources
}






gradle string-substitution






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 14:34









HervianHervian

731715




731715













  • resource files in src/test/resources directory help you to have different resource for test. did you tried that?

    – Rajkumar Natarajan
    Nov 27 '18 at 20:09











  • Thanks for your comment. I would like to avoid adding a copy of each database migration script to the test folder (most likely future developers will forget to do add a copy of their script to the test folder). Instead, i would like to use parameter substitution such that certain values gets substituted into the migration scripts when testing, and other values for the final artifact.

    – Hervian
    Nov 28 '18 at 12:51



















  • resource files in src/test/resources directory help you to have different resource for test. did you tried that?

    – Rajkumar Natarajan
    Nov 27 '18 at 20:09











  • Thanks for your comment. I would like to avoid adding a copy of each database migration script to the test folder (most likely future developers will forget to do add a copy of their script to the test folder). Instead, i would like to use parameter substitution such that certain values gets substituted into the migration scripts when testing, and other values for the final artifact.

    – Hervian
    Nov 28 '18 at 12:51

















resource files in src/test/resources directory help you to have different resource for test. did you tried that?

– Rajkumar Natarajan
Nov 27 '18 at 20:09





resource files in src/test/resources directory help you to have different resource for test. did you tried that?

– Rajkumar Natarajan
Nov 27 '18 at 20:09













Thanks for your comment. I would like to avoid adding a copy of each database migration script to the test folder (most likely future developers will forget to do add a copy of their script to the test folder). Instead, i would like to use parameter substitution such that certain values gets substituted into the migration scripts when testing, and other values for the final artifact.

– Hervian
Nov 28 '18 at 12:51





Thanks for your comment. I would like to avoid adding a copy of each database migration script to the test folder (most likely future developers will forget to do add a copy of their script to the test folder). Instead, i would like to use parameter substitution such that certain values gets substituted into the migration scripts when testing, and other values for the final artifact.

– Hervian
Nov 28 '18 at 12:51












1 Answer
1






active

oldest

votes


















0














I ended up copy pasting the migration scripts to the output test resources folder and do the filtering as part of this copy action.



Note that resources in the test folder takes precedence during testing. This means that the logic using these files, in my use case a database migration tool, do not need any test specific configuration due to the change below - the db migration scripts in the test resource folder simply takes precedence.



processResources {
filter ReplaceTokens, tokens: [
"index.refresh.period": project.property("index.refresh.period")
]
}

task copyFiles(type: Copy) {
println "Copying db migration scrips to test resources (in order to set shortest possible index refresh period)"
from 'src/main/resources/db/migration'
into "${buildDir}/resources/test/db/migration"
filter ReplaceTokens, tokens: [
"index.refresh.period": '1'
]
}

processTestResources {
dependsOn copyFiles
}


The project property 'index.refresh.period' is defined in my gradle.properties file as this:



index.refresh.period=60





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%2f53502019%2fgradle-filter-resource-one-way-for-tests-another-way-when-packaging-artifact%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I ended up copy pasting the migration scripts to the output test resources folder and do the filtering as part of this copy action.



    Note that resources in the test folder takes precedence during testing. This means that the logic using these files, in my use case a database migration tool, do not need any test specific configuration due to the change below - the db migration scripts in the test resource folder simply takes precedence.



    processResources {
    filter ReplaceTokens, tokens: [
    "index.refresh.period": project.property("index.refresh.period")
    ]
    }

    task copyFiles(type: Copy) {
    println "Copying db migration scrips to test resources (in order to set shortest possible index refresh period)"
    from 'src/main/resources/db/migration'
    into "${buildDir}/resources/test/db/migration"
    filter ReplaceTokens, tokens: [
    "index.refresh.period": '1'
    ]
    }

    processTestResources {
    dependsOn copyFiles
    }


    The project property 'index.refresh.period' is defined in my gradle.properties file as this:



    index.refresh.period=60





    share|improve this answer




























      0














      I ended up copy pasting the migration scripts to the output test resources folder and do the filtering as part of this copy action.



      Note that resources in the test folder takes precedence during testing. This means that the logic using these files, in my use case a database migration tool, do not need any test specific configuration due to the change below - the db migration scripts in the test resource folder simply takes precedence.



      processResources {
      filter ReplaceTokens, tokens: [
      "index.refresh.period": project.property("index.refresh.period")
      ]
      }

      task copyFiles(type: Copy) {
      println "Copying db migration scrips to test resources (in order to set shortest possible index refresh period)"
      from 'src/main/resources/db/migration'
      into "${buildDir}/resources/test/db/migration"
      filter ReplaceTokens, tokens: [
      "index.refresh.period": '1'
      ]
      }

      processTestResources {
      dependsOn copyFiles
      }


      The project property 'index.refresh.period' is defined in my gradle.properties file as this:



      index.refresh.period=60





      share|improve this answer


























        0












        0








        0







        I ended up copy pasting the migration scripts to the output test resources folder and do the filtering as part of this copy action.



        Note that resources in the test folder takes precedence during testing. This means that the logic using these files, in my use case a database migration tool, do not need any test specific configuration due to the change below - the db migration scripts in the test resource folder simply takes precedence.



        processResources {
        filter ReplaceTokens, tokens: [
        "index.refresh.period": project.property("index.refresh.period")
        ]
        }

        task copyFiles(type: Copy) {
        println "Copying db migration scrips to test resources (in order to set shortest possible index refresh period)"
        from 'src/main/resources/db/migration'
        into "${buildDir}/resources/test/db/migration"
        filter ReplaceTokens, tokens: [
        "index.refresh.period": '1'
        ]
        }

        processTestResources {
        dependsOn copyFiles
        }


        The project property 'index.refresh.period' is defined in my gradle.properties file as this:



        index.refresh.period=60





        share|improve this answer













        I ended up copy pasting the migration scripts to the output test resources folder and do the filtering as part of this copy action.



        Note that resources in the test folder takes precedence during testing. This means that the logic using these files, in my use case a database migration tool, do not need any test specific configuration due to the change below - the db migration scripts in the test resource folder simply takes precedence.



        processResources {
        filter ReplaceTokens, tokens: [
        "index.refresh.period": project.property("index.refresh.period")
        ]
        }

        task copyFiles(type: Copy) {
        println "Copying db migration scrips to test resources (in order to set shortest possible index refresh period)"
        from 'src/main/resources/db/migration'
        into "${buildDir}/resources/test/db/migration"
        filter ReplaceTokens, tokens: [
        "index.refresh.period": '1'
        ]
        }

        processTestResources {
        dependsOn copyFiles
        }


        The project property 'index.refresh.period' is defined in my gradle.properties file as this:



        index.refresh.period=60






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 12:48









        HervianHervian

        731715




        731715
































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53502019%2fgradle-filter-resource-one-way-for-tests-another-way-when-packaging-artifact%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

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)