活动公告

系统通知
通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,资源失效请在帖子内回复要求补档,会尽快处理!
10-23 09:31

IntelliJ IDEA运行Maven项目实战教程从环境配置到项目部署解决开发过程中可能遇到的各种问题助你成为高效Java开发者

SunJu_FaceMall

3万

主题

2720

科技点

3万

积分

执行版主

碾压王

积分
32881

塔罗立华奏

执行版主 发表于 2025-8-30 00:50:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言

IntelliJ IDEA作为目前最流行的Java集成开发环境(IDE)之一,与Maven项目构建工具的结合使用,已成为Java开发者的标准配置。Maven通过其强大的依赖管理和项目构建能力,极大地简化了Java项目的开发流程。本文将详细介绍从环境配置到项目部署的全过程,帮助开发者解决在实际开发中可能遇到的各种问题,提高开发效率。

环境配置

JDK安装与配置

Java开发工具包(JDK)是Java开发的基础,确保安装正确版本的JDK至关重要。

1. 下载JDK:访问Oracle官网或OpenJDK官网,下载适合你操作系统的JDK版本。推荐使用JDK 8或以上版本,以获得更好的性能和新特性支持。
2. 安装JDK:按照安装向导完成安装过程。Windows系统下,记住安装路径;macOS和Linux系统通常会将JDK安装到标准目录。
3.
  1. 配置环境变量:Windows系统:
  2. “`bash新建系统变量JAVA_HOMEJAVA_HOME=C:\Program Files\Java\jdk-11.0.12# 编辑Path变量,添加以下内容
  3. %JAVA_HOME%\bin- macOS/Linux系统:
  4. ```bash
  5. # 编辑~/.bash_profile或~/.zshrc文件(根据使用的shell)
  6. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.12.jdk/Contents/Home
  7. export PATH=$JAVA_HOME/bin:$PATH
  8. # 使配置生效
  9. source ~/.bash_profile  # 或 source ~/.zshrc
复制代码
4.
  1. Windows系统:
  2. “`bash新建系统变量JAVA_HOMEJAVA_HOME=C:\Program Files\Java\jdk-11.0.12
复制代码
5. 验证安装:java -version
javac -version如果正确显示Java版本,说明JDK安装成功。

下载JDK:访问Oracle官网或OpenJDK官网,下载适合你操作系统的JDK版本。推荐使用JDK 8或以上版本,以获得更好的性能和新特性支持。

安装JDK:按照安装向导完成安装过程。Windows系统下,记住安装路径;macOS和Linux系统通常会将JDK安装到标准目录。

配置环境变量:

  1. Windows系统:
  2. “`bash新建系统变量JAVA_HOMEJAVA_HOME=C:\Program Files\Java\jdk-11.0.12
复制代码

Windows系统:
“`bash

JAVA_HOME=C:\Program Files\Java\jdk-11.0.12

# 编辑Path变量,添加以下内容
%JAVA_HOME%\bin
  1. - macOS/Linux系统:
  2. ```bash
  3. # 编辑~/.bash_profile或~/.zshrc文件(根据使用的shell)
  4. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.12.jdk/Contents/Home
  5. export PATH=$JAVA_HOME/bin:$PATH
  6. # 使配置生效
  7. source ~/.bash_profile  # 或 source ~/.zshrc
复制代码

验证安装:
  1. java -version
  2. javac -version
复制代码

如果正确显示Java版本,说明JDK安装成功。

IntelliJ IDEA安装与配置

1. 下载IntelliJ IDEA:访问JetBrains官网,下载IntelliJ IDEA。有两种版本可供选择:Ultimate版(付费,功能全面)和Community版(免费,适合Java SE开发)。
2. 安装IntelliJ IDEA:按照安装向导完成安装过程。
3. 首次配置:选择主题和编辑器颜色方案配置默认插件导入已有配置(如果有)
4. 选择主题和编辑器颜色方案
5. 配置默认插件
6. 导入已有配置(如果有)
7.
  1. JVM配置(可选):如果需要调整IntelliJ IDEA的内存使用,可以修改idea64.exe.vmoptions(Windows)或idea.vmoptions(macOS/Linux)文件:-Xms1024m
  2. -Xmx4096m
  3. -XX:ReservedCodeCacheSize=512m
复制代码

下载IntelliJ IDEA:访问JetBrains官网,下载IntelliJ IDEA。有两种版本可供选择:Ultimate版(付费,功能全面)和Community版(免费,适合Java SE开发)。

安装IntelliJ IDEA:按照安装向导完成安装过程。

首次配置:

• 选择主题和编辑器颜色方案
• 配置默认插件
• 导入已有配置(如果有)

JVM配置(可选):如果需要调整IntelliJ IDEA的内存使用,可以修改idea64.exe.vmoptions(Windows)或idea.vmoptions(macOS/Linux)文件:
  1. -Xms1024m
  2. -Xmx4096m
  3. -XX:ReservedCodeCacheSize=512m
复制代码

Maven安装与配置

虽然IntelliJ IDEA内置了Maven,但独立安装Maven可以确保版本一致性和更好的控制。

1. 下载Maven:访问Maven官网,下载最新的稳定版本。
2. 安装Maven:解压下载的压缩包到指定目录,如C:\Program Files\Apache\maven(Windows)或/opt/maven(Linux)。
3.
  1. 配置环境变量:Windows系统:
  2. “`bash新建系统变量MAVEN_HOMEMAVEN_HOME=C:\Program Files\Apache\maven# 编辑Path变量,添加以下内容
  3. %MAVEN_HOME%\bin- macOS/Linux系统:
  4. ```bash
  5. # 编辑~/.bash_profile或~/.zshrc文件
  6. export MAVEN_HOME=/opt/maven
  7. export PATH=$MAVEN_HOME/bin:$PATH
  8. # 使配置生效
  9. source ~/.bash_profile  # 或 source ~/.zshrc
复制代码
4.
  1. Windows系统:
  2. “`bash新建系统变量MAVEN_HOMEMAVEN_HOME=C:\Program Files\Apache\maven
复制代码
5. 验证安装:mvn -version如果正确显示Maven版本,说明安装成功。
6. 配置Maven设置文件(settings.xml):
通常位于$MAVEN_HOME/conf/settings.xml,可以配置本地仓库位置、镜像服务器等:
“`xml/path/to/your/local/repo

下载Maven:访问Maven官网,下载最新的稳定版本。

安装Maven:解压下载的压缩包到指定目录,如C:\Program Files\Apache\maven(Windows)或/opt/maven(Linux)。

配置环境变量:

  1. Windows系统:
  2. “`bash新建系统变量MAVEN_HOMEMAVEN_HOME=C:\Program Files\Apache\maven
复制代码

Windows系统:
“`bash

MAVEN_HOME=C:\Program Files\Apache\maven

# 编辑Path变量,添加以下内容
%MAVEN_HOME%\bin
  1. - macOS/Linux系统:
  2. ```bash
  3. # 编辑~/.bash_profile或~/.zshrc文件
  4. export MAVEN_HOME=/opt/maven
  5. export PATH=$MAVEN_HOME/bin:$PATH
  6. # 使配置生效
  7. source ~/.bash_profile  # 或 source ~/.zshrc
复制代码

验证安装:
  1. mvn -version
复制代码

如果正确显示Maven版本,说明安装成功。

配置Maven设置文件(settings.xml):
通常位于$MAVEN_HOME/conf/settings.xml,可以配置本地仓库位置、镜像服务器等:
“`xml/path/to/your/local/repo
  1. <mirror>
  2.    <id>aliyun</id>
  3.    <mirrorOf>central</mirrorOf>
  4.    <name>Aliyun Maven Central</name>
  5.    <url>https://maven.aliyun.com/repository/central</url>
  6. </mirror>
复制代码
  1. ### 在IntelliJ IDEA中配置Maven
  2. 1. 打开IntelliJ IDEA,进入`File > Settings`(Windows/Linux)或`IntelliJ IDEA > Preferences`(macOS)。
  3. 2. 导航到`Build, Execution, Deployment > Build Tools > Maven`。
  4. 3. 配置Maven home path:选择你安装的Maven目录,而不是使用IDE捆绑的版本。
  5. 4. 配置User settings file:指向你的自定义settings.xml文件。
  6. 5. 配置Local repository:通常会自动根据settings.xml文件中的配置填充。
  7. 6. 在`Maven > Importing`设置中,可以配置:
  8.    - Automatically download:Sources和Documentation
  9.    - VM options for importer:可以增加内存设置,如`-Xmx1024m`
  10.    - Use Maven3 to import project:确保勾选
  11. 7. 在`Maven > Running`设置中,可以配置:
  12.    - VM Options:可以增加内存设置,如`-Xmx1024m -XX:MaxPermSize=512m`
  13.    - Environment variables:可以添加必要的环境变量
  14. ## 创建Maven项目
  15. ### 使用IntelliJ IDEA创建Maven项目
  16. 1. 打开IntelliJ IDEA,选择`File > New > Project`。
  17. 2. 在左侧选择`Maven`。
  18. 3. 配置项目SDK:选择已安装的JDK版本。
  19. 4. 选择Create from archetype(如果需要使用特定模板):
  20.    - 常用archetype包括:
  21.      - `maven-archetype-quickstart`:简单的Java项目
  22.      - `maven-archetype-webapp`:Java Web项目
  23.      - `org.apache.maven.archetypes:maven-archetype-quickstart`:标准的Java项目
  24. 5. 点击Next,填写项目信息:
  25.    - GroupId:通常是公司或组织域名的倒序,如`com.example`
  26.    - ArtifactId:项目名称,如`my-app`
  27.    - Version:项目版本,默认为`1.0-SNAPSHOT`
  28. 6. 点击Next,确认Maven设置和项目名称、位置。
  29. 7. 点击Finish,IntelliJ IDEA将创建项目结构并下载必要的依赖。
  30. ### Maven项目结构解析
  31. 标准的Maven项目结构如下:
复制代码

my-app/
├── src/
│   ├── main/
│   │   ├── java/          # Java源代码
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── App.java
│   │   ├── resources/     # 资源文件
│   │   │   └── application.properties
│   │   └── webapp/        # Web应用资源(如果是Web项目)
│   │       ├── WEB-INF/
│   │       │   └── web.xml
│   │       └── index.jsp
│   └── test/
│       ├── java/          # 测试源代码
│       │   └── com/
│       │       └── example/
│       │           └── AppTest.java
│       └── resources/     # 测试资源文件
├── target/                # 构建输出目录
├── pom.xml                # Maven项目配置文件
└── my-app.iml             # IntelliJ IDEA模块文件
  1. ### pom.xml文件详解
  2. pom.xml是Maven项目的核心配置文件,定义了项目的基本信息、依赖、构建配置等。
  3. 一个基本的pom.xml文件结构如下:
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <project xmlns="http://maven.apache.org/POM/4.0.0"
  7.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  9.     <modelVersion>4.0.0</modelVersion>
  10.     <!-- 项目基本信息 -->
  11.     <groupId>com.example</groupId>
  12.     <artifactId>my-app</artifactId>
  13.     <version>1.0-SNAPSHOT</version>
  14.     <packaging>jar</packaging> <!-- 打包类型,可以是jar, war, pom等 -->
  15.     <name>My Application</name>
  16.     <description>A sample Maven project</description>
  17.     <!-- 属性定义 -->
  18.     <properties>
  19.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  20.         <maven.compiler.source>11</maven.compiler.source>
  21.         <maven.compiler.target>11</maven.compiler.target>
  22.         <junit.version>5.8.2</junit.version>
  23.     </properties>
  24.     <!-- 依赖管理 -->
  25.     <dependencies>
  26.         <!-- JUnit 5 依赖 -->
  27.         <dependency>
  28.             <groupId>org.junit.jupiter</groupId>
  29.             <artifactId>junit-jupiter-api</artifactId>
  30.             <version>${junit.version}</version>
  31.             <scope>test</scope>
  32.         </dependency>
  33.         <dependency>
  34.             <groupId>org.junit.jupiter</groupId>
  35.             <artifactId>junit-jupiter-engine</artifactId>
  36.             <version>${junit.version}</version>
  37.             <scope>test</scope>
  38.         </dependency>
  39.         
  40.         <!-- 其他依赖示例 -->
  41.         <dependency>
  42.             <groupId>org.apache.commons</groupId>
  43.             <artifactId>commons-lang3</artifactId>
  44.             <version>3.12.0</version>
  45.         </dependency>
  46.     </dependencies>
  47.     <!-- 构建配置 -->
  48.     <build>
  49.         <plugins>
  50.             <!-- Maven编译插件 -->
  51.             <plugin>
  52.                 <groupId>org.apache.maven.plugins</groupId>
  53.                 <artifactId>maven-compiler-plugin</artifactId>
  54.                 <version>3.8.1</version>
  55.                 <configuration>
  56.                     <source>11</source>
  57.                     <target>11</target>
  58.                 </configuration>
  59.             </plugin>
  60.             
  61.             <!-- Maven Surefire插件,用于运行单元测试 -->
  62.             <plugin>
  63.                 <groupId>org.apache.maven.plugins</groupId>
  64.                 <artifactId>maven-surefire-plugin</artifactId>
  65.                 <version>3.0.0-M5</version>
  66.             </plugin>
  67.         </plugins>
  68.     </build>
  69. </project>
复制代码

依赖管理

添加依赖

在Maven项目中,添加依赖非常简单,只需在pom.xml文件的<dependencies>部分添加相应的依赖声明即可。

例如,添加Spring Boot Starter Web依赖:
  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-web</artifactId>
  4.     <version>2.6.3</version>
  5. </dependency>
复制代码

在IntelliJ IDEA中,更简单的方法是:

1. 打开pom.xml文件
2. 在代码编辑器中右键,选择Generate > Dependency
3. 在弹出的对话框中搜索所需的依赖
4. 选择依赖版本并点击添加

IntelliJ IDEA还提供了Maven工具窗口,可以通过该窗口快速添加依赖:

1. 打开右侧的Maven工具窗口
2. 展开项目,选择Dependencies
3. 右键点击,选择Add Dependency
4. 在弹出的对话框中搜索并添加依赖

依赖范围

Maven提供了多种依赖范围(scope),用于控制依赖在不同阶段(编译、测试、运行)的可见性。

常见的依赖范围包括:

1. compile(默认范围):在所有阶段都可用,会传递到依赖项目中。<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>5.3.15</version>
   <scope>compile</scope> <!-- 可以省略,因为这是默认值 -->
</dependency>
2. provided:在编译和测试阶段可用,但在运行时由容器提供(如Servlet API)。<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>4.0.1</version>
   <scope>provided</scope>
</dependency>
3. runtime:在运行和测试阶段可用,但在编译时不需要(如JDBC驱动)。<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.28</version>
   <scope>runtime</scope>
</dependency>
4. test:仅在测试阶段可用,不会传递到依赖项目中。<dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter-api</artifactId>
   <version>5.8.2</version>
   <scope>test</scope>
