Master Maven: From Plugin Setup to Full Web Project Build
This guide walks through Maven’s core workflow for Java backend development, covering plugin configuration, automatic project setup in IntelliJ IDEA, detailed pom.xml settings—including properties, dependencies, plugins—and code examples for Servlets and JSPs, plus dependency scope handling to avoid runtime conflicts.
Introduction
In the Java ecosystem, Maven is the cornerstone of engineering development, providing standardized processes for dependency management and build automation.
1. Maven Plugin Configuration
Maven plugins extend Maven’s core capabilities. To configure the installation path, configuration file path, and local repository, edit the Settings.xml file.
After setting these values, the Maven plugin is configured, but the configuration only applies to the current project.
To make the configuration reusable for new projects, use the IDE menu:
file → New Projects Setup → Settings for New Projects...
After repeating the same steps, newly created projects will inherit the Maven configuration automatically.
2. Building a Maven Web Project
Steps to create a Maven web project in IntelliJ IDEA:
Open IDEA and select “Create New Project”.
Choose “Maven” and select the web project archetype.
Manually add the src/main/java directory (initially cannot contain Java files).
Mark the src/main/java folder as “Sources Root” to enable Java editing.
Add source files and ensure the test folder is recognized as a test source.
After creating the directories, add files as needed.
Mark the src/main/java folder as “Sources Root” to write Java code, and the test folder is automatically set as a test source.
3. pom.xml Configuration
The pom.xml file is the “specification” of a Maven project, declaring dependencies, build plugins, and project metadata.
Project basic information (groupId, artifactId, version) is placed at the top of the file.
3.1 <properties>
Define configuration values such as source encoding and Java version.
<properties>
<!-- 编码格式 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 编译时使用的JAVA版本 -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>3.2 <dependencies>
For a Java web project, add javax.servlet and javax.servlet.jsp dependencies. Use https://mvnrepository.com/ to find the correct coordinates.
<!-- servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- jsp依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>3.3 <plugins>
Add the Tomcat 7 Maven plugin so that Maven can start an embedded Tomcat server.
<plugins>
<!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8080</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>4. Writing Code
Example Servlet:
@WebServlet("/demo1")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 跳转页面
req.getRequestDispatcher("hello.jsp").forward(req, resp);
}
}Example JSP (hello.jsp):
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>大家好,感谢支持艺杯羹!</h1>
</body>
</html>Run the project via IDEA’s Maven run configuration:
Open “Edit Configurations…”.
Add a new Maven configuration with goal tomcat7:run.
Apply and run using the IDE’s run button.
5. Dependency Scope
Running the servlet may produce a 500 error because the project includes servlet and JSP JARs that conflict with Tomcat’s own libraries.
Set the scope of these dependencies to provided so they are only needed at compile time.
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>After adding the provided scope, the application runs without JAR conflicts.
6. Summary
This tutorial demonstrated how to configure Maven plugins, set up a reusable Maven environment, build a Java web project in IntelliJ IDEA, define essential pom.xml sections (properties, dependencies, plugins), write basic Servlet and JSP code, run the project with the Tomcat Maven plugin, and resolve runtime conflicts by using the appropriate dependency scope.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
