0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · 课时

多语言项目构建

使用 Gradle 管理包含 Scala、Groovy 甚至 Android 在内的多语言项目。

多语言项目构建 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Groovy & Gradle: JVM Automation and Build Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What are Polyglot Projects?

A polyglot project is one that uses multiple programming languages. This isn't uncommon, especially in large systems or microservices architectures.

For example, you might have a backend service written in Java, a data processing component in Scala, and utility scripts in Groovy, all contributing to a single application.

Gradle excels at managing such diverse projects, allowing you to leverage the strengths of different languages within a unified build system.

Gradle's Language Power

Gradle achieves its polyglot capabilities through its powerful plugin system. Each language typically has a dedicated plugin that understands its compilation, testing, and packaging requirements.

These plugins integrate seamlessly, allowing Gradle to orchestrate builds involving Java, Groovy, Scala, Kotlin, and even native languages like C/C++ (via NDK for Android).

This means you can define dependencies and tasks across different language modules, all within a single Gradle build.

Integrating Groovy Sources

Groovy is a popular choice for scripting and enhancing Java projects. To include Groovy code, you typically apply the groovy plugin and place your source files in src/main/groovy.

Here's a minimal build.gradle for a Groovy project:

plugins {
  id 'groovy'
}

repositories {
  mavenCentral()
}

dependencies {
  implementation 'org.codehaus.groovy:groovy-all:3.0.9'
}

Now, let's see a simple Groovy program.

class Main {
  static void main(String[] args) {
    println "Hello from Groovy!"
  }
}

Scala Integration with Gradle

Scala is another powerful JVM language. Gradle supports Scala projects using the scala plugin. This plugin handles Scala compilation and integrates with Java source compilation.

To set up a Scala project, you'd apply the plugin and define Scala-specific dependencies, typically including the Scala library itself:

plugins {
  id 'scala'
}

repositories {
  mavenCentral()
}

dependencies {
  implementation 'org.scala-lang:scala-library:2.13.6'
  // If mixing with Java, add Java dependencies here too
}

Scala source files usually reside in src/main/scala.

Running Scala Code

With the scala plugin configured, Gradle can compile and run your Scala code. Here's a basic Scala program:

object Main {
  def main(args: Array[String]): Unit = {
    println("Hello from Scala!")
  }
}

Java and Scala Together

One of Gradle's strengths is allowing different JVM languages to coexist within the same project. You can have Java files in src/main/java and Scala files in src/main/scala.

Gradle's scala plugin automatically configures the build to compile both Java and Scala sources, ensuring they can call each other's code. This is crucial for migrating existing Java codebases or building complex polyglot applications.

The Java compiler runs first, then the Scala compiler, which can then see the compiled Java classes.

Android's Polyglot Nature

Android projects are prime examples of polyglot builds. They commonly involve Java, Kotlin, and sometimes even native C/C++ code (via the NDK).

The Android Gradle Plugin (AGP), applied via id 'com.android.application' or id 'com.android.library', is a sophisticated plugin that orchestrates the compilation, resource processing, and packaging for all these languages into an APK or AAR.

This allows developers to choose the best language for different parts of an Android app.

Adding Kotlin to Your Build

Kotlin is fully interoperable with Java and is another excellent choice for polyglot JVM projects. To include Kotlin, you apply the kotlin-jvm plugin and place source files in src/main/kotlin.

Here's a minimal build.gradle:

plugins {
  id 'org.jetbrains.kotlin.jvm' version '1.9.0' // Use a recent version
}

repositories {
  mavenCentral()
}

dependencies {
  implementation 'org.jetbrains.kotlin:kotlin-stdlib'
}

Let's run a simple Kotlin program.

fun main() {
  println("Hello from Kotlin!")
}

Inter-Module Dependencies

In multi-project Gradle builds, you can define dependencies between subprojects written in different languages. For example, a Groovy subproject might depend on a Scala library subproject.

This is handled using standard Gradle project dependencies:

// In settings.gradle
include 'my-scala-lib', 'my-groovy-app'

// In my-groovy-app/build.gradle
dependencies {
  implementation project(':my-scala-lib')
}

Gradle ensures that the dependent project (e.g., Scala) is built and compiled before the consuming project (e.g., Groovy).

Polyglot Plugin Knowledge

Which of these Gradle plugins are specifically used to add language support for a programming language in a polyglot project?

Recap & Next Steps

Congratulations! You've explored how Gradle empowers you to manage polyglot projects, integrating multiple programming languages within a single build.

  • Gradle's plugin system is key to its language-agnostic approach.
  • You learned how to apply plugins for Groovy, Scala, and Kotlin.
  • We discussed how different language sources can coexist and depend on each other.
  • Android projects serve as a powerful example of real-world polyglot builds.

Mastering polyglot builds allows you to choose the best tool for each job, enhancing your project's flexibility and power. Keep experimenting with different language combinations!

常见问题解答

「多语言项目构建」课时是免费的吗?

是的 — 「多语言项目构建」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Groovy & Gradle: JVM Automation and Build Engineering 课程的其余内容,请升级到 CoddyKit PRO。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。

「多语言项目构建」这节课中我会学到什么?

使用 Gradle 管理包含 Scala、Groovy 甚至 Android 在内的多语言项目。 你通过在浏览器中直接运行的动手代码来练习 Groovy & Gradle: JVM Automation and Build Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Groovy & Gradle: JVM Automation and Build Engineering 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Groovy & Gradle: JVM Automation and Build Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「多语言项目构建」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?

能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Gradle 的 Kotlin DSL
  2. 多语言项目构建
  3. IDE 集成与工具
  4. 使用 Kotlin DSL 编写自定义任务
← 返回 Groovy & Gradle: JVM Automation and Build Engineering