</dependency>
5.
  1. system:类似于provided,但需要显式提供JAR文件。不推荐使用。<dependency>
  2.    <groupId>com.example</groupId>
  3.    <artifactId>custom-library</artifactId>
  4.    <version>1.0</version>
  5.    <scope>system</scope>
  6.    <systemPath>${project.basedir}/lib/custom-library-1.0.jar</systemPath>
  7. </dependency>
复制代码
6. import:仅用于<dependencyManagement>部分,用于导入依赖管理信息。<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-dependencies</artifactId>
   <version>2.6.3</version>
   <type>pom</type>
   <scope>import</scope>
</dependency>

compile(默认范围):在所有阶段都可用,会传递到依赖项目中。
  1. <dependency>
  2.    <groupId>org.springframework</groupId>
  3.    <artifactId>spring-core</artifactId>
  4.    <version>5.3.15</version>
  5.    <scope>compile</scope> <!-- 可以省略,因为这是默认值 -->
  6. </dependency>
复制代码

provided:在编译和测试阶段可用,但在运行时由容器提供(如Servlet API)。
  1. <dependency>
  2.    <groupId>javax.servlet</groupId>
  3.    <artifactId>javax.servlet-api</artifactId>
  4.    <version>4.0.1</version>
  5.    <scope>provided</scope>
  6. </dependency>
复制代码

runtime:在运行和测试阶段可用,但在编译时不需要(如JDBC驱动)。
  1. <dependency>
  2.    <groupId>mysql</groupId>
  3.    <artifactId>mysql-connector-java</artifactId>
  4.    <version>8.0.28</version>
  5.    <scope>runtime</scope>
  6. </dependency>
复制代码

test:仅在测试阶段可用,不会传递到依赖项目中。
  1. <dependency>
  2.    <groupId>org.junit.jupiter</groupId>
  3.    <artifactId>junit-jupiter-api</artifactId>
  4.    <version>5.8.2</version>
  5.    <scope>test</scope>
  6. </dependency>
复制代码

system:类似于provided,但需要显式提供JAR文件。不推荐使用。
  1. <dependency>
  2.    <groupId>com.example</groupId>
  3.    <artifactId>custom-library</artifactId>
  4.    <version>1.0</version>
  5.    <scope>system</scope>
  6.    <systemPath>${project.basedir}/lib/custom-library-1.0.jar</systemPath>
  7. </dependency>
复制代码

import:仅用于<dependencyManagement>部分,用于导入依赖管理信息。
  1. <dependency>
  2.    <groupId>org.springframework.boot</groupId>
  3.    <artifactId>spring-boot-dependencies</artifactId>
  4.    <version>2.6.3</version>
  5.    <type>pom</type>
  6.    <scope>import</scope>
  7. </dependency>
复制代码

依赖冲突解决

当项目中的多个依赖依赖于同一个库的不同版本时,就会发生依赖冲突。Maven使用”最近定义策略”来解决依赖冲突,即使用依赖树中离项目最近的版本。

解决依赖冲突的几种方法:

1. 使用mvn dependency:tree命令查看依赖树:mvn dependency:tree在IntelliJ IDEA中,可以打开Maven工具窗口,点击Show Dependencies按钮查看依赖图。
2. 使用<exclusions>排除不需要的依赖:<dependency>
   <groupId>com.example</groupId>
   <artifactId>example-library</artifactId>
   <version>1.0.0</version>
   <exclusions>
       <exclusion>
           <groupId>commons-logging</groupId>
           <artifactId>commons-logging</artifactId>
       </exclusion>
   </exclusions>
</dependency>
3. 在<dependencyManagement>中明确指定依赖版本:<dependencyManagement>
   <dependencies>
       <dependency>
           <groupId>commons-logging</groupId>
           <artifactId>commons-logging</artifactId>
           <version>1.2</version>
       </dependency>
   </dependencies>
</dependencyManagement>
4. 使用Maven Helper插件(IntelliJ IDEA插件):安装Maven Helper插件打开pom.xml文件,切换到Dependency Analyzer选项卡查看冲突并快速排除
5. 安装Maven Helper插件
6. 打开pom.xml文件,切换到Dependency Analyzer选项卡
7. 查看冲突并快速排除

使用mvn dependency:tree命令查看依赖树:
  1. mvn dependency:tree
复制代码

在IntelliJ IDEA中,可以打开Maven工具窗口,点击Show Dependencies按钮查看依赖图。

使用<exclusions>排除不需要的依赖:
  1. <dependency>
  2.    <groupId>com.example</groupId>
  3.    <artifactId>example-library</artifactId>
  4.    <version>1.0.0</version>
  5.    <exclusions>
  6.        <exclusion>
  7.            <groupId>commons-logging</groupId>
  8.            <artifactId>commons-logging</artifactId>
  9.        </exclusion>
  10.    </exclusions>
  11. </dependency>
复制代码

在<dependencyManagement>中明确指定依赖版本:
  1. <dependencyManagement>
  2.    <dependencies>
  3.        <dependency>
  4.            <groupId>commons-logging</groupId>
  5.            <artifactId>commons-logging</artifactId>
  6.            <version>1.2</version>
  7.        </dependency>
  8.    </dependencies>
  9. </dependencyManagement>
复制代码

使用Maven Helper插件(IntelliJ IDEA插件):

• 安装Maven Helper插件
• 打开pom.xml文件,切换到Dependency Analyzer选项卡
• 查看冲突并快速排除

构建和运行Maven项目

使用Maven命令构建项目

Maven提供了一系列命令用于构建项目,常用的命令包括:

1. mvn clean:清理项目,删除target目录
2. mvn compile:编译项目源代码
3. mvn test:运行单元测试
4. mvn package:打包项目,生成JAR或WAR文件
5. mvn install:将打包好的文件安装到本地仓库
6. mvn deploy:将项目部署到远程仓库

这些命令可以组合使用,例如:
  1. mvn clean package  # 清理并打包项目
  2. mvn clean install  # 清理、编译、测试、打包并安装到本地仓库
复制代码

在IntelliJ IDEA中,可以通过以下方式执行Maven命令:

1. 打开Maven工具窗口
2. 展开项目生命周期
3. 双击要执行的生命周期阶段

或者,使用IntelliJ IDEA的终端执行Maven命令。

在IntelliJ IDEA中运行Maven项目

在IntelliJ IDEA中运行Maven项目有多种方式,具体取决于项目类型。

1. 创建运行配置:点击右上角的运行配置下拉菜单选择Edit Configurations...点击+号,选择Application配置Main class(主类)设置程序参数和VM选项(如果需要)点击OK保存配置
2. 点击右上角的运行配置下拉菜单
3. 选择Edit Configurations...
4. 点击+号,选择Application
5. 配置Main class(主类)
6. 设置程序参数和VM选项(如果需要)
7. 点击OK保存配置
8. 运行应用程序:点击工具栏上的运行按钮(绿色三角形)或使用快捷键Shift+F10(运行)/ Shift+F9(调试)
9. 点击工具栏上的运行按钮(绿色三角形)
10. 或使用快捷键Shift+F10(运行)/ Shift+F9(调试)

创建运行配置:

• 点击右上角的运行配置下拉菜单
• 选择Edit Configurations...
• 点击+号,选择Application
• 配置Main class(主类)
• 设置程序参数和VM选项(如果需要)
• 点击OK保存配置

运行应用程序:

• 点击工具栏上的运行按钮(绿色三角形)
• 或使用快捷键Shift+F10(运行)/ Shift+F9(调试)

