近期接到个任务,组内有个项目计划开源,让我负责生成license和notice文件,看了网上别的开源项目的,最终采用org.codehaus.mojo.license-maven-plugin和org.jasig.maven.maven-notice-plugin这两个插件,pom配置如下:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<!-- update header -->
<inceptionYear>2019</inceptionYear>
<licenseName>apache_v2</licenseName>
<organizationName>${organizationName}</organizationName>
<projectName>${projectName}</projectName>
<processStartTag> </processStartTag>
<processEndTag> </processEndTag>
<sectionDelimiter> </sectionDelimiter>
<addJavaLicenseAfterPackage>false</addJavaLicenseAfterPackage>
<emptyLineAfterHeader>true</emptyLineAfterHeader>
<outputDirectory>${project.basedir}</outputDirectory>
<useMissingFile>true</useMissingFile>
<thirdPartyFilename>LICENSE-3RD-PARTY</thirdPartyFilename>
<executeOnlyOnRootModule>true</executeOnlyOnRootModule>
<missingFile>${project.basedir}/LICENSE-3RD-PART.propertie</missingFile>
<roots>
<root>src/main/java</root>
<root>src/test/java</root>
</roots>
<includes>
<include>**/*.java</include>
<include>**/*.xml</include>
<include>**/*.sh</include>
<include>**/*.properties</include>
<include>**/*.sql</include>
<include>**/*.html</include>
<include>**/*.less</include>
<include>**/*.css</include>
<include>**/*.js</include>
<include>**/*.json</include>
</includes>
<!-- license:aggregate-download-licenses -->
<licensesOutputFile>${project.basedir}/lic/licenses.xml</licensesOutputFile>
<licensesOutputDirectory>${project.basedir}/lic/licenses/</licensesOutputDirectory>
<licensesErrorsFile>${project.basedir}/lic/licenses-errors.xml</licensesErrorsFile>
<!-- config for mvn license:update-project-license -->
<licenseFile>${project.basedir}/LICENSE</licenseFile>
<licenseMerges>
<licenseMerge>Apache 2.0|ASL, version
2|http://www.apache.org/licenses/LICENSE-2.0.txt|http://asm.ow2.org/license.html|The
Apache License, Version 2.0|Apache License|Apache License Version 2|Apache License
Version 2.0|Apache Software License - Version 2.0|Apache 2.0 License|Apache License
2.0|ASL|Apache 2|Apache-2.0|the Apache License, ASL Version 2.0|The Apache Software
License, Version 2.0|Apache License, Version 2.0|Apache Public License 2.0
</licenseMerge>
<licenseMerge>BSD|The BSD 3-Clause License|The BSD License|Modified BSD License|New BSD
License|New BSD license|Two-clause BSD-style license|BSD licence|BSD New|The New BSD
License|BSD 3-Clause|BSD 3-clause
</licenseMerge>
<licenseMerge>MIT|MIT License|The MIT License</licenseMerge>
<licenseMerge>LGPL|LGPL, version 2.1|GNU Library or Lesser General Public License (LGPL)
V2.1|GNU Lesser General Public License (LGPL), Version 2.1|GNU Lesser General Public
License, Version 2.1|LGPL 2.1
</licenseMerge>
<licenseMerge>CDDL|CDDL+GPL|CDDL+GPL License|CDDL + GPLv2 with classpath exception|CDDL
License|CDDL 1.0|CDDL 1.1|COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version
1.0|Common Development and Distribution License (CDDL) v1.0
</licenseMerge>
<licenseMerge>EPL|Eclipse Public License - Version 1.0</licenseMerge>
<licenseMerge>GPL|GPL2 w/ CPE|GPLv2+CE|GNU General Public Library</licenseMerge>
<licenseMerge>MPL|MPL 1.1</licenseMerge>
<licenseMerge>Public Domain</licenseMerge>
<licenseMerge>Common Public License|Common Public License Version 1.0</licenseMerge>
<licenseMerge>CC0|CC0 1.0 Universal|Public Domain, per Creative Commons CC0</licenseMerge>
<licenseMerge>Unknown License|Unknown license</licenseMerge>
</licenseMerges>
</configuration>
</plugin>
<plugin>
<groupId>org.jasig.maven</groupId>
<artifactId>maven-notice-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<generateChildNotices>false</generateChildNotices>
<noticeTemplate>https://source.jasig.org/licenses/NOTICE.template</noticeTemplate>
<licenseMapping>
<param>${project.basedir}/lic/license-mappings.xml</param>
<param>https://source.jasig.org/licenses/license-mappings.xml</param>
</licenseMapping>
</configuration>
</plugin>
需要将上述内容中的${organizationName}和${projectName}替换成你的真实组织和项目名字。
生成notice文件的时候由于部分jar包在maven仓库中没有,项目内是通过system引入的,需要单独配置下这些找不到license依赖的license,例如我的license-mappings.xml配置文件:
<license-lookup xmlns="https://source.jasig.org/schemas/maven-notice-plugin/license-lookup"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://source.jasig.org/schemas/maven-notice-plugin/license-lookup https://source.jasig.org/schemas/maven-notice-plugin/license-lookup/license-lookup-v1.0.xsd">
<artifact>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<license>Apache License, Version 2.0</license>
</artifact>
<artifact>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<license>Apache License, Version 2.0</license>
</artifact>
<artifact>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-auth</artifactId>
<license>Apache License, Version 2.0</license>
</artifact>
<artifact>
<groupId>com.robert.vesta</groupId>
<artifactId>vesta-service</artifactId>
<license>Apache License, Version 2.0</license>
</artifact>
<artifact>
<groupId>com.robert.vesta</groupId>
<artifactId>vesta-intf</artifactId>
<license>Apache License, Version 2.0</license>
</artifact>
</license-lookup>
之后通过下列命令即可将对应的license/notice以及代码中的文件头注释一起生成
mvn license:update-file-header
mvn license:aggregate-add-third-party
mvn license:update-project-license
mvn license:aggregate-download-licenses
mvn notice:generate
最终效果就是下述图片所示: