Migrating from ant to gradle - macrodef alternatives?












0















We are migrating from Ant to Gradle. In the starting Gradle looked promising, when I started migrating targets from Ant to Gradle, facing similar issues, earlier having with Ant.



The Ant Build Flow looks like -



We have a Java application - Ant used to clean, compile and build jar of the project.



Next we have few targets to copy built jar and some other resources files to windows server.Its just a windows server not a nexus like repo.



These copy targets are release based(internally using macrodefs) - if alpha, it has to copy to different folder and for stable release different folder.



Another macrodef was to just backup the existing files present in server to a backup folder before copying new files there.





Migrating to Gradle-
I liked the way Gradle does the tasks of clean, compile and build. I am using Java plugin for this.



I am facing issues in next tasks to copy files to windows server and taking backup of old files before copying new files.



In Gradle I created few custom tasks like copyJarAlpha, copyJarStable,backupJarAlpha,backupJarStable etc..



Internally they are doing the same thing just copying files from source to destination and some print statements. But it looks repetitive. I was looking for ways like macrodef in ant.



task deployAlpha {

doFirst {
isJenkinsAvailable(deployDirAlpha)
}

doFirst {
if(isDeployLibsTaskRequired(outputDir,deployDirAlpha)) {
tasks.findByName('deployLibsAlpha').execute()
}
}

doLast {
println "Deploying corejar jar to : ${deployDirAlpha}"
copy {
from "${outputDir}/${corejar}"
into "${deployDirAlpha}"
}
println "Deploying evan-core jar to : ${deployDirAlpha}/lib"
copy {
from "${externalLibsDir}/core2.jar"
into "${deployDirAlpha}/lib"
}
println "Deploying test resource files to : ${deployDirAlpha}"
copy {
from "./test-resources/logback-test.xml", "./test-resources/JUnit-TestSuiteResults.xsl"
into "${deployDirAlpha}"
}
}


}



I have similar snippets at few places. Its not looking nice.



Is there any way we can reuse methods/functions in gradle? or I am assuming wrong things here.. Or I am trying to use gradle for things its not inteded for? What could be the best way to accomplish these tasks of copying and backing up files if not using custom tasks in gradle?