1. 配置应用服务器:打开File > Settings > Build, Execution, Deployment > Application Servers点击+号,选择服务器类型(如Tomcat)配置服务器名称和路径点击OK
2. 打开File > Settings > Build, Execution, Deployment > Application Servers
3. 点击+号,选择服务器类型(如Tomcat)
4. 配置服务器名称和路径
5. 点击OK
6. 创建运行配置:点击右上角的运行配置下拉菜单选择Edit Configurations...点击+号,选择Tomcat Server > Local配置服务器名称和端口切换到Deployment选项卡点击+号,选择Artifact,选择要部署的WAR或EAR文件点击OK保存配置
7. 点击右上角的运行配置下拉菜单
8. 选择Edit Configurations...
9. 点击+号,选择Tomcat Server > Local
10. 配置服务器名称和端口
11. 切换到Deployment选项卡
12. 点击+号,选择Artifact,选择要部署的WAR或EAR文件
13. 点击OK保存配置
14. 运行Web应用程序:点击工具栏上的运行按钮或使用快捷键Shift+F10(运行)/ Shift+F9(调试)
15. 点击工具栏上的运行按钮
16. 或使用快捷键Shift+F10(运行)/ Shift+F9(调试)

配置应用服务器:

• 打开File > Settings > Build, Execution, Deployment > Application Servers
• 点击+号,选择服务器类型(如Tomcat)
• 配置服务器名称和路径
• 点击OK

创建运行配置:

• 点击右上角的运行配置下拉菜单
• 选择Edit Configurations...
• 点击+号,选择Tomcat Server > Local
• 配置服务器名称和端口
• 切换到Deployment选项卡
• 点击+号,选择Artifact,选择要部署的WAR或EAR文件
• 点击OK保存配置

运行Web应用程序:

• 点击工具栏上的运行按钮
• 或使用快捷键Shift+F10(运行)/ Shift+F9(调试)

1. 创建运行配置:点击右上角的运行配置下拉菜单选择Edit Configurations...点击+号,选择Spring Boot选择主类设置程序参数和VM选项(如果需要)点击OK保存配置
2. 点击右上角的运行配置下拉菜单
3. 选择Edit Configurations...
4. 点击+号,选择Spring Boot
5. 选择主类
6. 设置程序参数和VM选项(如果需要)
7. 点击OK保存配置
8. 运行Spring Boot应用程序:点击工具栏上的运行按钮或使用快捷键Shift+F10(运行)/ Shift+F9(调试)
9. 点击工具栏上的运行按钮
10. 或使用快捷键Shift+F10(运行)/ Shift+F9(调试)

创建运行配置:

• 点击右上角的运行配置下拉菜单
• 选择Edit Configurations...
• 点击+号,选择Spring Boot
• 选择主类
• 设置程序参数和VM选项(如果需要)
• 点击OK保存配置

运行Spring Boot应用程序:

• 点击工具栏上的运行按钮
• 或使用快捷键Shift+F10(运行)/ Shift+F9(调试)

调试Maven项目

IntelliJ IDEA提供了强大的调试功能,可以帮助开发者快速定位和解决问题。

1. 在代码行号旁边单击,设置断点(红色圆点)
2. 可以设置条件断点:右键点击断点,设置条件
3. 可以设置日志断点:右键点击断点,取消”Suspend”选项,勾选”Evaluate and log”

1. 点击工具栏上的调试按钮(带虫子的图标)
2. 或使用快捷键Shift+F9

调试工具窗口提供了以下功能:

1. 变量查看:查看当前作用域的变量值
2. 表达式求值:计算表达式的值
3. 帧管理:查看调用栈
4. 线程管理:查看和切换线程
5. 控制执行:F8:Step Over(单步跳过)F7:Step Into(单步进入)Shift+F8:Step Out(单步退出)F9:Resume Program(恢复程序)
6. F8:Step Over(单步跳过)
7. F7:Step Into(单步进入)
8. Shift+F8:Step Out(单步退出)
9. F9:Resume Program(恢复程序)

• F8:Step Over(单步跳过)
• F7:Step Into(单步进入)
• Shift+F8:Step Out(单步退出)
• F9:Resume Program(恢复程序)

对于部署在远程服务器上的应用程序,可以使用远程调试:

1. 启动远程应用程序时添加调试参数:java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar my-app.jar
2. 在IntelliJ IDEA中创建远程调试配置:点击右上角的运行配置下拉菜单选择Edit Configurations...点击+号,选择Remote配置名称和端口(与远程应用程序的端口一致)点击OK保存配置
3. 点击右上角的运行配置下拉菜单
4. 选择Edit Configurations...
5. 点击+号,选择Remote
6. 配置名称和端口(与远程应用程序的端口一致)
7. 点击OK保存配置
8. 启动远程调试会话:选择远程调试配置点击调试按钮
9. 选择远程调试配置
10. 点击调试按钮

启动远程应用程序时添加调试参数:
  1. java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar my-app.jar
复制代码

在IntelliJ IDEA中创建远程调试配置:

• 点击右上角的运行配置下拉菜单
• 选择Edit Configurations...
• 点击+号,选择Remote
• 配置名称和端口(与远程应用程序的端口一致)
• 点击OK保存配置

启动远程调试会话:

• 选择远程调试配置
• 点击调试按钮

常见问题及解决方案

依赖下载问题

在构建Maven项目时,依赖下载失败或速度缓慢。

1.
  1. 检查网络连接和Maven镜像配置:<mirrors>
  2. <mirror>
  3.    <id>aliyun</id>
  4.    <mirrorOf>central</mirrorOf>
  5.    <name>Aliyun Maven Central</name>
  6.    <url>https://maven.aliyun.com/repository/central</url>
  7. </mirror>
  8. </mirrors>
复制代码
2. 清理Maven本地仓库并重新下载:mvn clean install -U
3. 手动下载并安装缺失的依赖:mvn install:install-file -Dfile=path/to/your.jar -DgroupId=com.example -DartifactId=example-library -Dversion=1.0 -Dpackaging=jar
4. 使用离线模式(如果依赖已存在于本地仓库):mvn -o clean install
5. 在IntelliJ IDEA中,可以尝试以下操作:打开Maven工具窗口点击”刷新”按钮或右键点击项目,选择”Maven > Reimport”
6. 打开Maven工具窗口
7. 点击”刷新”按钮
8. 或右键点击项目,选择”Maven > Reimport”

检查网络连接和Maven镜像配置:
  1. <mirrors>
  2. <mirror>
  3.    <id>aliyun</id>
  4.    <mirrorOf>central</mirrorOf>
  5.    <name>Aliyun Maven Central</name>
  6.    <url>https://maven.aliyun.com/repository/central</url>
  7. </mirror>
  8. </mirrors>
复制代码

清理Maven本地仓库并重新下载:
  1. mvn clean install -U
复制代码

手动下载并安装缺失的依赖:
  1. mvn install:install-file -Dfile=path/to/your.jar -DgroupId=com.example -DartifactId=example-library -Dversion=1.0 -Dpackaging=jar
复制代码

使用离线模式(如果依赖已存在于本地仓库):
  1. mvn -o clean install
复制代码

在IntelliJ IDEA中,可以尝试以下操作:

• 打开Maven工具窗口
• 点击”刷新”按钮
• 或右键点击项目,选择”Maven > Reimport”

编译问题

项目编译失败,出现各种编译错误。

1. 检查JDK版本兼容性:<properties>
   <maven.compiler.source>11</maven.compiler.source>
   <maven.compiler.target>11</maven.compiler.target>
</properties>
2. 检查Maven编译插件配置:<build>
   <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>3.8.1</version>
           <configuration>
               <source>11</source>
               <target>11</target>
               <encoding>UTF-8</encoding>
           </configuration>
       </plugin>
   </plugins>
