[Maven-不忘初心,方得始終] Maven Project初識

 Maven項目目錄結構:java

src
    -main
        -java
            -package(包名)
    -test
        -java
            -package(包名)
    resources-存放資源文件

 以"Maven項目之HelloWorld"來講明如何純手工構建一個Maven項目. 按照上面給出的目錄結構,來構建第一個Maven項目shell

項目的目錄結構:apache

注: 藍框內的爲包名及類文件.框架

類HelloWorld,HelloWorld.java:maven

package com.thera.maven01.model;

public class HelloWorld {
	public String sayHello() {
		return "HelloWorld!";
	}
}

 測試類HelloWorldTest,HelloWorldTest.java:單元測試

package com.thera.maven01.model;

import org.junit.*;
import org.junit.Assert.*;

public class HelloWorldTest {
	@Test
	public void testHello() {
	    // 引用junit框架,並啓用斷言,爲HelloWorld類中的sayHello()方法提供驗證;
		Assert.assertEquals("HelloWorld!", new HelloWorld().sayHello());
	}
}

注意: 在HelloWorldTest中,使用了junit單元測試框架(重點)測試

到這裏已經完成了maven-01主要代碼編寫.ui

咱們在使用Eclipse或手工編寫一個JavaProject時,對引入.jar包的處理一般都是在項目路徑下新建一個lib目錄,用來存放項目中引入的jar包. 若是是手工編寫,咱們須要在啓動腳本中加入this

java -DLabel="Test" -Djava.ext.dirs=./:./lib com.thera.main.Test &

對lib包的引入後,就能夠使用jar包了.編碼

在Maven中提供了對包的新的管理方式,經過pom.xml配置文件來管理包引用。

pom.xml(存放在src目錄下):

<?xml version="1.0"?>

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.thera.maven01</groupId>
    <artifactId>maven01-model</artifactId>
    <version>0.0.1SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>        
    </dependencies>

</project>

關於Maven的配置文件-pom.xml 詳細瞭解,請到這裏: 


到這裏完成了Maven項目之HelloWorld的所有編碼工做,接下來使用命令編譯及測試

編譯:

D:\Maven-Workspace\maven-01>mvn compile
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.thera.maven01:maven01-model:jar:0.0.1SNAPSHOT
[WARNING] 'version' uses an unsupported snapshot version format, should be '*-SNAPSHOT' instead. @ line 11, column 14
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven01-model 0.0.1SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven01-model ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Maven-Workspace\maven-01\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven01-model ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Maven-Workspace\maven-01\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.579 s
[INFO] Finished at: 2016-02-15T21:57:33+08:00
[INFO] Final Memory: 11M/179M
[INFO] ------------------------------------------------------------------------

D:\Maven-Workspace\maven-01>

測試:

D:\Maven-Workspace\maven-01>mvn test
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.thera.maven01:maven01-model:jar:0.0.1SNAPSHOT
[WARNING] 'version' uses an unsupported snapshot version format, should be '*-SNAPSHOT' instead. @ line 11, column 14
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven01-model 0.0.1SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven01-model ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Maven-Workspace\maven-01\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven01-model ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven01-model ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Maven-Workspace\maven-01\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven01-model ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Maven-Workspace\maven-01\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven01-model ---
[INFO] Surefire report directory: D:\Maven-Workspace\maven-01\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.thera.maven01.model.HelloWorldTest
HelloWorld!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.158 s
[INFO] Finished at: 2016-02-15T22:00:56+08:00
[INFO] Final Memory: 13M/227M
[INFO] ------------------------------------------------------------------------

D:\Maven-Workspace\maven-01>

編譯執行後目錄結構:

相關文章
相關標籤/搜索