Skip to content

Maven 进阶内容

内容待优化

问题解决

➡️ 待实际记录

Spring Boot + Maven 使用

启动器依赖优化

xml
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

打包插件配置

xml
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <excludeDevtools>true</excludeDevtools>
        <executable>true</executable>
      </configuration>
    </plugin>
  </plugins>
</build>

Profile多环境切换

xml
<profiles>
  <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <activatedProperties>dev</activatedProperties>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <activatedProperties>prod</activatedProperties>
    </properties>
  </profile>
</profiles>

目录结构:

src/main/resources/
├── application-dev.properties
├── application-prod.properties
└── application.properties

资源过滤

xml
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <configuration>
    <delimiters>
      <delimiter>@</delimiter>
    </delimiters>
    <useDefaultDelimiters>false</useDefaultDelimiters>
  </configuration>
</plugin>

Swagger配置应用:

java
@Bean
public OpenAPI customOpenAPI(
    @Value("${app.version}") String appVersion) {
  return new OpenAPI().info(
      new Info().title("API文档")
          .version(appVersion));
}

Maven进阶核心技能

讲述一下技能篇,知识内容

多模块依赖管理

  • 场景:企业级项目模块化拆分
  • 配置
xml
<modules>
  <module>common-utils</module>
  <module>business-service</module>
  <module>web-api</module>
</modules>

<parent>
  <groupId>com.example</groupId>
  <artifactId>parent-pom</artifactId>
  <version>1.0.0</version>
  <relativePath>../pom.xml</relativePath>
</parent>

依赖版本统一管理

xml
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>3.2.4</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

构建资源过滤

xml
<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

应用示例:

app.version=@project.version@
build.time=@timestamp@

搭建项目使用maven的一些优化建议

➡️ 待实际项目中使用到,记录和思考

  • CI/CD集成
  • 安全加固
  • 依赖安全扫描

性能优化指标对比

可以尝试修改配置,或者使用不同方式,然后看一下项目的构建时间以及打包后的占用磁盘空间大小。

优化措施构建时间减少包体积缩减
并行构建(-T)35%-50%-
分层Docker镜像构建-60%-70%
排除无用依赖15%-20%30%-50%
增量编译(IDE集成)70%-90%-

推荐工具链:(⬇️ 这个部署很理解,平时使用很少,实际用到的话,可以更新一下)

  • 依赖分析:mvn dependency:analyze
  • 构建可视化:Maven Build Profiler
  • 镜像构建:Jib Maven Plugin
  • 文档生成:Maven Site Plugin

参考内容