首页 文章

如何创建Java gradle项目

提问于
浏览
110

如何从命令行创建Java Gradle项目?

它应该如下图所示创建standard maven folder layout .

Java Gradle created with Eclipse plugin

更新:

0.1 . 从http://www.gradle.org/docs/current/userguide/tutorial_java_projects.html我需要用2行创建文件 build.gradle

apply plugin:'java'
申请插件:'eclipse'

0.2 . 在下面添加 build.gradle 任务,而不是执行 gradle create-dirs

task“create-dirs”<< {
sourceSets * .java.srcDirs * .each {it.mkdirs()}
sourceSets * .resources.srcDirs * .each {it.mkdirs()}
}

0.3 . 然后运行 gradle eclipse (或相应的字符串到其他IDE插件配置)

那么有没有办法在一个命令中做到这一点?

7 回答

  • 1

    要创建Java项目:创建一个新项目目录,跳转到该项目并执行

    gradle init --type java-library
    

    将构建源文件夹和Gradle构建文件(包括包装器) .

  • 12

    朋友们正在尽力解决所有问题;-) . 他们最近(从1.9开始)添加了一个新功能(孵化):“build init”插件 .

    见:build init plugin documentation

  • 18

    不幸的是,你无法在一个命令中完成它 . 有一个开放issue for the very feature .

    目前你必须手工完成 . 如果您需要经常这样做,您可以创建一个custom gradle plugin,或者只是准备您自己的项目框架并在需要时复制它 .

    EDIT

    上述JIRA问题已于2013年5月1日解决,并已修订为1.7-rc-1 . 有关Build Init Plugin的文档可用,但它表明此功能仍处于"incubating"生命周期中 .

  • -1

    最后在比较了所有解决方案后,我认为从 build.gradle 文件开始可以很方便 .

    Gradle分发有 samples 文件夹,里面有很多例子,有 gradle init --type basic 命令见Chapter 47. Build Init Plugin . 但他们都需要一些编辑 .

    您也可以使用下面的template,然后运行 gradle initSourceFolders eclipse

    /*
    * Nodeclipse/Enide build.gradle template for basic Java project
    *   https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.enide.editors.gradle/docs/java/basic/build.gradle
    * Initially asked on
    *   http://stackoverflow.com/questions/14017364/how-to-create-java-gradle-project
    * Usage
    *   1. create folder (or general Eclipse project) and put this file inside
    *   2. run `gradle initSourceFolders eclipse` or `gradle initSourceFolders idea`
    * @author Paul Verest; 
    * based on `gradle init --type basic`, that does not create source folders 
    */
    
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    
    task initSourceFolders { // add << before { to prevent executing during configuration phase
       sourceSets*.java.srcDirs*.each { it.mkdirs() }
       sourceSets*.resources.srcDirs*.each { it.mkdirs() }
    }
    
    task wrapper(type: Wrapper) {
        gradleVersion = '1.11'
    }
    
    // In this section you declare where to find the dependencies of your project
    repositories {
        // Use Maven Central for resolving your dependencies.
        // You can declare any Maven/Ivy/file repository here.
        mavenCentral()
    }
    
    // In this section you declare the dependencies for your production and test code
    dependencies {
        //compile fileTree(dir: 'libs', include: '*.jar')
        // The production code uses the SLF4J logging API at compile time
        //compile 'org.slf4j:slf4j-api:1.7.5'
    
        // Declare the dependency for your favourite test framework you want to use in your tests.
        // TestNG is also supported by the Gradle Test task. Just change the
        // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
        // 'test.useTestNG()' to your build script.
        testCompile "junit:junit:4.11"
    }
    

    结果如下 .

    overview

    这可以在没有Eclipse的任何Gradle插件的情况下使用,
    (Enide) Gradle for Eclipse, Jetty, Android替代Gradle Integration for Eclipse

    editbox

  • 263

    如果您正在使用Eclipse,对于现有项目(具有 build.gradle 文件),您只需键入 gradle eclipse 即可为该项目创建所有Eclipse文件和文件夹 .

    它会为您处理所有依赖项,并将它们添加到Eclipse中的项目资源路径中 .

  • 11

    我可以使用 build.gradle 中的groovy方法来处理它,为java,资源和测试创建所有源文件夹 . 然后我将它设置为在 gradle eclipse 任务之前运行 .

    eclipseClasspath.doFirst {
        initSourceFolders()
    }
    
    def initSourceFolders() {
        sourceSets*.java.srcDirs*.each { it.mkdirs() }
        sourceSets*.resources.srcDirs*.each { it.mkdirs() }
    }
    

    现在我们可以用一个命令设置一个新的gradle Java EE项目来进行eclipse . 我把这个例子放在GitHub

  • 1

    我刚刚尝试使用Eclipse Neon.1和Gradle:

    ------------------------------------------------------------
    Gradle 3.2.1
    ------------------------------------------------------------
    
    Build time:   2016-11-22 15:19:54 UTC
    Revision:     83b485b914fd4f335ad0e66af9d14aad458d2cc5
    
    Groovy:       2.4.7
    Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
    JVM:          1.8.0_112 (Oracle Corporation 25.112-b15)
    OS:           Windows 10 10.0 amd64
    

    enter image description here

    在带有Java版本的Windows 10上:

    C:\FDriveKambiz\repo\gradle-gen-project>java -version
    java version "1.8.0_112"
    Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
    Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)
    

    正如你在Eclipse中看到的那样,它失败了 . 但是在Intellij中像一只飙升的老鹰一样航行......我不知道Intellij,并且是日食的忠实粉丝,但是普通的家伙,这意味着没有一个测试Neon.1用于最简单的用例......导入一个gradle项目 . 这还不够好 . 我正在转向Intellij进行gradle项目:

    enter image description here

相关问题