</build>
3. 增加编译内存:export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=512m"
4. 在IntelliJ IDEA中:检查项目SDK设置检查模块的Language Level设置尝试使用File > Invalidate Caches / Restart清除缓存并重启IDE
5. 检查项目SDK设置
6. 检查模块的Language Level设置
7. 尝试使用File > Invalidate Caches / Restart清除缓存并重启IDE
8. 处理注解处理器问题:<build>
   <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>3.8.1</version>
           <configuration>
               <annotationProcessorPaths>
                   <path>
                       <groupId>org.mapstruct</groupId>
                       <artifactId>mapstruct-processor</artifactId>
                       <version>1.4.2.Final</version>
                   </path>
               </annotationProcessorPaths>
           </configuration>
       </plugin>
   </plugins>
</build>

检查JDK版本兼容性:
  1. <properties>
  2.    <maven.compiler.source>11</maven.compiler.source>
  3.    <maven.compiler.target>11</maven.compiler.target>
  4. </properties>
复制代码

检查Maven编译插件配置:
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.apache.maven.plugins</groupId>
  5.            <artifactId>maven-compiler-plugin</artifactId>
  6.            <version>3.8.1</version>
  7.            <configuration>
  8.                <source>11</source>
  9.                <target>11</target>
  10.                <encoding>UTF-8</encoding>
  11.            </configuration>
  12.        </plugin>
  13.    </plugins>
  14. </build>
复制代码

增加编译内存:
  1. export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=512m"
复制代码

在IntelliJ IDEA中:

• 检查项目SDK设置
• 检查模块的Language Level设置
• 尝试使用File > Invalidate Caches / Restart清除缓存并重启IDE

处理注解处理器问题:
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.apache.maven.plugins</groupId>
  5.            <artifactId>maven-compiler-plugin</artifactId>
  6.            <version>3.8.1</version>
  7.            <configuration>
  8.                <annotationProcessorPaths>
  9.                    <path>
  10.                        <groupId>org.mapstruct</groupId>
  11.                        <artifactId>mapstruct-processor</artifactId>
  12.                        <version>1.4.2.Final</version>
  13.                    </path>
  14.                </annotationProcessorPaths>
  15.            </configuration>
  16.        </plugin>
  17.    </plugins>
  18. </build>
复制代码

运行问题

项目编译成功,但运行时出现错误。

1. 检查主类配置:<build>
   <plugins>
       <plugin>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-maven-plugin</artifactId>
           <version>2.6.3</version>
           <configuration>
               <mainClass>com.example.Application</mainClass>
           </configuration>
           <executions>
               <execution>
                   <goals>
                       <goal>repackage</goal>
                   </goals>
               </execution>
           </executions>
       </plugin>
   </plugins>
</build>
2. 检查运行时依赖:mvn dependency:tree
3. 增加JVM内存:export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=512m"
4. 在IntelliJ IDEA中:检查运行配置增加VM选项:-Xmx1024m -XX:MaxPermSize=512m检查环境变量和工作目录设置
5. 检查运行配置
6. 增加VM选项:-Xmx1024m -XX:MaxPermSize=512m
7. 检查环境变量和工作目录设置
8. 处理类加载问题:<build>
   <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-plugin</artifactId>
           <version>3.0.0-M5</version>
           <configuration>
               <useSystemClassLoader>false</useSystemClassLoader>
           </configuration>
       </plugin>
   </plugins>
</build>

检查主类配置:
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.springframework.boot</groupId>
  5.            <artifactId>spring-boot-maven-plugin</artifactId>
  6.            <version>2.6.3</version>
  7.            <configuration>
  8.                <mainClass>com.example.Application</mainClass>
  9.            </configuration>
  10.            <executions>
  11.                <execution>
  12.                    <goals>
  13.                        <goal>repackage</goal>
  14.                    </goals>
  15.                </execution>
  16.            </executions>
  17.        </plugin>
  18.    </plugins>
  19. </build>
复制代码

检查运行时依赖:
  1. mvn dependency:tree
复制代码

增加JVM内存:
  1. export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=512m"
复制代码

在IntelliJ IDEA中:

• 检查运行配置
• 增加VM选项:-Xmx1024m -XX:MaxPermSize=512m
• 检查环境变量和工作目录设置

处理类加载问题:
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.apache.maven.plugins</groupId>
  5.            <artifactId>maven-surefire-plugin</artifactId>
  6.            <version>3.0.0-M5</version>
  7.            <configuration>
  8.                <useSystemClassLoader>false</useSystemClassLoader>
  9.            </configuration>
  10.        </plugin>
  11.    </plugins>
  12. </build>
复制代码

插件问题

Maven插件执行失败或版本不兼容。

1. 更新插件版本:<build>
   <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>3.8.1</version>
       </plugin>
   </plugins>
</build>
2. 在插件管理中统一插件版本:<build>
   <pluginManagement>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>3.8.1</version>
           </plugin>
       </plugins>
   </pluginManagement>
</build>
3. 跳过插件执行(临时解决方案):mvn clean install -DskipTests
mvn clean install -Dmaven.test.skip=true
4. 在IntelliJ IDEA中:检查Maven设置尝试使用File > Invalidate Caches / Restart清除缓存并重启IDE检查项目Maven配置
5. 检查Maven设置
6. 尝试使用File > Invalidate Caches / Restart清除缓存并重启IDE
7. 检查项目Maven配置

更新插件版本:
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.apache.maven.plugins</groupId>
  5.            <artifactId>maven-compiler-plugin</artifactId>
  6.            <version>3.8.1</version>
  7.        </plugin>
  8.    </plugins>
  9. </build>
复制代码

在插件管理中统一插件版本:
  1. <build>
  2.    <pluginManagement>
  3.        <plugins>
  4.            <plugin>
  5.                <groupId>org.apache.maven.plugins</groupId>
  6.                <artifactId>maven-compiler-plugin</artifactId>
  7.                <version>3.8.1</version>
  8.            </plugin>
  9.        </plugins>
  10.    </pluginManagement>
  11. </build>
复制代码

跳过插件执行(临时解决方案):
  1. mvn clean install -DskipTests
  2. mvn clean install -Dmaven.test.skip=true
复制代码

在IntelliJ IDEA中:

• 检查Maven设置
• 尝试使用File > Invalidate Caches / Restart清除缓存并重启IDE
• 检查项目Maven配置

项目部署

打包应用

Maven提供了多种打包方式,具体取决于项目类型。

对于Java应用程序,通常打包为JAR文件:

1. 配置打包类型:<packaging>jar</packaging>
2. 使用Maven JAR插件:<build>
   <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-jar-plugin</artifactId>
           <version>3.2.0</version>
           <configuration>
               <archive>
                   <manifest>
                       <addClasspath>true</addClasspath>
                       <mainClass>com.example.Application</mainClass>
                   </manifest>
               </archive>
           </configuration>
       </plugin>
   </plugins>
</build>
3. 创建可执行JAR(包含所有依赖):<build>
   <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-assembly-plugin</artifactId>
           <version>3.3.0</version>
           <configuration>
               <descriptorRefs>
                   <descriptorRef>jar-with-dependencies</descriptorRef>
               </descriptorRefs>
               <archive>
                   <manifest>
                       <mainClass>com.example.Application</mainClass>
                   </manifest>
               </archive>
           </configuration>
           <executions>
               <execution>
                   <phase>package</phase>
                   <goals>
                       <goal>single</goal>
                   </goals>
               </execution>
           </executions>
       </plugin>
   </plugins>
