如何自定义包含许可证信息的模板?

解读

面试官抛出此题,意在验证候选人是否真正理解 Maven 构建输出的合规性与企业级可维护性。国内银行、保险、证券等甲方在引入开源组件时,必须生成带许可证的物料清单(SBOM)以备审计;同时,ToB 交付件也需随包附带 License 文件。只会用 maven-jar-plugin 默认生成 META-INF 的候选人,会被认为“只懂打包,不懂合规”。因此,回答要围绕“把许可证信息注入最终制品”与“让模板可复用、可继承”两条主线展开,并给出可直接落地的代码级方案。

知识点

  1. POM 级许可证声明:<licenses><license>...</license></licenses> 是法律意义上的声明,但不会自动复制到包里。
  2. 资源过滤(resource filtering):开启 <filtering>true</filtering> 后,Maven 会把 src/main/resources 下的 ${key} 替换为 POM 属性值,是模板引擎最简实现。
  3. maven-resources-plugin 3.2.0+ 的 delimiter 定制:国内项目常把 @ 作为分隔符,避免与 Spring 占位符冲突。
  4. maven-remote-resources-plugin:官方提供的“共享资源束”机制,可把许可证、NOTICE、DISCLAIMER 等文件打包成 resources.zip 并发布到 Nexus 私服,子项目通过 remote-resources:process 统一拉取,实现集团级模板复用。
  5. maven-assembly-plugin 的 component 机制:在 src/assembly/components.xml 中定义 <files><file><source>LICENSE.vm</source><outputDirectory>/</outputDirectory><filtered>true</filtered></file></files>,可把 Velocity 模板渲染后放到 zip/tar.gz 的根目录,满足国内交付“源码+二进制+许可证”三位一体要求。
  6. 合规三件套:LICENSE(许可证文本)、NOTICE(第三方版权)、DISCLAIMER(免责声明),缺一不可;模板中必须包含 ${project.name}、${project.version}、${license.name}、${license.url} 四个变量,方便审计追溯。
  7. CI 门禁:在 GitLab CI 或 Jenkins 中,通过 mvn verify 阶段检查 target/generated-resources/licenses 是否存在,若缺失则流水线失败,防止开发者漏配模板。

答案

分三步落地,全部代码可直接拷贝到企业私服:

第一步,在父 POM 中定义许可证坐标与变量,确保所有子模块继承:

<licenses>
  <license>
    <name>Apache License, Version 2.0</name>
    <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
    <distribution>repo</distribution>
  </license>
</licenses>
<properties>
  <license.dir>${basedir}/src/license</license.dir>
  <license.output.dir>${project.build.directory}/generated-resources/licenses</license.output.dir>
</properties>

第二步,创建 Velocity 模板 src/license/LICENSE.vm,内容如下,关键变量已加粗:

**${project.name}** **${project.version}**
Copyright © **${project.inceptionYear}** **${organization.name}**

This product is licensed under **${license.name}**.
You may obtain a copy of the License at
**${license.url}**

Third-party components and their licenses are listed in NOTICE.txt

把该目录通过 maven-remote-resources-plugin 打成 license-resources-1.0.zip 并 deploy 到 Nexus 的 releases 仓库,集团内任何项目只需:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-remote-resources-plugin</artifactId>
  <version>3.0.0</version>
  <configuration>
    <resourceBundles>
      <resourceBundle>com.xxx.build:license-resources:1.0</resourceBundle>
    </resourceBundles>
  </configuration>
  <executions>
    <execution>
      <goals><goal>process</goal></goals>
    </execution>
  </executions>
</plugin>

即可在 target/generated-resources/licenses 得到渲染后的 LICENSE、NOTICE、DISCLAIMER 三文件。

第三步,把生成的文件打进最终制品。以 Spring Boot 可执行 JAR 为例,在 spring-boot-maven-plugin 之前执行:

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.3.1</version>
  <executions>
    <execution>
      <id>copy-license</id>
      <phase>prepare-package</phase>
      <goals><goal>copy-resources</goal></goals>
      <configuration>
        <outputDirectory>${project.build.outputDirectory}/META-INF</outputDirectory>
        <resources>
          <resource>
            <directory>${license.output.dir}</directory>
            <includes>
              <include>LICENSE</include>
              <include>NOTICE</include>
              <include>DISCLAIMER</include>
            </includes>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>

至此,无论打出的 JAR、WAR 还是通过 assembly:single 生成的 xxx-distribution.tar.gz,根目录或 META-INF 下均自带合规许可证文件,满足国内监管与甲方审计要求。

拓展思考

  1. 如果公司存在双许可证(Community 版用 Apache,Enterprise 版用商业协议),可通过 Maven Profile 切换 license.url 与模板文件,实现同一源码、不同许可证的构建,避免维护两套仓库。
  2. 对于云原生场景,可把 LICENSE 文件通过 dockerfile-maven-plugin 的 <extraDirectories> 复制到镜像的 /licenses/ 目录,满足金融云“容器镜像必须可溯源”的合规基线。
  3. 在 SBOM 生成环节,可结合 cyclonedx-maven-plugin 与自定义模板,把许可证信息写入 bom.json 的 licenses 节点,再通过国内开源镜像站(如清华 TUNA)的漏洞 API 做二次校验,实现“构建即合规、发布即审计”的 DevSecOps 闭环。