Gradle:使用 init 初始化 Java 项目


#Gradle


创建 Java Application 项目

执行 gradle init --type java-application,根据提示输入内容即可。

▶ gradle init --type java-application

Select build script DSL:
  1: groovy
  2: kotlin
Enter selection (default: groovy) [1..2] 1

Select test framework:
  1: junit
  2: testng
  3: spock
Enter selection (default: junit) [1..3] 1

Project name (default: gradle-demo-0004): gradle-demo
Source package (default: gradle.demo): demo

BUILD SUCCESSFUL in 33s
2 actionable tasks: 2 executed

创建完成后,目录结构如下:

▶ tree .
.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── demo
    │   │       └── App.java
    │   └── resources
    └── test
        ├── java
        │   └── demo
        │       └── AppTest.java
        └── resources

setting.gradle 内容如下:

rootProject.name = 'gradle-demo-0004'

build.gradle 内容如下:

plugins {
    // Apply the java plugin to add support for Java
    id 'java'

    // Apply the application plugin to add support for building an application
    id 'application'
}

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is found on compile classpath of this component and consumers.
    implementation 'com.google.guava:guava:27.0.1-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

// Define the main class for the application
mainClassName = 'demo.App'

创建 Java Library 项目

▶ gradle init --type java-library

Select build script DSL:
  1: groovy
  2: kotlin
Enter selection (default: groovy) [1..2] 1

Select test framework:
  1: junit
  2: testng
  3: spock
Enter selection (default: junit) [1..3] 1

Project name (default: gradle-demo-0004): gradle-demo
Source package (default: gradle.demo): demo

BUILD SUCCESSFUL in 19s
2 actionable tasks: 2 executed
▶ tree .
.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── demo
    │   │       └── Library.java
    │   └── resources
    └── test
        ├── java
        │   └── demo
        │       └── LibraryTest.java
        └── resources

setting.gradle 内容如下:

rootProject.name = 'gradle-demo'

build.gradle 内容如下:

plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
}

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:27.0.1-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

( 本文完 )