設定 Gradle

使用 SpongeGradle

Using SpongeGradle is very simple and allows you to minimize the necessary Gradle configuration for setting up a Sponge plugin on Gradle. Additionally, it provides integration for 插件元數據, such as automatically contributing the group, project name, version and description defined in your build script to the built plugin, so you only need to update your plugin version in one file.

小訣竅

Most problems are caused by attempting to use an outdated Gradle version. We recommend using the latest Gradle version together with SpongeGradle. The Gradle section of the build systems page explains how to setup Gradle on your computer.

Below is a simple template that should be usable for most plugins. Make sure to replace the group with the group ID you have chosen before.

plugins {
    id 'java'
    id 'org.spongepowered.plugin' version '0.9.0'
}

// This may not be required, but has solved issues in the past
compileJava.options.encoding = 'UTF-8'

// TODO: Change the following to match your information
group = 'com.example'
version = '1.0.0-SNAPSHOT'
description = 'Here lies an example plugin definition'

repositories {
    jcenter()
}

dependencies {
    compile 'org.spongepowered:spongeapi:7.1.0'
}

These few lines handle most settings you would normally do manually:

  • 基本的 Gradle Java 設定
  • 設定你的專案使用 Java 8 編譯
  • 增加 Sponge 的 Maven 儲存庫(以及 Maven Central)
  • Set up a plugin with the project name in lower case as plugin ID
  • Automatically includes the project name, description and version in 插件元數據.

手動設定插件 ID

By default, the Gradle plugin will configure your plugin ID with project name (in lowercase) you have configured. If you want to use a custom plugin ID and still use the 插件元數據 integration you can change it manually:

sponge {
    plugin {
        id = 'pluginidgoeshere'
    }
}

覆蓋預設值

By default, the Gradle plugin will contribute the plugin name, plugin version, and description automatically to 插件元數據 with defaults defined in the project properties. It is also possible to override these if you want to specify them manually:

sponge {
    plugin {
        meta {
            name = 'Example Plugin'
            version = '1.0.0-SNAPSHOT'
            description = 'This is an example plugin'
            url = 'http://www.example.com/'
        }
    }
}

You can also remove a default value entirely:

sponge {
    plugin {
        meta {
            name = null
            description = null
        }
    }
}

不使用 SpongeGradle

警告

We recommend using SpongeGradle for Gradle plugins since it will provide additional Gradle integration for Sponge plugins.

Generally, everything necessary to compile a Sponge plugin using Gradle can be done by simply adding the SpongeAPI dependency to your project:

repositories {
    jcenter()
    maven {
        name = 'sponge-repo'
        url = 'https://repo.spongepowered.org/maven'
    }
}

dependencies {
    compile 'org.spongepowered:spongeapi:7.1.0'
}