</build>
4. 使用Spring Boot Maven插件(对于Spring Boot应用):<build>
   <plugins>
       <plugin>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-maven-plugin</artifactId>
           <version>2.6.3</version>
           <executions>
               <execution>
                   <goals>
                       <goal>repackage</goal>
                   </goals>
               </execution>
           </executions>
       </plugin>
   </plugins>
</build>
5. 执行打包命令:mvn clean package

配置打包类型:
  1. <packaging>jar</packaging>
复制代码

使用Maven JAR插件:
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.apache.maven.plugins</groupId>
  5.            <artifactId>maven-jar-plugin</artifactId>
  6.            <version>3.2.0</version>
  7.            <configuration>
  8.                <archive>
  9.                    <manifest>
  10.                        <addClasspath>true</addClasspath>
  11.                        <mainClass>com.example.Application</mainClass>
  12.                    </manifest>
  13.                </archive>
  14.            </configuration>
  15.        </plugin>
  16.    </plugins>
  17. </build>
复制代码

创建可执行JAR(包含所有依赖):
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.apache.maven.plugins</groupId>
  5.            <artifactId>maven-assembly-plugin</artifactId>
  6.            <version>3.3.0</version>
  7.            <configuration>
  8.                <descriptorRefs>
  9.                    <descriptorRef>jar-with-dependencies</descriptorRef>
  10.                </descriptorRefs>
  11.                <archive>
  12.                    <manifest>
  13.                        <mainClass>com.example.Application</mainClass>
  14.                    </manifest>
  15.                </archive>
  16.            </configuration>
  17.            <executions>
  18.                <execution>
  19.                    <phase>package</phase>
  20.                    <goals>
  21.                        <goal>single</goal>
  22.                    </goals>
  23.                </execution>
  24.            </executions>
  25.        </plugin>
  26.    </plugins>
  27. </build>
复制代码

使用Spring Boot Maven插件(对于Spring Boot应用):
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.springframework.boot</groupId>
  5.            <artifactId>spring-boot-maven-plugin</artifactId>
  6.            <version>2.6.3</version>
  7.            <executions>
  8.                <execution>
  9.                    <goals>
  10.                        <goal>repackage</goal>
  11.                    </goals>
  12.                </execution>
  13.            </executions>
  14.        </plugin>
  15.    </plugins>
  16. </build>
复制代码

执行打包命令:
  1. mvn clean package
复制代码

对于Web应用程序,通常打包为WAR文件:

1. 配置打包类型:<packaging>war</packaging>
2. 配置WAR插件:<build>
   <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-war-plugin</artifactId>
           <version>3.3.2</version>
           <configuration>
               <warSourceDirectory>src/main/webapp</warSourceDirectory>
               <failOnMissingWebXml>false</failOnMissingWebXml>
           </configuration>
       </plugin>
   </plugins>
</build>
3. 对于Spring Boot应用,还需要:<dependencies>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-tomcat</artifactId>
       <scope>provided</scope>
   </dependency>
</dependencies>
4. 执行打包命令:mvn clean package

配置打包类型:
  1. <packaging>war</packaging>
复制代码

配置WAR插件:
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.apache.maven.plugins</groupId>
  5.            <artifactId>maven-war-plugin</artifactId>
  6.            <version>3.3.2</version>
  7.            <configuration>
  8.                <warSourceDirectory>src/main/webapp</warSourceDirectory>
  9.                <failOnMissingWebXml>false</failOnMissingWebXml>
  10.            </configuration>
  11.        </plugin>
  12.    </plugins>
  13. </build>
复制代码

对于Spring Boot应用,还需要:
  1. <dependencies>
  2.    <dependency>
  3.        <groupId>org.springframework.boot</groupId>
  4.        <artifactId>spring-boot-starter-tomcat</artifactId>
  5.        <scope>provided</scope>
  6.    </dependency>
  7. </dependencies>
复制代码

执行打包命令:
  1. mvn clean package
复制代码

部署到Tomcat

1. 配置Tomcat服务器:打开File > Settings > Build, Execution, Deployment > Application Servers点击+号,选择Tomcat Server配置Tomcat名称和路径点击OK
2. 打开File > Settings > Build, Execution, Deployment > Application Servers
3. 点击+号,选择Tomcat Server
4. 配置Tomcat名称和路径
5. 点击OK
6. 创建运行配置:点击右上角的运行配置下拉菜单选择Edit Configurations...点击+号,选择Tomcat Server > Local配置服务器名称和端口切换到Deployment选项卡点击+号,选择Artifact,选择要部署的WAR文件点击OK保存配置
7. 点击右上角的运行配置下拉菜单
8. 选择Edit Configurations...
9. 点击+号,选择Tomcat Server > Local
10. 配置服务器名称和端口
11. 切换到Deployment选项卡
12. 点击+号,选择Artifact,选择要部署的WAR文件
13. 点击OK保存配置
14. 部署并运行:点击工具栏上的运行按钮Tomcat将启动并部署应用程序
15. 点击工具栏上的运行按钮
16. Tomcat将启动并部署应用程序

配置Tomcat服务器:

• 打开File > Settings > Build, Execution, Deployment > Application Servers
• 点击+号,选择Tomcat Server
• 配置Tomcat名称和路径
• 点击OK

创建运行配置:

• 点击右上角的运行配置下拉菜单
• 选择Edit Configurations...
• 点击+号,选择Tomcat Server > Local
• 配置服务器名称和端口
• 切换到Deployment选项卡
• 点击+号,选择Artifact,选择要部署的WAR文件
• 点击OK保存配置

部署并运行:

• 点击工具栏上的运行按钮
• Tomcat将启动并部署应用程序

1. 将WAR文件复制到Tomcat的webapps目录:cp target/my-app.war /path/to/tomcat/webapps/
2. 启动Tomcat:/path/to/tomcat/bin/startup.sh  # Linux/macOS
/path/to/tomcat/bin/startup.bat # Windows
3. 访问应用程序:http://localhost:8080/my-app/

将WAR文件复制到Tomcat的webapps目录:
  1. cp target/my-app.war /path/to/tomcat/webapps/
复制代码

启动Tomcat:
  1. /path/to/tomcat/bin/startup.sh  # Linux/macOS
  2. /path/to/tomcat/bin/startup.bat # Windows
复制代码

访问应用程序:
  1. http://localhost:8080/my-app/
复制代码

1.
  1. 配置Tomcat插件:<build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.apache.tomcat.maven</groupId>
  5.            <artifactId>tomcat7-maven-plugin</artifactId>
  6.            <version>2.2</version>
  7.            <configuration>
  8.                <url>http://localhost:8080/manager/text</url>
  9.                <server>TomcatServer</server>
  10.                <path>/my-app</path>
  11.            </configuration>
  12.        </plugin>
  13.    </plugins>
  14. </build>
复制代码
2. 在Maven的settings.xml中配置Tomcat服务器凭据:<servers>
<server>
   <id>TomcatServer</id>
   <username>admin</username>
   <password>password</password>
</server>
</servers>
3. 部署到Tomcat:mvn tomcat7:deploy
4. 重新部署:mvn tomcat7:redeploy
5. 取消部署:mvn tomcat7:undeploy

配置Tomcat插件:
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.apache.tomcat.maven</groupId>
  5.            <artifactId>tomcat7-maven-plugin</artifactId>
  6.            <version>2.2</version>
  7.            <configuration>
  8.                <url>http://localhost:8080/manager/text</url>
  9.                <server>TomcatServer</server>
  10.                <path>/my-app</path>
  11.            </configuration>
  12.        </plugin>
  13.    </plugins>
  14. </build>
复制代码

在Maven的settings.xml中配置Tomcat服务器凭据:
  1. <servers>
  2. <server>
  3.    <id>TomcatServer</id>
  4.    <username>admin</username>
  5.    <password>password</password>
  6. </server>
  7. </servers>
