使用的插件:license-maven-plugin。執(zhí)行命令: 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>
<!-- 該插件有很多種執(zhí)行目標(biāo),比如 license-list 是用來列出可用的 license,add-third-party 用來檢測單個項目中的 license -->
<!-- aggregate-add-third-party 表示檢測多模塊項目的 license -->
<goal>aggregate-add-third-party</goal>
</goals>
</execution>
</executions>
<!-- 設(shè)置配置,以下配置都是非必須的 -->
<configuration>
<!-- 設(shè)置檢測結(jié)果文件的存放目錄 -->
<outputDirectory>${basedir}/target/generated-sources/license</outputDirectory>
<!-- 設(shè)置檢測結(jié)果文件,默認(rèn)是 txt 文件 -->
<thirdPartyFilename>all_license.xls</thirdPartyFilename>
<!-- 設(shè)置檢測結(jié)果文件的樣式,默認(rèn)的樣式會生成 txt 文件 -->
<fileTemplate>${basedir}/src/license/excel_export.ftl</fileTemplate>
<!-- mvn 命令會從不同的 repository 中下載 license 文件,經(jīng)常會出現(xiàn)請求超時的情況,默認(rèn)超時時間是 5 秒,將其調(diào)整為 10 秒 -->
<connectionRequestTimeout>10000</connectionRequestTimeout>
<connectTimeout>10000</connectTimeout>
<socketTimeout>10000</socketTimeout>
<!-- 有些依賴可能檢測不到 license、mvn 檢測出來的 license 有誤,或者你需要對某些模塊的 license 自己指定,將該配置設(shè)置為 true -->
<!-- 默認(rèn)配置在 ${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 一定不能出現(xiàn) -->
<!-- failIfWarning 設(shè)置為 true,表示 license 不在白名單中、或 license 在黑名單中,將會構(gòu)建失敗。結(jié)合這個功能,非常方便看出項目中是否有非法 license -->
<failIfWarning>true</failIfWarning>
</configuration>
</plugin>
</plugins>
</build>
excel_export.ftl:導(dǎo)出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:導(dǎo)出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>