【问题标题】:Can gradle create the java project directory structure?gradle可以创建java项目目录结构吗?
【发布时间】:2014-06-18 03:33:52
【问题描述】:
gradle 1.12
Fedora 20

我第一次使用 gradle 来构建我的 java 程序。我正在使用 emacs,因为我不喜欢使用 eclipse。因为我喜欢在命令行上做任何事情。

Gradle 期望在 src/main/java 下找到您的生产源代码,在 src/test/java 下找到您的测试源代码。另外,src/main/resources下的任何文件

我想知道有没有可以在 gradle 中使用的命令来让它自动创建这个项目结构?

我可以写一个脚本文件,但在我这样做之前我想知道 gradle 是否可以做到。

非常感谢,

【问题讨论】:

    标签: java gradle


    【解决方案1】:

    目前没有创建目录结构的内置方法,但gradle init 可能会在某个时候处理这​​个问题。

    更新:现在可以通过Gradle init plugin 获得。 一个示例用法是:

    gradle init --type java-library
    

    【讨论】:

      【解决方案2】:

      你可以使用gradle-templates

      第 1 步:将模板插件添加到 build.gradle

      group 'com.hireartists.consumer.repository'
      version '1.0-SNAPSHOT'
      
      apply plugin: 'java'
      apply plugin: 'idea'
      
      sourceCompatibility = 1.8
      
      repositories {
          mavenCentral()
      }
      
      dependencies {
          compile 'org.springframework:spring-webmvc:4.2.1.RELEASE'
          compile 'org.apache.kafka:kafka_2.10:0.8.2.2'
          compile 'javax.servlet:jstl:1.2'
          testCompile group: 'junit', name: 'junit', version: '4.11'
      }
      
      buildscript {
          repositories {
              maven {
                  url 'http://dl.bintray.com/cjstehno/public'
              }
          }
          dependencies {
              classpath 'gradle-templates:gradle-templates:1.5'
          }
      }
      
      apply plugin:'templates' 
      

      第 2 步:查看您需要的任务

      ./gradlew tasks
      
      Template tasks
      --------------
      createGradlePlugin - Creates a new Gradle Plugin project in a new directory named after your project.
      createGroovyClass - Creates a new Groovy class in the current project.
      createGroovyProject - Creates a new Gradle Groovy project in a new directory named after your project.
      createJavaClass - Creates a new Java class in the current project.
      createJavaProject - Creates a new Gradle Java project in a new directory named after your project.
      createScalaClass - Creates a new Scala class in the current project.
      createScalaObject - Creates a new Scala object in the current project.
      createScalaProject - Creates a new Gradle Scala project in a new directory named after your project.
      createWebappProject - Creates a new Gradle Webapp project in a new directory named after your project.
      exportAllTemplates - Exports all the default template files into the current directory.
      exportGroovyTemplates - Exports the default groovy template files into the current directory.
      exportJavaTemplates - Exports the default java template files into the current directory.
      exportPluginTemplates - Exports the default plugin template files into the current directory.
      exportScalaTemplates - Exports the default scala template files into the current directory.
      exportWebappTemplates - Exports the default webapp template files into the current directory.
      initGradlePlugin - Initializes a new Gradle Plugin project in the current directory.
      initGroovyProject - Initializes a new Gradle Groovy project in the current directory.
      initJavaProject - Initializes a new Gradle Java project in the current directory.
      initScalaProject - Initializes a new Gradle Scala project in the current directory.
      initWebappProject - Initializes a new Gradle Webapp project in the current directory.
      

      第 3 步:应用任务

      ./gradlew initWebappProject
      > Building 0% > :initWebappProject
      templates> Use Jetty Plugin? (Y|n) [n] n
      :initWebappProject
      
      BUILD SUCCESSFUL
      
      Total time: 12.057 secs
      

      第 4 步:您现在可以看到文件夹结构

      prayag-top:hire-artists-consumer prayagupd$ ll src/main/
      total 0
      drwxr-xr-x  2 prayagupd  staff   68 Oct 31 12:48 java
      drwxr-xr-x  2 prayagupd  staff   68 Oct 31 12:48 resources
      drwxr-xr-x  3 prayagupd  staff  102 Oct 31 12:48 webapp
      

      更多信息请阅读https://github.com/townsfolk/gradle-templates#installation

      【讨论】:

        猜你喜欢
        • 2012-05-21
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 2017-12-03
        • 2013-07-11
        • 1970-01-01
        • 1970-01-01
        • 2012-12-10
        相关资源
        最近更新 更多