复制代码

部署到Tomcat:
  1. mvn tomcat7:deploy
复制代码

重新部署:
  1. mvn tomcat7:redeploy
复制代码

取消部署:
  1. mvn tomcat7:undeploy
复制代码

部署到其他服务器

1. 配置JBoss插件:<build>
   <plugins>
       <plugin>
           <groupId>org.wildfly.plugins</groupId>
           <artifactId>wildfly-maven-plugin</artifactId>
           <version>2.0.2.Final</version>
           <configuration>
               <hostname>localhost</hostname>
               <port>9990</port>
               <username>admin</username>
               <password>password</password>
           </configuration>
       </plugin>
   </plugins>
</build>
2. 部署到JBoss/WildFly:mvn wildfly:deploy

配置JBoss插件:
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>org.wildfly.plugins</groupId>
  5.            <artifactId>wildfly-maven-plugin</artifactId>
  6.            <version>2.0.2.Final</version>
  7.            <configuration>
  8.                <hostname>localhost</hostname>
  9.                <port>9990</port>
  10.                <username>admin</username>
  11.                <password>password</password>
  12.            </configuration>
  13.        </plugin>
  14.    </plugins>
  15. </build>
复制代码

部署到JBoss/WildFly:
  1. mvn wildfly:deploy
复制代码

1.
  1. 使用Docker Maven插件:<build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>com.spotify</groupId>
  5.            <artifactId>docker-maven-plugin</artifactId>
  6.            <version>1.2.2</version>
  7.            <configuration>
  8.                <imageName>my-app</imageName>
  9.                <dockerDirectory>src/main/docker</dockerDirectory>
  10.                <resources>
  11.                    <resource>
  12.                        <targetPath>/</targetPath>
  13.                        <directory>${project.build.directory}</directory>
  14.                        <include>${project.build.finalName}.jar</include>
  15.                    </resource>
  16.                </resources>
  17.            </configuration>
  18.        </plugin>
  19.    </plugins>
  20. </build>
复制代码
2.
  1. 创建Dockerfile:FROM openjdk:11
  2. VOLUME /tmp
  3. ADD my-app.jar app.jar
  4. ENTRYPOINT ["java","-jar","/app.jar"]
复制代码
3. 构建Docker镜像:mvn clean package docker:build
4. 运行Docker容器:docker run -p 8080:8080 -t my-app

使用Docker Maven插件:
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>com.spotify</groupId>
  5.            <artifactId>docker-maven-plugin</artifactId>
  6.            <version>1.2.2</version>
  7.            <configuration>
  8.                <imageName>my-app</imageName>
  9.                <dockerDirectory>src/main/docker</dockerDirectory>
  10.                <resources>
  11.                    <resource>
  12.                        <targetPath>/</targetPath>
  13.                        <directory>${project.build.directory}</directory>
  14.                        <include>${project.build.finalName}.jar</include>
  15.                    </resource>
  16.                </resources>
  17.            </configuration>
  18.        </plugin>
  19.    </plugins>
  20. </build>
复制代码

创建Dockerfile:
  1. FROM openjdk:11
  2. VOLUME /tmp
  3. ADD my-app.jar app.jar
  4. ENTRYPOINT ["java","-jar","/app.jar"]
复制代码

构建Docker镜像:
  1. mvn clean package docker:build
复制代码

运行Docker容器:
  1. docker run -p 8080:8080 -t my-app
复制代码

高级技巧

使用Maven Profile

Maven Profile允许根据不同环境(开发、测试、生产等)使用不同的配置。

1.
  1. 定义Profile:<profiles>
  2.    <profile>
  3.        <id>dev</id>
  4.        <activation>
  5.            <activeByDefault>true</activeByDefault>
  6.        </activation>
  7.        <properties>
  8.            <environment>dev</environment>
  9.            <database.url>jdbc:mysql://localhost:3306/dev_db</database.url>
  10.        </properties>
  11.    </profile>
  12.    <profile>
  13.        <id>test</id>
  14.        <properties>
  15.            <environment>test</environment>
  16.            <database.url>jdbc:mysql://test-server:3306/test_db</database.url>
  17.        </properties>
  18.    </profile>
  19.    <profile>
  20.        <id>prod</id>
  21.        <properties>
  22.            <environment>prod</environment>
  23.            <database.url>jdbc:mysql://prod-server:3306/prod_db</database.url>
  24.        </properties>
  25.    </profile>
  26. </profiles>
复制代码
2. 使用Profile:mvn clean package -Pprod
3. 在IntelliJ IDEA中激活Profile:打开Maven工具窗口点击”Profiles”按钮选择要激活的Profile
4. 打开Maven工具窗口
5. 点击”Profiles”按钮
6. 选择要激活的Profile
7. 基于条件的Profile激活:<profile>
   <id>jdk11</id>
   <activation>
       <jdk>11</jdk>
   </activation>
   <properties>
       <java.version>11</java.version>
   </properties>
</profile>

定义Profile:
  1. <profiles>
  2.    <profile>
  3.        <id>dev</id>
  4.        <activation>
  5.            <activeByDefault>true</activeByDefault>
  6.        </activation>
  7.        <properties>
  8.            <environment>dev</environment>
  9.            <database.url>jdbc:mysql://localhost:3306/dev_db</database.url>
  10.        </properties>
  11.    </profile>
  12.    <profile>
  13.        <id>test</id>
  14.        <properties>
  15.            <environment>test</environment>
  16.            <database.url>jdbc:mysql://test-server:3306/test_db</database.url>
  17.        </properties>
  18.    </profile>
  19.    <profile>
  20.        <id>prod</id>
  21.        <properties>
  22.            <environment>prod</environment>
  23.            <database.url>jdbc:mysql://prod-server:3306/prod_db</database.url>
  24.        </properties>
  25.    </profile>
  26. </profiles>
复制代码

使用Profile:
  1. mvn clean package -Pprod
复制代码

在IntelliJ IDEA中激活Profile:

• 打开Maven工具窗口
• 点击”Profiles”按钮
• 选择要激活的Profile

基于条件的Profile激活:
  1. <profile>
  2.    <id>jdk11</id>
  3.    <activation>
  4.        <jdk>11</jdk>
  5.    </activation>
  6.    <properties>
  7.        <java.version>11</java.version>
  8.    </properties>
  9. </profile>
复制代码

多模块项目开发

Maven支持多模块项目,便于管理大型复杂项目。

1.
  1. 创建父项目POM:<project xmlns="http://maven.apache.org/POM/4.0.0"
  2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.    <modelVersion>4.0.0</modelVersion>
  5.    <groupId>com.example</groupId>
  6.    <artifactId>my-project</artifactId>
  7.    <version>1.0-SNAPSHOT</version>
  8.    <packaging>pom</packaging>
  9.    <modules>
  10.        <module>module-a</module>
  11.        <module>module-b</module>
  12.        <module>web-app</module>
  13.    </modules>
  14.    <properties>
  15.        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16.        <maven.compiler.source>11</maven.compiler.source>
  17.        <maven.compiler.target>11</maven.compiler.target>
  18.    </properties>
  19.    <dependencyManagement>
  20.        <dependencies>
  21.            <dependency>
  22.                <groupId>org.springframework.boot</groupId>
  23.                <artifactId>spring-boot-dependencies</artifactId>
  24.                <version>2.6.3</version>
  25.                <type>pom</type>
  26.                <scope>import</scope>
  27.            </dependency>
  28.        </dependencies>
  29.    </dependencyManagement>
  30.    <build>
  31.        <pluginManagement>
  32.            <plugins>
  33.                <plugin>
  34.                    <groupId>org.springframework.boot</groupId>
  35.                    <artifactId>spring-boot-maven-plugin</artifactId>
  36.                    <version>2.6.3</version>
  37.                </plugin>
  38.            </plugins>
  39.        </pluginManagement>
  40.    </build>
  41. </project>
