Groovy & Gradle: JVM Automation and Build Engineering · Lección

Estructura de proyectos de Gradle

Explore la estructura estándar de un proyecto de Gradle y el papel de `build.gradle` y `settings.gradle`.

Lección 2 de 412 pasos

Estructura de proyectos de Gradle es una lección gratuita de Groovy & Gradle: JVM Automation and Build Engineering en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Groovy & Gradle: JVM Automation and Build Engineering, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Groovy & Gradle: JVM Automation and Build Engineering incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

Why Structure Matters

A well-organized project is key to successful development! Just like a recipe needs clear steps, a software project needs a clear structure.

Gradle projects follow a standard layout. This makes it easier for developers to understand, maintain, and collaborate on code.

The Gradle Root Project

Every Gradle build has a root project. This is usually the top-level directory where you run Gradle commands.

  • It's the starting point for Gradle's build process.
  • It can be a single project or contain multiple subprojects.

Think of it as the main folder for your entire application.

`settings.gradle`: Build Setup

The settings.gradle file is crucial for telling Gradle about your project structure.

  • It defines the project's name.
  • For multi-project builds, it lists all subprojects.

Even for a single project, it's good practice to have this file.

rootProject.name = 'my-first-gradle-app'

`build.gradle`: The Core Script

The build.gradle file is where you define all the rules for building your project. It contains:

  • Plugins to add specific functionalities (like Java compilation).
  • Project dependencies (libraries your code needs).
  • Custom tasks and configurations.

This is your project's main instruction manual for Gradle!

Key Project Files

When you initialize a Gradle project, you'll see a few important files:

  • settings.gradle: Configures the build structure.
  • build.gradle: Defines build logic (tasks, dependencies).
  • gradlew (and gradlew.bat): The Gradle Wrapper scripts.
  • .gradle/: Directory for Gradle's internal files (cache, daemon logs).

These files work together to make your build happen.

Common Directories

Gradle projects typically follow a convention for directories:

  • src/: Contains all your source code and resources.
  • build/: Where Gradle puts all generated files (compiled code, JARs, reports).
  • lib/ (optional): Sometimes used for unmanaged, local dependencies.

Sticking to this layout helps Gradle find things automatically.

The `src` Directory Explained

The src directory is further organized:

  • src/main/java: Your main application's Java source files.
  • src/main/resources: Non-code assets for your main app (e.g., config files).
  • src/test/java: Your test source files.
  • src/test/resources: Non-code assets for your tests.

This separation helps keep your code clean and manageable.

First Java `build.gradle`

Here's a simple build.gradle file for a Java project. It tells Gradle to apply the Java plugin and define basic project info.

plugins {
    id 'java' // Apply the Java plugin
}

group = 'com.coddykit'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral() // Where to find dependencies
}

// No dependencies for this basic example
dependencies {
}

Hello Gradle Java App

Let's create a simple Java program that fits into our src/main/java structure. Try running it!

// File: src/main/java/com/coddykit/Main.java
package com.coddykit;

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello from Gradle Project!");
  }
}

Gradle's Build Flow

When you run a Gradle command (like gradle build), here's a simplified flow:

  • Gradle finds and executes settings.gradle.
  • It then processes the build.gradle file(s).
  • Plugins are applied, tasks are configured.
  • Finally, the requested tasks are executed (e.g., compile code, run tests).

This process transforms your source code into a usable output.

Quick Check: Core Files

Understanding the role of each file is fundamental to Gradle.

Recap: Gradle Structure

In this lesson, we explored the standard structure of a Gradle project:

  • The root project as the entry point.
  • settings.gradle for project configuration.
  • build.gradle for core build logic.
  • The conventional src/ and build/ directories.

This structure ensures clarity and efficiency in your build automation. Next, we'll dive deeper into tasks!

Gratis para empezar

Aprende Groovy con un tutor de IA — gratis

Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.

Cursos
12
Lecciones
48

Preguntas frecuentes

¿La lección «Estructura de proyectos de Gradle» es gratis?

Sí — el texto completo de «Estructura de proyectos de Gradle» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Groovy & Gradle: JVM Automation and Build Engineering, actualiza a CoddyKit PRO. El curso de Groovy & Gradle: JVM Automation and Build Engineering incluye 4 lecciones en total.

¿Qué aprenderé en «Estructura de proyectos de Gradle»?

Explore la estructura estándar de un proyecto de Gradle y el papel de `build.gradle` y `settings.gradle`. Practicas Groovy & Gradle: JVM Automation and Build Engineering con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Groovy & Gradle: JVM Automation and Build Engineering?

No se requiere experiencia previa. Groovy & Gradle: JVM Automation and Build Engineering en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «Estructura de proyectos de Gradle»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Groovy & Gradle: JVM Automation and Build Engineering?

Sí. Cada lección de Groovy & Gradle: JVM Automation and Build Engineering incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Instalación y CLI de Gradle
  2. Estructura de proyectos de Gradle
  3. Tareas y ciclo de vida de compilación
  4. Compilaciones multiproyecto y archivo de configuración
← Volver a Groovy & Gradle: JVM Automation and Build Engineering