目錄
- 1 創建專案目錄
- 2 撰寫主代碼
- 2.1 在根目錄創建pom.xml 檔案
- 2.2 撰寫主代碼類
- 2.3 編譯maven專案
- 3 撰寫測驗代碼
- 3.1 更新pom.xml 檔案
- 3.2 撰寫測驗類
- 3.3 使用mvn運行測驗類
- 4 打包專案
- 4.1 使用mvn打包專案
- 4.2 jar包安裝到maven倉庫
- 5 使用archetype 生成專案目錄
- 6 總結一些要點
1 創建專案目錄
為什么要練習這個?
主要是想熟悉一下maven 的工程結構,本身也不是做Java開發的,能從代碼里把邏輯扒出來就行 --!
這個練習參考《Maven實戰》 許曉斌 著,
創建一個learnmvn目錄,用來存放不同階段的專案目錄,
然后創建hello-mvn目錄,這是專案目錄,
目錄結構
learnmvn/hello-mvn
2 撰寫主代碼
2.1 在根目錄創建pom.xml 檔案
檔案路徑 hello-mvn/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.juvenxu.mvnbook</groupId>
<artifactId>hello-mvn</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Hello World Project</name>
</project>
必備的配置項
- modelVersion
- groupId
- artifactId
- version
- name
2.2 撰寫主代碼類
默認情況下,按照maven約定,專案的主代碼應該放到 src/main/java 目錄下,專案的測驗代碼應該放到src/test/java 目錄下,
- 要創建主代碼,則需要創建src/main/java 目錄,
- 然后創建包目錄 src/main/java/com/juvenxu/mvnbook/helloworld/
- 在包目錄下創建HelloMvn.java
package com.juvenxu.mvnbook.helloworld;
public class HelloMvn{
public String sayHello()
{
return "Hello Maven";
}
public static void main(String[] args)
{
System.out.println( new HelloMvn().sayHello());
}
}
2.3 編譯maven專案
在專案根目錄下打開cmd , 運行 mvn clean compile
F:\learnmvn\hello-mvn>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.juvenxu.mvnbook:hello-mvn >--------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-mvn ---
[INFO] Deleting F:\learnmvn\hello-mvn\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-mvn ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\learnmvn\hello-mvn\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-mvn ---
[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 F:\learnmvn\hello-mvn\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.919 s
[INFO] Finished at: 2020-12-30T10:03:39+08:00
[INFO] ------------------------------------------------------------------------
3 撰寫測驗代碼
單元測驗的三個步驟:
- 準備測驗類及資料
- 執行要測驗的行為
- 檢查結果
3.1 更新pom.xml 檔案
<?xml version="1.0" encoding="UTF-8"?>
<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.juvenxu.mvnbook</groupId>
<artifactId>hello-mvn</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Hello World Project</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- scope 表示此段依賴的范圍,如果寫的是test , 則只能在src/test/java 中的類參考這個依賴,主代碼參考會編譯錯誤,此標簽不指定,則默認值是compile,表示對主類和測驗類都生效,
3.2 撰寫測驗類
按照maven約定,專案的測驗代碼應該放到src/test/java 目錄下,
- 創建src/test/java 目錄
- 創建包目錄 src/test/java/com/juvenxu/mvnbook/helloworld
- 在包目錄下創建HelloWorldTest.java
package com.juvenxu.mvnbook.helloworld;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HelloWorldTest
{
@Test
public void testSayHello()
{
HelloMvn hellomvn = new HelloMvn();
String result = hellomvn.sayHello();
assertEquals("Hello Maven", result);
}
}
斷言的作用是,判斷result中的值是不是 "Hello Maven"
3.3 使用mvn運行測驗類
在專案根目錄下打開cmd , 運行 mvn clean test
從下面的輸出能看出測驗成功了,
F:\learnmvn\hello-mvn>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.juvenxu.mvnbook:hello-mvn >--------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading ...
Downloaded ...
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-mvn ---
[INFO] Deleting F:\learnmvn\hello-mvn\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-mvn ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\learnmvn\hello-mvn\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-mvn ---
[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 F:\learnmvn\hello-mvn\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-mvn ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\learnmvn\hello-mvn\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-mvn ---
[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 F:\learnmvn\hello-mvn\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-mvn ---
[INFO] Surefire report directory: F:\learnmvn\hello-mvn\target\surefire-reports
Downloading ...
Downloaded ...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.429 s
[INFO] Finished at: 2020-12-30T10:21:48+08:00
[INFO] ------------------------------------------------------------------------
F:\learnmvn\hello-mvn>
4 打包專案
執行編譯、測驗之后,下一個步驟是打包,
4.1 使用mvn打包專案
在專案根目錄下打開cmd , 運行 mvn clean test
可以發現mvn會依次執行 clean,resources,compile,testResources,testCompile,test,jar
- 打包的包名是 artifact-version.jar ,如果有需要可以使用finalName來定義檔案名稱
- 打包檔案會存放到target目錄下
F:\learnmvn\hello-mvn>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.juvenxu.mvnbook:hello-mvn >--------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-mvn ---
[INFO] Deleting F:\learnmvn\hello-mvn\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-mvn ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\learnmvn\hello-mvn\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-mvn ---
[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 F:\learnmvn\hello-mvn\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-mvn ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\learnmvn\hello-mvn\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-mvn ---
[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 F:\learnmvn\hello-mvn\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-mvn ---
[INFO] Surefire report directory: F:\learnmvn\hello-mvn\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.033 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-mvn ---
[INFO] Building jar: F:\learnmvn\hello-mvn\target\hello-mvn-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.266 s
[INFO] Finished at: 2020-12-30T10:49:10+08:00
[INFO] ------------------------------------------------------------------------
4.2 jar包安裝到maven倉庫
對于maven專案而言,只有jar包放到maven倉庫才會被構建,所以自己寫的專案需要放到maven倉庫才可以被其他專案所參考,
專案根目錄下打開cmd,運行 mvn clean install
F:\learnmvn\hello-mvn>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.juvenxu.mvnbook:hello-mvn >--------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-mvn ---
[INFO] Deleting F:\learnmvn\hello-mvn\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-mvn ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\learnmvn\hello-mvn\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-mvn ---
[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 F:\learnmvn\hello-mvn\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-mvn ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\learnmvn\hello-mvn\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-mvn ---
[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 F:\learnmvn\hello-mvn\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-mvn ---
[INFO] Surefire report directory: F:\learnmvn\hello-mvn\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-mvn ---
[INFO] Building jar: F:\learnmvn\hello-mvn\target\hello-mvn-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-mvn ---
[INFO] Installing F:\learnmvn\hello-mvn\target\hello-mvn-1.0-SNAPSHOT.jar to d:\mavenreopsitory\com\juvenxu\mvnbook\hello-mvn\1.0-SNAPSHOT\hello-mvn-1.0-SNAPSHOT.jar
[INFO] Installing F:\learnmvn\hello-mvn\pom.xml to d:\mavenreopsitory\com\juvenxu\mvnbook\hello-mvn\1.0-SNAPSHOT\hello-mvn-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.223 s
[INFO] Finished at: 2020-12-30T10:57:36+08:00
[INFO] ------------------------------------------------------------------------
5 使用archetype 生成專案目錄
F:\learnmvn\hello-mvnarche>mvn archetype:generate
[INFO] Scanning for projects...
Downloading ...
Downloaded ...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site.
This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR-268 Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype which contains a sample Maven site which demonstrates
some of the supported document types like APT, XDoc, and FML and demonstrates how
to i18n your site. This archetype can be layered upon an existing Maven project.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An archetype which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7: 注釋:默認就是7 ,可以直接回車
Downloading ...
Downloaded ...
Define value for property 'groupId': com.juvenxu.mvnbook 注釋:填寫 groupId
Define value for property 'artifactId': hello-world 注釋:填寫artifactId
Define value for property 'version' 1.0-SNAPSHOT: : 注釋:默認
Define value for property 'package' com.juvenxu.mvnbook: : com.juvenxu.mvnbook.helloworld 注釋:填寫包名稱
Confirm properties configuration:
groupId: com.juvenxu.mvnbook
artifactId: hello-world
version: 1.0-SNAPSHOT
package: com.juvenxu.mvnbook.helloworld
Y: : y 注釋:y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: F:\learnmvn\hello-mvnarche
[INFO] Parameter: package, Value: com.juvenxu.mvnbook.helloworld
[INFO] Parameter: groupId, Value: com.juvenxu.mvnbook
[INFO] Parameter: artifactId, Value: hello-world
[INFO] Parameter: packageName, Value: com.juvenxu.mvnbook.helloworld
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: F:\learnmvn\hello-mvnarche\hello-world
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:17 min
[INFO] Finished at: 2020-12-30T12:30:52+08:00
[INFO] ------------------------------------------------------------------------
6 總結一些要點
- 根目錄存放pom.xml檔案
- src 下的目錄需要手工創建, target 下的目錄通過mvn自動創建
- 主代碼存放目錄 src/main/java/
- 測驗代碼存放目錄 src/test/java/
- 編譯之后的主類存放目錄 target/classes
- 編譯之后的測驗類存放目錄 target/test-classes
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/243108.html
標籤:Java
上一篇:Java向上轉型和向下轉型
下一篇:分庫分表就能無限擴容嗎?
