ubuntu

Java编译Ubuntu如何使用Gradle

小樊
48
2025-11-08 22:16:51
栏目: 编程语言

Prerequisites: Install Java Development Kit (JDK)
Before using Gradle, ensure you have JDK 8 or higher installed. Run java -version and javac -version to verify. If not installed, use Ubuntu’s package manager:

sudo apt update
sudo apt install openjdk-11-jdk

Step 1: Install Gradle on Ubuntu
You can install Gradle via the package manager (easier) or manually (more control).

Option A: Install via Ubuntu Package Manager (Recommended)

  1. Add the Gradle repository and GPG key:
    sudo apt update
    wget -qO - https://packages.gradle.org/gpg | sudo apt-key add -
    sudo sh -c 'echo "deb https://packages.gradle.org/distributions/ gradle/$(curl -s https://packages.gradle.org/distributions/latest/release)" > /etc/apt/sources.list.d/gradle.list'
    sudo apt update
    
  2. Install Gradle:
    sudo apt install gradle
    

Option B: Manual Installation (Specific Version)

  1. Download the latest Gradle binary from the official website.
  2. Extract it to a directory (e.g., ~/gradle):
    unzip gradle-x.y.z-bin.zip -d ~/gradle
    
  3. Add Gradle to your PATH:
    echo "export PATH=$HOME/gradle/gradle-x.y.z/bin:$PATH" >> ~/.bashrc
    source ~/.bashrc
    

Step 2: Verify Gradle Installation
Run gradle -v in the terminal. You should see the installed Gradle version, JVM details, and operating system info.

Step 3: Create or Initialize a Java Project
You can create a project manually or use Gradle’s init task.

Option A: Manual Project Structure

Create a root directory and standard source folders:

mkdir my-java-project
cd my-java-project
mkdir -p src/main/java src/test/java

Option B: Use Gradle Init

Run gradle init in an empty directory and follow the prompts:

mkdir my-java-project
cd my-java-project
gradle init

Select basic project type, then choose Java application when prompted. This generates a build.gradle file and basic directories.

Step 4: Configure build.gradle
Edit the build.gradle file to define project settings, dependencies, and tasks. A minimal Java project requires:

Example build.gradle:

plugins {
    id 'java'          // Provides Java compilation, testing, and packaging tasks
    id 'application'   // Adds tasks to run and package the application
}

group = 'com.example'  // Project group (like a package name)
version = '1.0-SNAPSHOT' // Project version

repositories {
    mavenCentral()     // Uses Maven Central for dependency resolution
}

dependencies {
    // Add project dependencies here (e.g., libraries)
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

application {
    mainClass = 'com.example.Main' // Fully qualified name of the main class
}

test {
    useJUnitPlatform()   // Configures JUnit 5 for testing
}

Step 5: Compile the Java Project
Navigate to the project root directory and run:

gradle build

This command:

  1. Downloads dependencies (if any) from the configured repository.
  2. Compiles Java code in src/main/java.
  3. Runs unit tests in src/test/java.
  4. Packages compiled classes into a JAR file at build/libs/<project-name>-<version>.jar.

Step 6: Run the Compiled Project
To execute the program, use the run task (if you added the application plugin) or manually run the JAR:

# Option 1: Use Gradle's run task (simpler)
gradle run

# Option 2: Run the JAR file directly
java -jar build/libs/my-java-project-1.0-SNAPSHOT.jar

Optional: Use Gradle Wrapper for Reproducibility
The Gradle wrapper (gradlew) ensures your project uses a specific Gradle version, even if it’s not installed on the system. To add it:

gradle wrapper --gradle-version 8.5  # Replace with desired version

This creates gradlew (Linux/macOS) and gradlew.bat (Windows) scripts. Use them instead of the global gradle command:

./gradlew build  # Linux/macOS
./gradlew.bat build  # Windows

0
看了该问题的人还看了