新手搭建Maven多模块Java Web项目遇问题,求技术解答
Hey there, let's work through the kinks in your Maven multi-module setup—since you're new to this, I'll break everything down clearly:
1. Fix Your Project Structure & Parent POM Module Order
First, there's a confusion in how you've structured your project vs. how your parent POM defines modules. You described MonEar |_ Module_db1 |_ Module_db2 |_ Module_war, but your parent POM lists all four as top-level modules. That's going to throw Maven off.
The correct structure should have a root parent project (your MonAppMultiModule) that contains all four modules as siblings:
MonAppMultiModule/ (parent project, packaging = pom) ├── Module_db1/ (jar) ├── Module_db2/ (jar) ├── Module_war/ (war) └── MonEar/ (ear, packages the war module)
Your parent POM is on the right track with the modules list, but make sure the parent itself has <packaging>pom</packaging> (this is non-negotiable for parent projects).
2. Correct Duplicate Dependency in Module_war
Looking at your Module_war POM, you've added the Module_db1 dependency twice and completely missed Module_db2 (if your web app needs code from Module_db2, this will cause compilation errors). Fix it like this:
<dependencies> <dependency> <groupId>${project.parent.groupId}</groupId> <artifactId>Module_db1</artifactId> <version>${project.parent.version}</version> </dependency> <dependency> <groupId>${project.parent.groupId}</groupId> <artifactId>Module_db2</artifactId> <version>${project.parent.version}</version> </dependency> </dependencies>
3. Configure the MonEar Module Properly
Since MonEar is an EAR module, it needs specific configuration to package your war correctly. Here's what its POM should look like:
<artifactId>MonEar</artifactId> <packaging>ear</packaging> <name>MonEar</name> <parent> <groupId>MonAppMultiModule</groupId> <artifactId>app.multi.module</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <!-- Pull in the war module as a dependency --> <dependency> <groupId>${project.parent.groupId}</groupId> <artifactId>Module_war</artifactId> <version>${project.parent.version}</version> <type>war</type> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-ear-plugin</artifactId> <version>3.3.0</version> <!-- Use a compatible version --> <configuration> <modules> <webModule> <groupId>${project.parent.groupId}</groupId> <artifactId>Module_war</artifactId> <contextRoot>/your-web-app</contextRoot> <!-- Set your web app's context path here --> </webModule> </modules> </configuration> </plugin> </plugins> </build>
4. Build the Project Correctly
Once all configurations are fixed, navigate to the root parent project directory (MonAppMultiModule) and run this command:
mvn clean install
Maven will automatically build the modules in the correct order (db modules first, then war, then ear) because it resolves dependencies. Your final EAR file will be in MonEar/target/.
内容的提问来源于stack exchange,提问作者jones brown

