🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] # 示例 > ## 基础 POM.xml 1. dependencies:依赖标签,用于声明项目的依赖jar包,这也是Maven的核心功能之一。 2. plugins:插件标签,用于配置各种构建插件,扩展Maven的功能。 3. build:构建标签,控制项目构建策略,比如编译版本、资源过滤、输出目录、插件执行顺序等。 4. profiles: profiles标签,定义不同环境(dev/test/prod)的构建profile,可以切换不同的依赖配置。 5. repositories:仓库标签,定义第三方jar包仓库,扩展 jar 包来源。 6. parent:继承父POM,实现项目重复配置的抽取与继承。 7. properties:属性标签,定义Maven常用变量,扩展POM的灵活性。 8. dependencyManagement:依赖管理标签,管理jar包版本,实现版本统一管理。 9. modules:模块标签,定义项目的子模块结构,实现模块依赖与继承。 10. scm:源码管理标签,定义项目的源码存放地址。方便开发人员获取最新源码。 ``` <?xml version="1.0" encoding="UTF-8"?> <!-- POM文件的根元素,用于定义项目的基本信息和依赖关系 --> <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"> <!-- POM模型版本号 --> <modelVersion>4.0.0</modelVersion> <!-- 项目的组ID --> <groupId>com.example</groupId> <!-- 项目的唯一标识符 --> <artifactId>example-project</artifactId> <!-- 项目的版本号 --> <version>1.0.0-SNAPSHOT</version> <!-- 项目的打包方式,默认为jar --> <packaging>jar</packaging> <!-- 项目的名称 --> <name>Example Project</name> <!-- 项目的描述信息 --> <description>This is an example project for demonstration purposes.</description> <!-- 项目的主页地址 --> <url>https://www.example.com</url> <!-- 项目的许可证信息 --> <licenses> <license> <name>Apache License 2.0</name> <url>https://www.apache.org/licenses/LICENSE-2.0</url> </license> </licenses> <!-- 项目的开发者信息 --> <developers> <developer> <id>johndoe</id> <name>John Doe</name> <email>johndoe@example.com</email> <organization>Example Organization</organization> <organizationUrl>https://www.example.com</organizationUrl> <roles> <role>developer</role> </roles> </developer> </developers> <!-- 项目的依赖关系 --> <dependencies> <!-- 项目的依赖项 --> <dependency> <!-- 依赖项的组ID --> <groupId>com.example</groupId> <!-- 依赖项的唯一标识符 --> <artifactId>example-library</artifactId> <!-- 依赖项的版本号 --> <version>1.0.0</version> <!-- 依赖项的作用范围 --> <scope>compile</scope> <!-- 是否为可选依赖 --> <optional>false</optional> <!-- 排除依赖项 --> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <!-- 项目的仓库信息 --> <repositories> <!-- Maven仓库的地址和协议信息 --> <repository> <!-- 仓库的唯一标识符 --> <id>central</id> <!-- 仓库的URL地址 --> <url>https://repo.maven.apache.org/maven2/</url> </repository> </repositories> <!-- 项目的构建信息 --> <build> <!-- 源代码目录 --> <sourceDirectory>src/main/java</sourceDirectory> <!-- 最终构建产物的名称 --> <finalName>example-project</finalName> <!-- 项目的插件信息 --> <plugins> <!-- 项目的插件 --> <plugin> <!-- 插件的唯一标识符 --> <groupId>org.apache.maven.plugins</groupId> <!-- 插件的唯一标识符(续) --> <artifactId>maven-compiler-plugin</artifactId> <!-- 插件的版本号 --> <version>3.8.1</version> <!-- 插件的执行配置信息 --> <executions> <!-- 插件的执行配置 --> <execution> <!-- 配置项的激活条件 --> <id>default-compile</id> <!-- 插件的目标 --> <goals> <goal>compile</goal> </goals> </execution> </executions> <!-- 插件的配置信息 --> <configuration> <!-- 插件的配置项 --> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project> ``` > ## 项目构建 build ``` <!-- 项目的构建信息 --> <build> <!-- 源代码目录 --> <sourceDirectory>src/main/java</sourceDirectory> <!-- 最终构建产物的名称 --> <finalName>example-project</finalName> <!-- 项目的插件信息 --> <plugins> <!-- 项目的插件 --> <plugin> <!-- 插件的唯一标识符 --> <groupId>org.apache.maven.plugins</groupId> <!-- 插件的唯一标识符(续) --> <artifactId>maven-compiler-plugin</artifactId> <!-- 插件的版本号 --> <version>3.8.1</version> <!-- 插件的执行配置信息 --> <executions> <!-- 插件的执行配置 --> <execution> <!-- 配置项的激活条件 --> <id>default-compile</id> <!-- 插件的目标 --> <goals> <goal>compile</goal> </goals> </execution> </executions> <!-- 插件的配置信息 --> <configuration> <!-- 插件的配置项 --> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> ``` > ## 项目仓库信息 repositories ``` <!-- 项目的仓库信息 --> <repositories> <!-- Maven仓库的地址和协议信息 --> <repository> <!-- 仓库的唯一标识符 --> <id>central</id> <!-- 仓库的URL地址 --> <url>https://repo.maven.apache.org/maven2/</url> </repository> </repositories> ``` > ## 项目依赖信息 ``` <!-- 项目的依赖关系 --> <dependencies> <!-- 项目的依赖项 --> <dependency> <!-- 依赖项的组ID --> <groupId>com.example</groupId> <!-- 依赖项的唯一标识符 --> <artifactId>example-library</artifactId> <!-- 依赖项的版本号 --> <version>1.0.0</version> <!-- 依赖项的作用范围 --> <scope>compile</scope> <!-- 是否为可选依赖 --> <optional>false</optional> <!-- 排除依赖项 --> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> </dependencies> ``` > ## 项目开发者信息 ``` <!-- 项目的开发者信息 --> <developers> <developer> <id>johndoe</id> <name>John Doe</name> <email>johndoe@example.com</email> <organization>Example Organization</organization> <organizationUrl>https://www.example.com</organizationUrl> <roles> <role>developer</role> </roles> </developer> </developers> ``` >## PIG 项目 pom.xml 示例 ``` <?xml version="1.0" encoding="UTF-8"?> <!-- ~ Copyright (c) 2020 pig4cloud Authors. All Rights Reserved. ~ ~ Licensed under the Apache License, Version 2.0 (the "License"); ~ you may not use this file except in compliance with the License. ~ You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, software ~ distributed under the License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~ See the License for the specific language governing permissions and ~ limitations under the License. --> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" 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.pig4cloud</groupId> <artifactId>pig</artifactId> <name>${project.artifactId}</name> <!-- name标签定义项目显示名称,一般引用${project.artifactId}变量 --> <version>3.6.5</version> <packaging>pom</packaging> <!-- packaging标签定义项目的打包方式,可选jar、war、pom等 --> <url>https://www.pig4cloud.com</url> <!-- properties标签定义项目属性,常用于定义版本号等 --> <properties> <spring-boot.version>2.7.7</spring-boot.version> <spring-cloud.version>2021.0.5</spring-cloud.version> <spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring-boot-admin.version>2.7.9</spring-boot-admin.version> <spring.authorization.version>0.4.0</spring.authorization.version> <dynamic-ds.version>3.6.0</dynamic-ds.version> <captcha.version>2.2.2</captcha.version> <velocity.version>2.3</velocity.version> <velocity.tool.version>3.1</velocity.tool.version> <configuration.version>1.10</configuration.version> <jasypt.version>2.1.0</jasypt.version> <swagger.fox.version>3.0.0</swagger.fox.version> <knife4j.ui.version>3.0.3</knife4j.ui.version> <xxl-job.version>2.3.1</xxl-job.version> <docker.plugin.version>0.32.0</docker.plugin.version> <docker.host>http://192.168.0.100:2375</docker.host> <docker.registry>192.168.0.100</docker.registry> <docker.namespace>pig4cloud</docker.namespace> <docker.username>username</docker.username> <docker.password>password</docker.password> <git.commit.plugin>4.9.9</git.commit.plugin> <spring.checkstyle.plugin>0.0.35</spring.checkstyle.plugin> </properties> <!-- dependencies标签定义项目的依赖 --> <!-- 以下依赖 全局所有的模块都会引入 --> <dependencies> <!--配置文件处理器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--配置文件加解密--> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>${jasypt.version}</version> </dependency> <!--监控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--监控客户端--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>${spring-boot-admin.version}</version> </dependency> <!--Lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <!--测试依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!-- modules标签定义项目的子模块 --> <modules> <module>pig-register</module> <module>pig-gateway</module> <module>pig-auth</module> <module>pig-upms</module> <module>pig-common</module> <module>pig-visual</module> <module>xudemo</module> </modules> <!-- dependencyManagement标签定义依赖管理,用于控制jar包的版本 --> <dependencyManagement> <dependencies> <!--pig 公共版本定义--> <dependency> <groupId>com.pig4cloud</groupId> <artifactId>pig-common-bom</artifactId> <version>${project.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring boot 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring cloud 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring cloud alibaba 依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- build标签定义项目的构建设置,比如编译版本、输出目录、插件配置等 --> <build> <finalName>${project.name}</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <finalName>${project.build.finalName}</finalName> <layers> <enabled>true</enabled> </layers> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>${docker.plugin.version}</version> <configuration> <!-- Docker Remote Api--> <dockerHost>${docker.host}</dockerHost> <!-- Docker 镜像私服--> <registry>${docker.registry}</registry> <!-- 认证信息--> <authConfig> <push> <username>${docker.username}</username> <password>${docker.password}</password> </push> </authConfig> <images> <image> <!-- 镜像名称: 172.17.0.111/library/pig-gateway:2.6.3--> <name>${docker.registry}/${docker.namespace}/${project.name}:${project.version}</name> <build> <dockerFile>${project.basedir}/Dockerfile</dockerFile> </build> </image> </images> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <!--打包jar 与git commit 关联插件--> <plugin> <groupId>io.github.git-commit-id</groupId> <artifactId>git-commit-id-maven-plugin</artifactId> <version>${git.commit.plugin}</version> <executions> <execution> <id>get-the-git-infos</id> <goals> <goal>revision</goal> </goals> <phase>initialize</phase> </execution> </executions> <configuration> <failOnNoGitDirectory>false</failOnNoGitDirectory> <generateGitPropertiesFile>true</generateGitPropertiesFile> <!--因为项目定制了jackson的日期时间序列化/反序列化格式,因此这里要进行配置,不然通过management.info.git.mode=full进行完整git信息监控时会存在问题--> <dateFormat>yyyy-MM-dd HH:mm:ss</dateFormat> <includeOnlyProperties> <includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty> <includeOnlyProperty>^git.commit.(id|message|time).*$</includeOnlyProperty> </includeOnlyProperties> </configuration> </plugin> <!-- 代码格式插件,默认使用spring 规则,可运行命令进行项目格式化:./mvnw spring-javaformat:apply 或 mvn spring-javaformat:apply,可在IDEA中安装插件以下插件进行自动格式化: https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-intellij-idea-plugin --> <plugin> <groupId>io.spring.javaformat</groupId> <artifactId>spring-javaformat-maven-plugin</artifactId> <version>${spring.checkstyle.plugin}</version> <executions> <execution> <phase>validate</phase> <inherited>true</inherited> <goals> <goal>validate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <!-- profiles标签定义环境切换profiles,可以根据不同环境进行依赖调整等设置 --> <profiles> <profile> <id>dev</id> <properties> <!-- 环境标识,需要与配置文件的名称相对应 --> <profiles.active>dev</profiles.active> </properties> <activation> <!-- 默认环境 --> <activeByDefault>true</activeByDefault> </activation> </profile> </profiles> </project> ```