如何編寫一個Maven外掛程式

來源:互聯網
上載者:User

標籤:des   style   http   java   os   io   

如何編寫一個Maven外掛程式

本文基於eclipse ee.

  • 建立一個maven外掛程式工程:

    

  • 主要就是讓項目能依賴maven-plugin-api:

    

  • 編寫一個Mojo類:
/** * 用於輸出訊息的Mojo * @goal hello */public class HelloMojo extends AbstractMojo {/** * @parameter expression="${echo.message}" default-value="Hello Maven World..." */private Object message;public void execute() throws MojoExecutionException {getLog().info(message.toString());}}
  • 安裝到本地 mvn install
  • 運行外掛程式:

    

    

  • 但是對於上面我們運行外掛程式時要寫這麼多<groupId>:<artifactId>:<version>:goal是一件比較痛苦的事,我們需要配置一個首碼來運行我們的外掛程式,比如hello:hello即表示運行hello-mave-plugin的hello的目標:

      配置M2_HOME/settings.xml添加外掛程式組:

<pluginGroups>    <!--  外掛程式的groupId -->    <pluginGroup>org.idestiny.learn.maven</pluginGroup></pluginGroups>
      於是你可以通過首碼來運行外掛程式目標,那麼mvn預設會以{prefix}-maven-plugin或maven-{prefix}-plugin去找對應的外掛程式,在這裡prefix即為hello:

     

那麼如何在我們install我們的外掛程式時就指定其首碼,而不是像上面這樣,我們需要在build外掛程式時,加上一個外掛程式即可,加入配置到pom.xml:  

<build>    <plugins><!-- 配置本外掛程式的首碼, 則可以通過 abcd:hello 運行本外掛程式目標hello --><plugin>    <artifactId>maven-plugin-plugin</artifactId>    <version>2.3</version>    <configuration> <goalPrefix>abcd</goalPrefix>    </configuration></plugin>    </plugins></build>
  • mvn install安裝外掛程式,運行外掛程式abcd:hello:

    

具體其他一些api細節可以參考:

http://maven.apache.org/developers/mojo-api-specification.html

不吝指正。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.