Intellij Idea 简明教程

Intellij Idea - Build Tools

IntelliJ 提供了一种方法来构建和打包 Java 包。它支持 Maven 和 Gradle 等外部构建工具。本章将讨论这些构建工具。

IntelliJ provides a way to build and package Java package. It supports external build tools like Maven and Gradle. This chapter discusses about these build tools.

Creating Maven Project

按照以下步骤创建 Maven 项目 −

Follow these steps to create a Maven project −

  1. Navigate to File → Project.

  2. Select Maven option and click on Next button.

maven project
  1. In the new project window enter tutorialspoint.com as GroupId and HelloWorld as ArtifactId.

  2. In the New window, it will open the pom.xml file.

  3. We need to add properties to this file; the final pom.xml file should look like this −

<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
      http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoing</groupId>
   <artifactId>HelloWorld</artifactId>
   <version>1.0-SNAPSHOT</version>
   <properties>
      <maven.compiler.source>1.7</maven.compiler.source>
      <maven.compiler.target>1.7</maven.compiler.target>
   </properties>
</project>

现在,让我们在 Maven 项目的 src/main/java 目录内创建一个 Java 类。按照以下步骤创建该类 −

Now, let us create a Java class inside the src/main/java directory of the Maven project. Follow these steps to create the class −

  1. Navigate to the src/main/java directory.

  2. Right click on it and select New → Java Class.

按照以下步骤使用 Maven 编译此类 −

Follow these steps to compile this class using Maven −

  1. Navigate to Run → Edit Configuration.

  2. Click on the green plus icon and select the Maven option from the dropdown menu.

  3. Enter the project name as Maven-Package.

  4. Provide package as the command line.

  5. Click on the OK button.

command line
  1. Navigate to Run and select the Maven-Package option.

  2. It will start building package. Upon successful building of the package, you will see the following result −

building package

Create Gradle Project

在本节中,我们将学习如何创建 Gradle 项目 −

In this section, we will learn how to create a Gradle project −

  1. Navigate to File → Project and select Gradle.

  2. Click on the Next button.

  3. In the new project window, enter tutorialspoint as GroupId and HelloWorld as ArtifactId.

  4. Click on the Next button, verify the project details and click on the Finish button.

  5. Follow the on-screen instructions to complete the process.

finish button
  1. Open the buildl.gradle file and write Hello task as shown in the above screenshot.

  2. To compile this task, navigate to the Run → Build-Gradle option.