share|improve this question



























    0















    We are migrating from Ant to Gradle. In the starting Gradle looked promising, when I started migrating targets from Ant to Gradle, facing similar issues, earlier having with Ant.



    The Ant Build Flow looks like -



    We have a Java application - Ant used to clean, compile and build jar of the project.



    Next we have few targets to copy built jar and some other resources files to windows server.Its just a windows server not a nexus like repo.



    These copy targets are release based(internally using macrodefs) - if alpha, it has to copy to different folder and for stable release different folder.



    Another macrodef was to just backup the existing files present in server to a backup folder before copying new files there.





    Migrating to Gradle-
    I liked the way Gradle does the tasks of clean, compile and build. I am using Java plugin for this.



    I am facing issues in next tasks to copy files to windows server and taking backup of old files before copying new files.



    In Gradle I created few custom tasks like copyJarAlpha, copyJarStable,backupJarAlpha,backupJarStable etc..



    Internally they are doing the same thing just copying files from source to destination and some print statements. But it looks repetitive. I was looking for ways like macrodef in ant.



    task deployAlpha {

    doFirst {
    isJenkinsAvailable(deployDirAlpha)
    }

    doFirst {
    if(isDeployLibsTaskRequired(outputDir,deployDirAlpha)) {
    tasks.findByName('deployLibsAlpha').execute()
    }
    }

    doLast {
    println "Deploying corejar jar to : ${deployDirAlpha}"
    copy {
    from "${outputDir}/${corejar}"
    into "${deployDirAlpha}"
    }
    println "Deploying evan-core jar to : ${deployDirAlpha}/lib"
    copy {
    from "${externalLibsDir}/core2.jar"
    into "${deployDirAlpha}/lib"
    }
    println "Deploying test resource files to : ${deployDirAlpha}"
    copy {
    from "./test-resources/logback-test.xml", "./test-resources/JUnit-TestSuiteResults.xsl"
    into "${deployDirAlpha}"
    }
    }


    }



    I have similar snippets at few places. Its not looking nice.



    Is there any way we can reuse methods/functions in gradle? or I am assuming wrong things here.. Or I am trying to use gradle for things its not inteded for? What could be the best way to accomplish these tasks of copying and backing up files if not using custom tasks in gradle?










    share|improve this question

























      0












      0








      0








      We are migrating from Ant to Gradle. In the starting Gradle looked promising, when I started migrating targets from Ant to Gradle, facing similar issues, earlier having with Ant.



      The Ant Build Flow looks like -



      We have a Java application - Ant used to clean, compile and build jar of the project.



      Next we have few targets to copy built jar and some other resources files to windows server.Its just a windows server not a nexus like repo.



      These copy targets are release based(internally using macrodefs) - if alpha, it has to copy to different folder and for stable release different folder.



      Another macrodef was to just backup the existing files present in server to a backup folder before copying new files there.





      Migrating to Gradle-
      I liked the way Gradle does the tasks of clean, compile and build. I am using Java plugin for this.



      I am facing issues in next tasks to copy files to windows server and taking backup of old files before copying new files.



      In Gradle I created few custom tasks like copyJarAlpha, copyJarStable,backupJarAlpha,backupJarStable etc..



      Internally they are doing the same thing just copying files from source to destination and some print statements. But it looks repetitive. I was looking for ways like macrodef in ant.



      task deployAlpha {

      doFirst {
      isJenkinsAvailable(deployDirAlpha)
      }

      doFirst {
      if(isDeployLibsTaskRequired(outputDir,deployDirAlpha)) {
      tasks.findByName('deployLibsAlpha').execute()
      }
      }

      doLast {
      println "Deploying corejar jar to : ${deployDirAlpha}"
      copy {
      from "${outputDir}/${corejar}"
      into "${deployDirAlpha}"
      }
      println "Deploying evan-core jar to : ${deployDirAlpha}/lib"
      copy {
      from "${externalLibsDir}/core2.jar"
      into "${deployDirAlpha}/lib"
      }
      println "Deploying test resource files to : ${deployDirAlpha}"
      copy {
      from "./test-resources/logback-test.xml", "./test-resources/JUnit-TestSuiteResults.xsl"
      into "${deployDirAlpha}"
      }
      }


      }



      I have similar snippets at few places. Its not looking nice.



      Is there any way we can reuse methods/functions in gradle? or I am assuming wrong things here.. Or I am trying to use gradle for things its not inteded for? What could be the best way to accomplish these tasks of copying and backing up files if not using custom tasks in gradle?










      share|improve this question














      We are migrating from Ant to Gradle. In the starting Gradle looked promising, when I started migrating targets from Ant to Gradle, facing similar issues, earlier having with Ant.



      The Ant Build Flow looks like -



      We have a Java application - Ant used to clean, compile and build jar of the project.



      Next we have few targets to copy built jar and some other resources files to windows server.Its just a windows server not a nexus like repo.



      These copy targets are release based(internally using macrodefs) - if alpha, it has to copy to different folder and for stable release different folder.



      Another macrodef was to just backup the existing files present in server to a backup folder before copying new files there.





      Migrating to Gradle-
      I liked the way Gradle does the tasks of clean, compile and build. I am using Java plugin for this.



      I am facing issues in next tasks to copy files to windows server and taking backup of old files before copying new files.



      In Gradle I created few custom tasks like copyJarAlpha, copyJarStable,backupJarAlpha,backupJarStable etc..



      Internally they are doing the same thing just copying files from source to destination and some print statements. But it looks repetitive. I was looking for ways like macrodef in ant.



      task deployAlpha {

      doFirst {
      isJenkinsAvailable(deployDirAlpha)
      }

      doFirst {
      if(isDeployLibsTaskRequired(outputDir,deployDirAlpha)) {
      tasks.findByName('deployLibsAlpha').execute()
      }
      }

      doLast {
      println "Deploying corejar jar to : ${deployDirAlpha}"
      copy {
      from "${outputDir}/${corejar}"
      into "${deployDirAlpha}"
      }
      println "Deploying evan-core jar to : ${deployDirAlpha}/lib"
      copy {
      from "${externalLibsDir}/core2.jar"
      into "${deployDirAlpha}/lib"
      }
      println "Deploying test resource files to : ${deployDirAlpha}"
      copy {
      from "./test-resources/logback-test.xml", "./test-resources/JUnit-TestSuiteResults.xsl"
      into "${deployDirAlpha}"
      }
      }


      }



      I have similar snippets at few places. Its not looking nice.



      Is there any way we can reuse methods/functions in gradle? or I am assuming wrong things here.. Or I am trying to use gradle for things its not inteded for? What could be the best way to accomplish these tasks of copying and backing up files if not using custom tasks in gradle?







      java gradle ant






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 9:03









      Kuldeep SinghKuldeep Singh

      3794626




      3794626
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Here's a couple of options.



          Create a plugin (See writing custom plugins)



          public class MyPlugin implements Plugin<Project> {
          void apply(Project project) {
          project.with {
          apply plugin: 'x'
          dependencies {...}
          task deployAlpha{...}
          }
          }
          }


          Usage



          apply plugin: MyPlugin 


          Or a script plugin



          apply from: 'path/to/script.gradle'


          You can also attach functions to domain objects via the ext of a domain object (see ExtraPropertiesExtension)



          allprojects {
          ext.doProjectStuff = { String arg -> return "project${arg}"}
          dependencies.ext.doSependenciesStuff = { String arg -> return "dependencies${arg}"}
          }


          Usage



          def foo = doProjectStuff('x')
          dependencies {
          def bar = doDependenciesStuff('y')
          }





          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%2f53427230%2fmigrating-from-ant-to-gradle-macrodef-alternatives%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














            Here's a couple of options.



            Create a plugin (See writing custom plugins)



            public class MyPlugin implements Plugin<Project> {
            void apply(Project project) {
            project.with {
            apply plugin: 'x'
            dependencies {...}
            task deployAlpha{...}
            }
            }
            }


            Usage



            apply plugin: MyPlugin 


            Or a script plugin



            apply from: 'path/to/script.gradle'


            You can also attach functions to domain objects via the ext of a domain object (see ExtraPropertiesExtension)



            allprojects {
            ext.doProjectStuff = { String arg -> return "project${arg}"}
            dependencies.ext.doSependenciesStuff = { String arg -> return "dependencies${arg}"}
            }


            Usage



            def foo = doProjectStuff('x')
            dependencies {
            def bar = doDependenciesStuff('y')
            }





            share|improve this answer






























              0














              Here's a couple of options.



              Create a plugin (See writing custom plugins)



              public class MyPlugin implements Plugin<Project> {
              void apply(Project project) {
              project.with {
              apply plugin: 'x'
              dependencies {...}
              task deployAlpha{...}
              }
              }
              }


              Usage



              apply plugin: MyPlugin 


              Or a script plugin



              apply from: 'path/to/script.gradle'


              You can also attach functions to domain objects via the ext of a domain object (see ExtraPropertiesExtension)



              allprojects {
              ext.doProjectStuff = { String arg -> return "project${arg}"}
              dependencies.ext.doSependenciesStuff = { String arg -> return "dependencies${arg}"}
              }


              Usage



              def foo = doProjectStuff('x')
              dependencies {
              def bar = doDependenciesStuff('y')
              }





              share|improve this answer




























                0












                0








                0







                Here's a couple of options.



                Create a plugin (See writing custom plugins)



                public class MyPlugin implements Plugin<Project> {
                void apply(Project project) {
                project.with {
                apply plugin: 'x'
                dependencies {...}
                task deployAlpha{...}
                }
                }
                }


                Usage



                apply plugin: MyPlugin 


                Or a script plugin



                apply from: 'path/to/script.gradle'


                You can also attach functions to domain objects via the ext of a domain object (see ExtraPropertiesExtension)



                allprojects {
                ext.doProjectStuff = { String arg -> return "project${arg}"}
                dependencies.ext.doSependenciesStuff = { String arg -> return "dependencies${arg}"}
                }


                Usage



                def foo = doProjectStuff('x')
                dependencies {
                def bar = doDependenciesStuff('y')
                }





                share|improve this answer















                Here's a couple of options.



                Create a plugin (See writing custom plugins)



                public class MyPlugin implements Plugin<Project> {
                void apply(Project project) {
                project.with {
                apply plugin: 'x'
                dependencies {...}
                task deployAlpha{...}
                }
                }
                }


                Usage



                apply plugin: MyPlugin 


                Or a script plugin



                apply from: 'path/to/script.gradle'


                You can also attach functions to domain objects via the ext of a domain object (see ExtraPropertiesExtension)



                allprojects {
                ext.doProjectStuff = { String arg -> return "project${arg}"}
                dependencies.ext.doSependenciesStuff = { String arg -> return "dependencies${arg}"}
                }


                Usage



                def foo = doProjectStuff('x')
                dependencies {
                def bar = doDependenciesStuff('y')
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 '18 at 14:31

























                answered Nov 22 '18 at 9:32









                lance-javalance-java

                16.5k12961




                16.5k12961






























                    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%2f53427230%2fmigrating-from-ant-to-gradle-macrodef-alternatives%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

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Redirect URL with Chrome Remote Debugging Android Devices

                    Dieringhausen