基于Maven搭建指定模块结构的多分层Java项目技术问询
Hey there! Let's walk through setting up your multi-layered Java project with Maven exactly as you described. Since you've already started with a project named flash, here's how to structure each module properly:
The root pom acts as the parent/aggregator for all modules. It should have a pom packaging type and list all child modules.
<!-- flash/pom.xml --> <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.example.flash</groupId> <artifactId>flash-root</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <name>Flash Root Project</name> <!-- List all child modules --> <modules> <module>flash-ear</module> <module>flash-common</module> <module>flash-dal</module> <module>flash-service</module> <module>flash-gui</module> <module>flash-gateway</module> </modules> <!-- Optional: Define common dependencies/plugins for all modules --> <dependencyManagement> <dependencies> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-dal</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-service</artifactId> <version>${project.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
This module is shared across all layers, so it should contain reusable utilities, constants, or DTOs. Packaging type is jar.
<!-- flash/flash-common/pom.xml --> <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"> <parent> <groupId>com.example.flash</groupId> <artifactId>flash-root</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>flash-common</artifactId> <packaging>jar</packaging> <name>Flash Common Layer</name> <!-- Add shared dependencies here (e.g., Lombok, Apache Commons) --> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> <scope>provided</scope> </dependency> </dependencies> </project>
Handles database interactions, depends only on the CommonLayer. Packaging type is jar.
<!-- flash/flash-dal/pom.xml --> <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"> <parent> <groupId>com.example.flash</groupId> <artifactId>flash-root</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>flash-dal</artifactId> <packaging>jar</packaging> <name>Flash Data Access Layer</name> <dependencies> <!-- Depend on CommonLayer --> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-common</artifactId> </dependency> <!-- Add DB dependencies (e.g., Hibernate, Spring Data JPA) --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>6.4.4.Final</version> </dependency> </dependencies> </project>
Contains business logic, depends on DataAccessLayer and CommonLayer. Packaging type is jar.
<!-- flash/flash-service/pom.xml --> <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"> <parent> <groupId>com.example.flash</groupId> <artifactId>flash-root</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>flash-service</artifactId> <packaging>jar</packaging> <name>Flash Service Layer</name> <dependencies> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-common</artifactId> </dependency> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-dal</artifactId> </dependency> <!-- Add business logic dependencies (e.g., Spring Context) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.1.5</version> </dependency> </dependencies> </project>
Web module for UI, depends on ServiceModule and CommonLayer. Packaging type is war.
<!-- flash/flash-gui/pom.xml --> <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"> <parent> <groupId>com.example.flash</groupId> <artifactId>flash-root</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>flash-gui</artifactId> <packaging>war</packaging> <name>Flash GUI Module</name> <dependencies> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-common</artifactId> </dependency> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-service</artifactId> </dependency> <!-- Add JSF/JSP dependencies (e.g., Mojarra) --> <dependency> <groupId>org.glassfish</groupId> <artifactId>jakarta.faces</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies> <!-- Maven WAR plugin configuration --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.2</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
Web module for REST APIs, depends on ServiceModule and CommonLayer. Packaging type is war.
<!-- flash/flash-gateway/pom.xml --> <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"> <parent> <groupId>com.example.flash</groupId> <artifactId>flash-root</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>flash-gateway</artifactId> <packaging>war</packaging> <name>Flash Gateway Module</name> <dependencies> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-common</artifactId> </dependency> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-service</artifactId> </dependency> <!-- Add REST dependencies (e.g., Spring Web) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>6.1.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.2</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
Packages both GUIModule and GatewayModule into an EAR for application server deployment. Packaging type is ear.
<!-- flash/flash-ear/pom.xml --> <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"> <parent> <groupId>com.example.flash</groupId> <artifactId>flash-root</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>flash-ear</artifactId> <packaging>ear</packaging> <name>Flash EAR Module</name> <dependencies> <!-- Include web modules as EAR modules --> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-gui</artifactId> <type>war</type> </dependency> <dependency> <groupId>com.example.flash</groupId> <artifactId>flash-gateway</artifactId> <type>war</type> </dependency> </dependencies> <!-- Maven EAR plugin configuration --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-ear-plugin</artifactId> <version>3.3.0</version> <configuration> <version>8</version> <defaultLibBundleDir>lib</defaultLibBundleDir> <modules> <webModule> <groupId>com.example.flash</groupId> <artifactId>flash-gui</artifactId> <contextRoot>/flash-gui</contextRoot> </webModule> <webModule> <groupId>com.example.flash</groupId> <artifactId>flash-gateway</artifactId> <contextRoot>/flash-gateway</contextRoot> </webModule> </modules> </configuration> </plugin> </plugins> </build> </project>
Final Project Directory Structure
Once all modules are created, your project will look like this:
flash/ ├── pom.xml # Root POM ├── flash-ear/ │ └── pom.xml # EAR module ├── flash-common/ │ └── pom.xml # Common layer ├── flash-dal/ │ └── pom.xml # Data access layer ├── flash-service/ │ └── pom.xml # Business service layer ├── flash-gui/ │ ├── pom.xml # JSF/JSP web module │ └── src/main/webapp/ # UI resources └── flash-gateway/ ├── pom.xml # REST web service module └── src/main/webapp/
Building the Project
From the root flash/ directory, run:
mvn clean install
This will build all modules and generate the final EAR file in flash-ear/target/flash-ear-1.0.0-SNAPSHOT.ear, ready for deployment to servers like WildFly or WebLogic.
内容的提问来源于stack exchange,提问作者Hosein Aqajani

