如何创建Java gradle项目

Pau*_*est 115 java gradle

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

它应该创建标准的maven文件夹布局,如下图所示.

使用Eclipse插件创建的Java Gradle

更新:

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

apply plugin: 'java'
apply plugin: 'eclipse'
Run Code Online (Sandbox Code Playgroud)

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

task "create-dirs" << {
   sourceSets*.java.srcDirs*.each { it.mkdirs() }
   sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}
Run Code Online (Sandbox Code Playgroud)

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

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

Mik*_*ike 277

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

gradle init --type java-library
Run Code Online (Sandbox Code Playgroud)

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

  • 正如其他答案所述,build init插件的文档可以在这里找到:http://www.gradle.org/docs/current/userguide/build_init_plugin.html (7认同)
  • 使用`mkdir -p src/{main,test}/{java,resources}` (6认同)
  • `gradle wrapper --gradle-version 4.1 init --type java-library` 如果要指定包装版本 (2认同)

roo*_*msg 19

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

请参阅:build init插件文档


rod*_*ion 13

不幸的是,你无法在一个命令中完成它.该功能有一个未解决的问题.

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

编辑

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


Pau*_*est 12

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

Gradle发布包含samples大量示例的文件夹,并且有gradle init --type basic命令参见第47章 .Build Init插件.但他们都需要一些编辑.

您也可以使用下面的模板,然后运行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"
}
Run Code Online (Sandbox Code Playgroud)

结果如下.

概观

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

编辑框