使用license-maven-plugin為項(xiàng)目源文件頂部添加許可證

前言

??在開源或公司中開發(fā)項(xiàng)目時(shí),通常需要在源文件頂部添加許可證以保護(hù)版權(quán)。在自己從業(yè)到現(xiàn)在,幾家公司、見過太多的項(xiàng)目,但都出乎我意料,對版權(quán)都并不是很在意,甚至干了很久的老前輩們也不曾清楚這個(gè)東西,自己在代碼上可是杠精,在意各種小細(xì)節(jié)。今天就來聊聊如何給代碼添加版權(quán)。
??如果說第一次知道版權(quán)是從什么時(shí)候,那應(yīng)該是我剛工作的時(shí)候,非常喜歡去找別人封裝的框架,因?yàn)槲抑牢叶軓闹袑W(xué)到什么,果然我發(fā)現(xiàn)別人的代碼上面有版權(quán)聲明,隨即便開始搜索研究這個(gè)東西,然后給公司的項(xiàng)目代碼也添加上了代碼版權(quán)。當(dāng)時(shí)添加用的是IDEA開發(fā)工具進(jìn)行配置后全局更新添加的,IDEA Copyright 大家想了解可以去點(diǎn)擊鏈接看官方幫助手冊了解。但這種方式有著局限,那就是公司里面不光用使用IDEA的同學(xué),也有eclipse的同學(xué)呀,如果人很多,你需要幫助他們配置或者讓他們學(xué)習(xí)后自己配置,還是比較耗費(fèi)時(shí)間和精力的,那有沒有一種更好的方式呢?答案是肯定的,license-maven-plugin它來了??梢宰屇銦o視開發(fā)工具,通過maven命令直接生成,真是爽歪歪啊。
??說了不少,那我又是怎么知道這個(gè)插件的呢?答案還是看源碼,周五的時(shí)候看螞蟻金服sofa框架,無意中便發(fā)現(xiàn)了這個(gè)小東西,隨即了解了一下,便用上了,真是好用來,便放棄了IDEA配置copyright方式,轉(zhuǎn)而是用license-maven-plugin。

官網(wǎng)

http://mycila.mathieu.photography/license-maven-plugin/

特征

check:檢查某些源文件中是否缺少標(biāo)題
format:如果缺少則添加標(biāo)題
remove:可以刪除現(xiàn)有的標(biāo)題
update:用一個(gè)新的頭更新現(xiàn)有的頭
custom mappings:可輕松支持新文件擴(kuò)展名
variable replacement:您可以在標(biāo)頭中添加一些變量,例如${year},${owner},它們將被取自pom或系統(tǒng)屬性的相應(yīng)值代替。

配置

HEADER文件

在項(xiàng)目下添加HEADER文件,license-maven-plugin隨附以下許可證模板:

  • AGPL3
  • APACHE2
  • BSD 2,3,4
  • LGPL3
  • MirOS
  • MIT
  • MPL2
  • WTFPL
  • UNLICENSE
    可以到此找到帶有預(yù)配置占位符的許可證模板
    這里我提供一份適合公司使用的許可證模板,大家改改給自己項(xiàng)目用上去吧。
**********************************************************************
Copyright (c) ${project.inceptionYear} .
All rights reserved.
項(xiàng)目名稱:${project.name} 
項(xiàng)目描述:${project.description}
版權(quán)說明:本軟件屬XXX有限公司所有,在未獲得XXX有限公司正式授權(quán)情況下,任何企業(yè)和個(gè)人,不能獲取、閱讀、安裝、傳播本軟件涉及的任何受知識(shí)產(chǎn)權(quán)保護(hù)的內(nèi)容。
***********************************************************************

在上面可以看到${}占位符,下面講述一下屬性和占位符??梢杂米髡嘉环膶傩詠碜裕?/p>

  • 環(huán)境變量
  • POM屬性
    • project.groupId
    • project.artifactId
    • project.version
    • project.name
    • project.description
    • project.inceptionYear
    • project.url
  • 文檔屬性
    • file.name
  • 插件配置屬性(來自<properties>標(biāo)記)
  • 系統(tǒng)屬性

聲明

<properties>
    <!--main.user.dir-->
   <main.user.dir>${user.dir}</main.user.dir>  
