建置 Eclipse Maven 專案 - Spring Boot 168 EP 4

建置 Eclipse Maven 專案 – Spring Boot 168 EP 4

能夠快速產出一些基本的類別與單元測試,生成 pom.xml 專案設置檔,Maven Eclipse 導入單元測試 JUnit, EP 4 用此方式建置,節省專案管理設定上的時間,提升開發效率。

功能簡介

Maven 是一個專案管理及自動構建工具,採用 XML 格式設置,一個依賴管理系統,和用來運行定義在生命周期階段中插件目標和邏輯。

專案建置

建置 Eclipse Maven 專案,點選選單,File -> New -> Other -> Maven Project

Spring Boot 168 EP 4 建置 Eclipse Maven 專案

選擇類型

選擇 maven-archetype-quickstart

Spring Boot 168 EP 4 建置 Eclipse Maven 專案 選擇 maven-archetype-quickstart

輸入資訊

輸入 groupId、artifactId

Spring Boot 168 EP 4 建置 Eclipse Maven 專案 輸入 groupId、artifactId

完成建置

這樣就快速建置了第一個 Maven 專案, Maven in Eclipse 都已經完成設置,在專案上點右鍵最下方 Properties,可以查看到更多相關資訊。

Spring Boot 168 EP 4 建置 Eclipse Maven 專案 Maven 專案屬性

組態設定

Maven Eclipse 使用插件及增加相依套件。

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.ruoxue</groupId>
  <artifactId>spring-boot-168-maven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring-boot-168-maven</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

單元測試

Eclipse with Maven 預設已經產生了一個類別 App 及一個單元測試 AppTest,在測試方法上點右鍵執行 Run As -> JUnit Test,查看結果,就會發現已經通過測試。

Spring Boot 168 EP 4 建置 Eclipse Maven 專案 執行單元測試
Spring Boot 168 EP 4 建置 Eclipse Maven 專案 單元測試結果

心得分享

Maven with Eclipse 專案管理,並沒有侷限在 Maven 上,有些人採用 Gradle,也有些人繼續用 Ant,也有只用腳本檔進行編譯、打包、佈署等相關作業,隨著專案開發項目越來越多,相依賴的套件不斷地增加,造成衝突的狀況也會越來越多,選擇合適的自動化工具,將有助於更專心在開發項目上。

發佈留言