复制代码
2.
  1. 创建子模块:<project xmlns="http://maven.apache.org/POM/4.0.0"
  2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.    <modelVersion>4.0.0</modelVersion>
  5.    <parent>
  6.        <groupId>com.example</groupId>
  7.        <artifactId>my-project</artifactId>
  8.        <version>1.0-SNAPSHOT</version>
  9.    </parent>
  10.    <artifactId>module-a</artifactId>
  11.    <packaging>jar</packaging>
  12.    <dependencies>
  13.        <dependency>
  14.            <groupId>org.springframework.boot</groupId>
  15.            <artifactId>spring-boot-starter</artifactId>
  16.        </dependency>
  17.    </dependencies>
  18. </project>
复制代码
3.
  1. 模块间依赖:<dependencies>
  2.    <dependency>
  3.        <groupId>com.example</groupId>
  4.        <artifactId>module-a</artifactId>
  5.        <version>${project.version}</version>
  6.    </dependency>
  7. </dependencies>
复制代码
4. 构建多模块项目:mvn clean install
5. 在IntelliJ IDEA中:导入多模块项目使用Maven工具窗口管理模块可以单独构建特定模块或整个项目
6. 导入多模块项目
7. 使用Maven工具窗口管理模块
8. 可以单独构建特定模块或整个项目

创建父项目POM:
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.    <modelVersion>4.0.0</modelVersion>
  5.    <groupId>com.example</groupId>
  6.    <artifactId>my-project</artifactId>
  7.    <version>1.0-SNAPSHOT</version>
  8.    <packaging>pom</packaging>
  9.    <modules>
  10.        <module>module-a</module>
  11.        <module>module-b</module>
  12.        <module>web-app</module>
  13.    </modules>
  14.    <properties>
  15.        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16.        <maven.compiler.source>11</maven.compiler.source>
  17.        <maven.compiler.target>11</maven.compiler.target>
  18.    </properties>
  19.    <dependencyManagement>
  20.        <dependencies>
  21.            <dependency>
  22.                <groupId>org.springframework.boot</groupId>
  23.                <artifactId>spring-boot-dependencies</artifactId>
  24.                <version>2.6.3</version>
  25.                <type>pom</type>
  26.                <scope>import</scope>
  27.            </dependency>
  28.        </dependencies>
  29.    </dependencyManagement>
  30.    <build>
  31.        <pluginManagement>
  32.            <plugins>
  33.                <plugin>
  34.                    <groupId>org.springframework.boot</groupId>
  35.                    <artifactId>spring-boot-maven-plugin</artifactId>
  36.                    <version>2.6.3</version>
  37.                </plugin>
  38.            </plugins>
  39.        </pluginManagement>
  40.    </build>
  41. </project>
复制代码

创建子模块:
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.    <modelVersion>4.0.0</modelVersion>
  5.    <parent>
  6.        <groupId>com.example</groupId>
  7.        <artifactId>my-project</artifactId>
  8.        <version>1.0-SNAPSHOT</version>
  9.    </parent>
  10.    <artifactId>module-a</artifactId>
  11.    <packaging>jar</packaging>
  12.    <dependencies>
  13.        <dependency>
  14.            <groupId>org.springframework.boot</groupId>
  15.            <artifactId>spring-boot-starter</artifactId>
  16.        </dependency>
  17.    </dependencies>
  18. </project>
复制代码

模块间依赖:
  1. <dependencies>
  2.    <dependency>
  3.        <groupId>com.example</groupId>
  4.        <artifactId>module-a</artifactId>
  5.        <version>${project.version}</version>
  6.    </dependency>
  7. </dependencies>
复制代码

构建多模块项目:
  1. mvn clean install
复制代码

在IntelliJ IDEA中:

• 导入多模块项目
• 使用Maven工具窗口管理模块
• 可以单独构建特定模块或整个项目

自定义Maven插件

Maven允许开发自定义插件,以满足特定需求。

1. 创建Maven插件项目:mvn archetype:generate -DgroupId=com.example.plugins -DartifactId=my-maven-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo
2.
  1. 编写Mojo(Maven普通Java对象):
  2. “`java
  3. package com.example.plugins;
复制代码

创建Maven插件项目:
  1. mvn archetype:generate -DgroupId=com.example.plugins -DartifactId=my-maven-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo
复制代码

编写Mojo(Maven普通Java对象):
“`java
package com.example.plugins;

import org.apache.maven.plugin.AbstractMojo;
   import org.apache.maven.plugin.MojoExecutionException;
   import org.apache.maven.plugins.annotations.Mojo;
   import org.apache.maven.plugins.annotations.Parameter;

/**
  1. * Says "Hi" to the user.
  2. */
复制代码

@Mojo(name = “sayhi”)
   public class GreetingMojo extends AbstractMojo {
  1. /**
  2.     * The greeting to display.
  3.     */
  4.    @Parameter(property = "greeting", defaultValue = "Hello World!")
  5.    private String greeting;
  6.    public void execute() throws MojoExecutionException {
  7.        getLog().info(greeting);
  8.    }
复制代码

}
  1. 3. 构建插件:
  2.    ```bash
  3.    mvn clean install
复制代码

1. 在其他项目中使用自定义插件:<build>
   <plugins>
       <plugin>
           <groupId>com.example.plugins</groupId>
           <artifactId>my-maven-plugin</artifactId>
           <version>1.0-SNAPSHOT</version>
           <executions>
               <execution>
                   <phase>compile</phase>
                   <goals>
                       <goal>sayhi</goal>
                   </goals>
                   <configuration>
                       <greeting>Hi from custom plugin!</greeting>
                   </configuration>
               </execution>
           </executions>
       </plugin>
   </plugins>
</build>
2. 运行自定义插件:mvn compile

在其他项目中使用自定义插件:
  1. <build>
  2.    <plugins>
  3.        <plugin>
  4.            <groupId>com.example.plugins</groupId>
  5.            <artifactId>my-maven-plugin</artifactId>
  6.            <version>1.0-SNAPSHOT</version>
  7.            <executions>
  8.                <execution>
  9.                    <phase>compile</phase>
  10.                    <goals>
  11.                        <goal>sayhi</goal>
  12.                    </goals>
  13.                    <configuration>
  14.                        <greeting>Hi from custom plugin!</greeting>
  15.                    </configuration>
  16.                </execution>
  17.            </executions>
  18.        </plugin>
  19.    </plugins>
  20. </build>
复制代码

运行自定义插件:
  1. mvn compile
复制代码

总结

本文详细介绍了使用IntelliJ IDEA运行Maven项目的全过程,从环境配置到项目部署,并解决了开发过程中可能遇到的各种问题。通过掌握这些知识和技巧,你可以显著提高Java开发效率,成为一名高效的Java开发者。

IntelliJ IDEA和Maven的强大组合为Java开发提供了极大的便利,但真正掌握它们需要不断的实践和探索。希望本文能够帮助你更好地理解和使用这些工具,在实际开发中游刃有余。

随着技术的发展,IntelliJ IDEA和Maven也在不断更新和改进,建议持续关注它们的最新版本和特性,以便在开发中充分利用这些新功能。同时,积极参与社区讨论,分享经验和解决方案,也是提高自身技能的有效途径。
「七転び八起き(ななころびやおき)」
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则