一、新建工程
1.1 什麼是m2eclipse
m2eclipse就是eclipse內置的maven插件。
1.2 maven約定
maven相比其它項目,約定了一些固定格式的目錄,可以簡化配置。當然,也可以不遵循這些默認約定,但有時候需要修改一些配置項。默認約定如下:
- pom.xml。放在根目錄中。
- 主代碼。放在src/main/java下。
- 測試代碼。放在src/test/java下。
- 輸出目錄。根目錄/target/
1.3 新建工程與Archetype
在構建maven項目時,手動創建各種目錄來遵循默認約定是可行的,但這種重複性質的工作可以被簡化,即使用maven的archetype來快速勾勒項目骨架。
命令行版本(maven 3),運行
mvn archetype:generate
然後選擇archetype,並輸入groupId、artifactId、version、package。
eclipse版本。
IDE自動會使用maven-archetype-plugin插件創建項目。即New一個Maven Project,選擇maven-archetype-quickstart,輸入groupId、artifactId、version、package。
圖文詳見上一篇Maven安裝與入門
除了使用簡單現成的archetype,也可以開發自己的archetype,用自定義版本快速生成項目骨架。這部分屬於進階內容。
二、編寫代碼
2.1 主代碼
隨便寫個hello world程序。例如
/**
* Hello world!
*
*/
public class Hello
{
public String sayHello() {
return "Hello Maven";
}
public static void main( String[] args )
{
System.out.println( new Hello().sayHello() );
}
}
2.2 測試代碼
也是隨便寫一個,不過用了Junit,需要先在pom.xml中聲明依賴junit。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
注:<scope>
默認值是compile,表示對主代碼和測試代碼都有用。寫爲<scope>test</scope>
表示只用於這個插件使用範圍是test下的java文件。主代碼下import JUnit會在編譯時出錯。
測試代碼,例如
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class HelloTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public HelloTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( HelloTest.class );
}
/**
* Rigorous Test :-)
*/
public void testApp()
{
Hello hello = new Hello();
String result = hello.sayHello();
assertEquals( "Hello Maven", result );
}
}
三、編譯
3.1 命令行方式
3.1.1 主代碼
在項目根目錄下運行
mvn clean compile
clean表示清理輸出目錄target/,compile表示編譯項目主代碼。默認情況下,maven構建都所有輸出都在target/目錄下。
順序爲:
- 執行clean。刪除輸出目錄target下的內容;
- 執行resources。目前沒有定義項目資源。
- 執行compile。將項目主代碼編譯到輸出目錄target/classes目錄
這樣,在maven.hello\target\classes\項目名\包名下就出現了Hello.class文件
3.1.2 測試代碼
類似的,運行mvn clean test
3.2 Eclipse方式
如下圖所示,maven給出了幾個常用的選擇,例如maven clean等。如果不在這幾個右鍵菜單項的命令,可以:
- 項目上右鍵,選擇maven build
- 輸入要執行的maven命令,例如命令行中我們輸入mvn clean compile,那ide中就是輸入clean compile。
- 測試代碼則爲clean test。
注意到,這裏有兩個maven build。如果輸入過一遍命令,在第一個中可以選擇以前輸入過的,不需要再輸入一遍。
最終效果:
[INFO] Scanning for projects…
[INFO]
[INFO] --------------------< com.learning.lyl:maven.hello >--------------------
[INFO] Building maven.hello 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] — maven-clean-plugin:2.5:clean (default-clean) @ maven.hello —
[INFO] Deleting G:\mission\JavaLearning\maven.hello\target
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ maven.hello —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory G:\mission\JavaLearning\maven.hello\src\main\resources
[INFO]
[INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ maven.hello —
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to G:\mission\JavaLearning\maven.hello\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.443 s
[INFO] Finished at: 2019-09-15T16:40:39+08:00
[INFO] ------------------------------------------------------------------------
3.3 常見Error
一個常見error是
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] 不再支持源選項 5。請使用 6 或更高版本。
[ERROR] 不再支持目標選項 1.5。請使用 1.6 或更高版本。
這是因爲maven默認編譯的版本是java1.3。可以在<properties>
標籤中配置java的版本和maven編譯版本(properties在根標籤project下一級),取值等於你用的java版本。具體內容如下所示:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
或者在build
下配置也一樣(同樣在project下一級)。具體內容如下所示:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
四、打包
命令行輸入
mvn clean package
或是eclipse的maven build中輸入clean package
結果如下:
[INFO] Building jar: G:\mission\JavaLearning\maven.hello\target\maven.hello-0.0.1-SNAPSHOT.jar