Maven Projects Setup

本文详细介绍了如何使用Maven创建Java Web应用,并通过Eclipse/IntelliJ进行项目创建。包括项目初始化设置、添加依赖和配置文件的步骤,以及Maven构建过程的各个阶段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You can create project by maven. But basically, we don’t prefer to do it. We always use Eclipse/IntelliJ to create maven project.

After creating project by maven project template in IntelliJ, we got below folder.
MyWebApp
|–.idea
|–src
|—-main
|—-resources
|–test
|–pom.xml

By default, we only have one setting named as “POM.xml”. It includes all core information related to project building/dependencies/basic artefact information.

Initialize pom.xml:

<?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>myApp</groupId>
    <artifactId>myApp</artifactId>
    <version>1.0-SNAPSHOT</version>
</project>

Based on these initial settings, you can install package through maven clean install command.

Maven Build Process:

–validate: validate the project is correct and all necessary information is available
–compile: compile the source code of the project
–test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
–package: take the compiled code and package it in its distributable format, such as a JAR.
–integration-test: process and deploy the package if necessary into an environment where integration tests can be run
–verify: run any checks to verify the package is valid and meets quality criteria
–install: install the package into the local repository, for use as a dependency in other projects locally
–deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

However, we want to add some dependencies and repositories. Given that, we need to modify pom.xml & add another setting file named as “Settings.xml”.

There are two settings.
POM.xml:
(1) We want to add JUNIT dependency. Given that, we add below settings.

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
</dependencies>

(2) We want to add one plug-in when building projects, so that we add these settings.

<build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.11.v20150529</version>
                <configuration>
                    <webApp>
                        <contextPath>/MyWebApp</contextPath>
                    </webApp>
                    <httpConnector>
                        <port>80</port>
                    </httpConnector>
                </configuration>
            </plugin>
        </plugins>

    </build>

Settings.xml: This configuration manage the global level configuration information for all maven projects.

These include values such as the local repository location, alternate remote repository servers, and authentication information

We define local repository & remote repository.

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <localRepository>/Users/kangxiwei/maven</localRepository>

    <interactiveMode>true</interactiveMode>

    <usePluginRegistry>false</usePluginRegistry>

    <profiles>
        <profile>
            <id>profile-default</id>
            <repositories>
                <repository>
                    <id>repo-mirror</id>
                    <url>http://maven.net.cn/content/groups/public/</url>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>plugin-repo-mirror</id>
                    <url>http://maven.net.cn/content/groups/public/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>

    </profiles>
</settings>

Related links:
Maven in 5 Minutes
Maven Getting Started
Maven Settings.xml

### IntelliJ IDEA 2025 中使用 Maven 进行项目开发的配置指南 IntelliJ IDEA 是目前所有 IDE 中最具备沉浸式的集成开发环境之一,其对 Maven 的支持非常完善,可以极大地提升开发者在构建、管理和部署 Java 项目的效率。以下是在 IntelliJ IDEA 2025 中配置和使用 Maven 项目的详细步骤: #### 创建新的 Maven 项目 1. 打开 IntelliJ IDEA,点击 **File | New | Project**。 2. 在新建项目向导中,选择 **Maven** 并勾选 **Create from archetype**(如果需要从模板创建)[^1]。 3. 点击 **Next**,输入 GroupId、ArtifactId 和 Version,这些是 Maven 项目的基本标识符。 4. 指定项目的存放路径,并点击 **Finish** 完成创建。 #### 导入现有 Maven 项目 1. 点击 **File | Open**,选择包含 `pom.xml` 文件的项目根目录。 2. IntelliJ IDEA 将自动识别并导入 Maven 项目,同时下载所需的依赖项。 3. 如果需要调整 Maven 设置,可以通过 **File | Settings | Build, Execution, Deployment | Build Tools | Maven** 来修改本地仓库路径、User Settings 文件等配置。需要注意的是,IDEA 设置中的 Maven 配置优先级高于自带 Maven 的 `settings.xml` 文件中的设置 [^3]。 #### 配置保存时执行操作(Actions on Save) 在 IntelliJ IDEA 2025 中,可以启用保存文件时自动触发的操作功能: 1. 转到 **File | New Projects Setup | Preferences For New Projects | Tools | Actions on Save**。 2. 勾选需要在保存时执行的任务,例如优化导入或格式化代码,这样可以在每次保存文件时自动执行这些任务 [^2]。 #### 配置 CI/CD 流程中的覆盖率检测 如果你的项目涉及持续集成流程,可以在 Jenkins Pipeline 或 GitHub Actions 中配置 JaCoCo 插件来生成测试覆盖率报告。例如,在 `pom.xml` 中添加如下插件配置: ```xml <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.11</version> <executions> <execution> <goals> <goal>prepare-agent</goal> <goal>report</goal> </goals> </execution> </executions> </plugin> ``` 然后根据具体的 CI 工具配置流水线脚本以生成和上传覆盖率报告 [^4]。 #### 使用 Maven 构建 Web 项目 如果你正在开发基于 Servlet 的 Web 应用,可以使用 IntelliJ IDEA 提供的 Maven 支持快速搭建项目结构。首先确保已安装 JDK 和 Maven,然后通过 IDEA 创建 Maven Webapp 模板项目,并在 `pom.xml` 中添加必要的依赖项,如 Servlet API 和 Tomcat 插件 [^5]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值