</properties>
<plugin>
     <groupId>com.mycila</groupId>
     <artifactId>license-maven-plugin</artifactId>
     <version>3.0</version>
     <executions>
         <execution>
             <phase>generate-sources</phase>
             <goals>
                 <goal>remove</goal>
                 <goal>format</goal>
             </goals>
         </execution>
     </executions>
     <configuration>
         <quiet>true</quiet>
         <!--HEADER文件-->
         <header>${main.user.dir}/HEADER</header>
         <!--排除文件-->
         <excludes>
             <exclude>**/*.properties</exclude>
             <exclude>*.sh</exclude>
             <exclude>*.yml</exclude>
             <exclude>.editorconfig</exclude>
             <exclude>.gitignore</exclude>
             <exclude>**/*.md</exclude>
             <exclude>**/*.xml</exclude>
         </excludes>
         <!--嚴(yán)格檢查-->
         <strictCheck>true</strictCheck>
         <mapping>
             <java>SLASHSTAR_STYLE</java>
         </mapping>
     </configuration>
 </plugin>

命令

  • mvn license:check:驗(yàn)證那些文件是否缺少許可證標(biāo)題。如果在上述pom.xml中聲明了此goals,則該goals將附加到驗(yàn)證階段。
  • mvn license:format:缺少時(shí)添加許可證標(biāo)題。如果許可證標(biāo)題存在,則將其更新為新許可證標(biāo)題。
  • mvn license:remove:刪除現(xiàn)有的許可證標(biāo)題。

使用

現(xiàn)在我們可以執(zhí)行package命令試試,完成以后看下源文件標(biāo)題是不是已經(jīng)添加上版權(quán)聲明了。

進(jìn)階

注釋類型

我們可以為不同的類型的文件設(shè)置不同類型的注釋類型

  • JAVAPKG_STYLE (like Javadoc, but only for files that are in a Java package, skips the first line): not assigned to a file extension by default (see Java packages below for how to enable it)
    package com.example;
    /*-
     * My comment
     */
  • XML_STYLE (XML-like comments): *.pom, *.xml, *.xhtml, *.mxml, *.dtd, *.xsd, *.jspx, *.fml, *.xsl, *.html, *.htm, *.kml, *.gsp, *.tld
    <!--
        My comment
    -->
  • XML_PER_LINE (alternate XML-like comments)
    <!-- My first comment  -->
    <!-- My second comment -->

(automatically right-adjusts the closing comment)

  • DOUBLETILDE_STYLE (APT-like comments): *.apt
    ~~ My comment
  • SCRIPT_STYLE (Property file or shell comments): *.properties, *.sh, *.py, *.rb, *.pl, *.pm, *.yml, *.yaml
    # My comment
  • HAML_STYLE: *.haml, *.scaml
    -# My comment
  • BATCH (Windows batch comments): *.bat, *.cmd
    @REM My comment
  • TEXT (Text like comments): *.txt
    ====
        My comment
    ====

(4 spaces, then the lines of the header)

  • DOUBLEDASHES_STYLE (Sql like comments): *.sql, *.adb, *.ads, *.e
    --
    -- test comment
    --
  • DYNASCRIPT_STYLE (JSP like comments): *.jsp
    <%--
        comment
    --%>
  • FTL (FreeMarker like comments): *.ftl
    <#--
        comment
    -->
  • FTL_ALT (FreeMarker Alternative Syntax comments)
    [#ftl ...]
    [#--
        comment
    --]
  • SHARPSTAR_STYLE (Velocity templates comments): *.vm
    #*
        comment
    *#
  • SEMICOLON_STYLE (Assembler like comments): *.asm
    ;
    ; comment
    ;
  • BRACESSTAR_STYLE (Delphi like comments): *.pas
    {*
     * comment
     *}
  • APOSTROPHE_STYLE (VisualBasic like comments): *.bas
    '
    ' comment
    '
  • EXCLAMATION_STYLE (Fortran like comments): *.f
    !
    ! comment
    !
  • SLASHSTAR_STYLE (JavaScript like comments): *.js
    /*
     * comment
     */
  • DYNASCRIPT3_STYLE (Coldfusion like comments): *.cfc, *.cfm
    <!---
        comment
    --->
  • PERCENT_STYLE (Teχ like comments): *.cls, *.sty, *.tex
    % comment
  • PERCENT3_STYLE (Erlang like comments): *.erl, *.hrl
    %%%
    %%% comment
    %%%
  • EXCLAMATION3_STYLE (Lisp like comments): *.el
    !!!
    !!! comment
    !!!
  • LUA (Lua like comments): *.lua
    --[[
    comment
    ]]
  • ASP (Asp like comments): *.asp
    <%
    ' comment
    %>
  • PHP (PHP comments): *.php
    /*
     * comment
     */

(inserted after the <?php> tag)

  • DOUBLESLASH_STYLE (often used comments style)
    //
    // comment
    //

自定義映射

該插件使您可以添加所需的任何其他映射。在.xml、.application文件中添加許可證標(biāo)頭。由于這些文件是xml文件,因此您只需要在項(xiàng)目pom中添加license-maven-plugin的以下映射:

 <mapping>
     <application>XML_STYLE</application>
     <xml>XML_STYLE</xml>
 </mapping>

有關(guān)更加詳細(xì)的配置請查看官方文檔

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容