gradletaskglobalarchetypes

gradle init project with specific struct


I want create project with custom files for few project with equal struct, for example every project should have custom css files, example bootstrap.min.css in /webapp/css, custom login page in /webapp/html, and logo image in webapp/image enter image description here How can I do it? Gradle don't have archetypes generate. How can I create some "global" task? For example when I execute gradle createSimpleProject in any dir, it generate this custom project. Can I create this use gradle init?


Solution

  • Every directory is more or less a Gradle project - in particular an empty directory is an empty Gradle project. If you run gradle tasks inside an empty directory, you'll get list of tasks, including basic init tasks or wrapper task.

    You can add to these tasks by setting up init.gradle file in your gradle directory (on Mac it's ~/.gradle/gradle.init, for other platforms see here). Adding a task to root project will allow you to call it even in empty directory. Here's sample content of init.gradle file:

    gradle.rootProject {
      task yourInit {
        group = "Helper"
        description = "Create structure for whatever project"
        doLast {
          println 'Create structure here'
        }
      }
    }
    

    Opening terminal in empty directory and running gradle yourInit will cause block in doLast to run in this directory. You can put directories and files generation there.