使用的插件:license-maven-plugin。执行命令: mvn clean license:aggregate-add-third-party
参考文章:https://blog.csdn.net/Leo_Steven/article/details/105170894
在maven pom.xml文件中增加如下配置:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<goals>
<!-- 该插件有很多种执行目标,比如 license-list 是用来列出可用的 license,add-third-party 用来检测单个项目中的 license -->
<!-- aggregate-add-third-party 表示检测多模块项目的 license -->
<goal>aggregate-add-third-party</goal>
</goals>
</execution>
</executions>
<!-- 设置配置,以下配置都是非必须的 -->
<configuration>
<!-- 设置检测结果文件的存放目录 -->
<outputDirectory>${basedir}/target/generated-sources/license</outputDirectory>
<!-- 设置检测结果文件,默认是 txt 文件 -->
<thirdPartyFilename>all_license.xls</thirdPartyFilename>
<!-- 设置检测结果文件的样式,默认的样式会生成 txt 文件 -->
<fileTemplate>${basedir}/src/license/excel_export.ftl</fileTemplate>
<!-- mvn 命令会从不同的 repository 中下载 license 文件,经常会出现请求超时的情况,默认超时时间是 5 秒,将其调整为 10 秒 -->
<connectionRequestTimeout>10000</connectionRequestTimeout>
<connectTimeout>10000</connectTimeout>
<socketTimeout>10000</socketTimeout>
<!-- 有些依赖可能检测不到 license、mvn 检测出来的 license 有误,或者你需要对某些模块的 license 自己指定,将该配置设置为 true -->
<!-- 默认配置在 ${basedir}/src/license/THIRD-PARTY.properties 文件中,稍后进行解释 -->
<useMissingFile>true</useMissingFile>
<!-- 排除项目中某些 groupId,不对其进行检测,比如自己写的模块,是没必要进行检测的 -->
<excludedGroups>io\.leo\.test</excludedGroups>
<!-- 由于不同依赖中使用的 license 名称不同,为了便于自己分类管理,可以将 license 进行合并,比如以下将 Apache 系列的 license 都合并为 The Apache Software License -->
<licenseMerges>
<!-- 第一个为合并之后的 license 名称,之后每个 license 以 | 分隔 -->
<licenseMerge>
The Apache Software License
|Apache
|ASF 2.0
|Apache v2
|The Apache Software License, Version 2.0
</licenseMerge>
<licenseMerge>
BSD License
|BSD
|BSD-2-Clause
|The BSD 3-Clause License
</licenseMerge>
</licenseMerges>
<!-- failIfWarning 配合这两个属性使用 <includedLicenses> <excludedLicenses> -->
<!-- <includedLicenses> 表示白名单,license 一定要在这个列表中 -->
<!-- <excludedLicenses> 表示黑名单,这个列表中的 license 一定不能出现 -->
<!-- failIfWarning 设置为 true,表示 license 不在白名单中、或 license 在黑名单中,将会构建失败。结合这个功能,非常方便看出项目中是否有非法 license -->
<failIfWarning>true</failIfWarning>
</configuration>
</plugin>
</plugins>
</build>
excel_export.ftl:导出excel格式,同时需要将thirdPartyFilename后缀改为.xls
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>xiajiafu</Author>
<LastAuthor>xiajiafu</LastAuthor>
<Created>2015-06-05T18:19:34Z</Created>
<LastSaved>2023-04-07T05:40:20Z</LastSaved>
<Version>16.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>15720</WindowHeight>
<WindowWidth>29040</WindowWidth>
<WindowTopX>32767</WindowTopX>
<WindowTopY>32767</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="等线" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s63" ss:Name="超链接">
<Font ss:FontName="等线" x:CharSet="134" ss:Size="11" ss:Color="#0563C1"
ss:Underline="Single"/>
</Style>
<Style ss:ID="s16">
<Font ss:FontName="等线" x:CharSet="134" ss:Size="14" ss:Color="#000000"/>
</Style>
<Style ss:ID="s66" ss:Parent="s63">
<Font ss:FontName="等线" x:CharSet="134" ss:Size="14" ss:Color="#0563C1"
ss:Underline="Single"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="500" x:FullColumns="1"
x:FullRows="1" ss:StyleID="s16" ss:DefaultColumnWidth="54"
ss:DefaultRowHeight="18">
<Column ss:StyleID="s16" ss:AutoFitWidth="0" ss:Width="231.75"/>
<Column ss:StyleID="s16" ss:AutoFitWidth="0" ss:Width="168"/>
<Column ss:StyleID="s16" ss:AutoFitWidth="0" ss:Width="429.75"/>
<Column ss:StyleID="s16" ss:AutoFitWidth="0" ss:Width="143.25"/>
<Row>
<Cell><Data ss:Type="String">Dependency</Data></Cell>
<Cell><Data ss:Type="String">JAR</Data></Cell>
<Cell><Data ss:Type="String">License name</Data></Cell>
<Cell><Data ss:Type="String">License Agreement Url</Data></Cell>
</Row>
<#list dependencyMap as e>
<#assign project = e.getKey()/>
<#assign licenses = e.getValue()/>
<#if !licenses?seq_contains('Unknown license')>
<Row>
<Cell><Data ss:Type="String">${project.groupId}:${project.artifactId}:${project.version}</Data></Cell>
<Cell><Data ss:Type="String">${project.artifactId}-${project.version}.jar</Data></Cell>
<Cell><Data ss:Type="String"><#list licenses as license>${license}<#if (license_has_next)>, </#if></#list></Data></Cell>
<Cell ss:StyleID="s66" ss:HRef="${project.url}"><Data ss:Type="String">License Agreement</Data></Cell>
</Row>
</#if>
</#list>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>3</ActiveRow>
<ActiveCol>3</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
html_export.ftl:导出html格式,同时将thirdPartyFilename后缀改为.html
<#--获取证书分类-->
<#function licenseFormat licenses>
<#assign result><#list licenses as license>${license}<#if (license_has_next)>, </#if></#list></#assign>
<#return result>
</#function>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
table {
width: 100%;
border-collapse: collapse;
}
table caption {
font-size: 2em;
font-weight: bold;
margin: 1em 0;
}
th, td {
border: 1px solid #999;
text-align: left;
padding: 20px 0;
}
table thead tr {
background-color: #008c8c;
color: #fff;
}
table tbody tr:nth-child(odd) {
background-color: #eee;
}
table tbody tr:hover {
background-color: #ccc;
}
table tbody tr td:first-child {
color: #f40;
}
table tfoot tr td {
text-align: right;
padding-right: 20px;
}
</style>
</head>
<body style="text-align: center;">
<table cellspacing="0" cellpadding="0">
<thead>
<tr>
<td>Dependency</td>
<td>Jar</td>
<td>License name</td>
<td>License Agreement Url</td>
</tr>
</thead>
<tbody>
<#list dependencyMap as e>
<#assign project = e.getKey()/>
<#assign licenses = e.getValue()/>
<#if !licenses?seq_contains('Unknown license')>
<tr>
<td>${project.groupId}:${project.artifactId}:${project.version}</td>
<td>${project.artifactId}-${project.version}.jar</td>
<td>${licenseFormat(licenses)}</td>
<td><a href="${project.url}">License Agreement</a></td>
</tr>
</#if>
</#list>
</tbody>
</table>
</body>
</html>
网友评论