日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

maven(3)maven3.3.9使用入门

發布時間:2023/12/3 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 maven(3)maven3.3.9使用入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【0】README 1)maven 安裝 step1)檢查 jdk ?是否安裝且 環境變量 JAVA_HOME 是否設置; step2)download maven:?https://maven.apache.org/download.cgi?Preferred=ftp://mirror.reverse.net/pub/apache/ step3)解壓,同樣添加maven的環境變量M2_HOME(maven_home/bin 所在目錄); step4)mvn -n 測試 maven 安裝是否正確;
2)相關概念補充(Supplement) s1)構建:我們每天有相當一部分時間花在了編譯,運行單元測試,生成文檔,打包和部署等繁瑣且不起眼的工作上,這叫做構建; s2)maven 通過一個坐標系統準確地定位每一個構建(jar文件):也就是通過一組坐標 maven 能夠找到任何一個 java 類庫(如jar文件);(干貨——引入了坐標+定位)
3)maven的中央倉庫和本地倉庫 3.1)中央倉庫:maven 為全球的 java 開發者提供了一個免費的中央倉庫(http://repo1.maven.org/maven2/),在其中集合可以找到任何的流行開源類庫;(干貨——引入了中央倉庫,當然與之對應的還有本地倉庫) 3.2)本地倉庫:用戶目錄/.m2/repository/ (用戶目錄如 C:\Users\lenovo) ; (t2)
4)配置用戶范圍 settings.xml 4.1)大多數用戶需要將 M2_HOME/conf/settings.xml(dir1) copy 到 本地倉庫的父目錄下(dir2)(即它們是兄弟,如上圖所示) 4.2)reasons: reason1)dir1 是全局配置,該機子上的所有用戶都要受到該配置的影響,而dir2 是用戶范圍的,只對當前用戶(lenovo)產生影響; reason2)配置用戶范圍 settting.xml 文件還便于maven升級。直接修改 conf/settings.xml 文件 會導致 maven 升級不便; Attention) A1)maven 能夠幫助我們 清理,編譯,測試,打包,部署,然后得到 final products; A2)通過 其 衍生工具 Nexus,我們還能對開源類庫(如jar)進行快速地搜索;
【1】編寫POM 1)Hello的 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.0http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.maven.chapter3</groupId><artifactId>service</artifactId><version>1.0-SNAPSHOT</version><name>service says hello maven.</name> </project> 對以上代碼的分析(Analysis):
A1)project:是所有 pom.xml 的根元素,它聲明了一些 POM 相關的命名空間和 xsd 元素; A2)modelVersion: 指定了當前 pom 模型的版本,對于 maven 3來說,它只能是4.0.0 A3)groupId:定義了項目輸入那個組; A4)artifactId:定義了當前maven項目在組中唯一的id號碼; A5)version:指定了Hello 項目當前的版本; A6)name:聲明了一個對于用戶更為友好的項目名稱; Attention) A1)要知道, groupId + artifactId ?+ version 組成了一個坐標,當然你也看做是 三維坐標用來定位 所需 類庫的位置;(干貨——坐標==groupId + artifactId ?+ version,坐標的作用是定位 依賴類庫的位置) A2)看個荔枝:以junit為例,其jar list 如下所示;如果要用 Junit4.7的話,則其 坐標定義如下: <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> (t3) (t4)
【2】編寫主代碼 1)Hello.java 代碼如下: package com.maven.chapter3.service;public class Hello {public String sayHello() {return "hello maven.";}public static void main(String[] args) {System.out.println((new Hello()).sayHello());} } 2)關于主代碼有兩點注意(Attention): A1)在 大多數case下:應該把項目主代碼放到 src/main/java 目錄下;maven 會自動搜尋該目錄找到項目主代碼; A2)該java類的包名是 com.maven.chapter3.service:這與之前在 pom 中定義的 groupId 和 artifactID 要吻合; 3)代碼編寫完畢后,使用maven進行編譯(mvn clean compile) 對以上console info的分析(Analysis): A1)以上命令所進行的任務有: clean:clean, resources:resource, compiler:compile; A2)clean 告訴 maven 清理輸出目錄 target/;compile 編譯項目主代碼;resources:resources 任務(未定義項目資源,略過);

【3】編寫測試代碼(測試用例) 1)HelloTest.java 源碼如下: package com.maven.chapter3.service;import static org.junit.Assert.*;import org.junit.Test;public class HelloTest {@Testpublic void testSayHello() {Hello hello = new Hello();String result = hello.sayHello();assertEquals("hello maven.", result);} } 2)關于測試代碼有兩點注意(Attention): A1)在 大多數case下:應該把測試代碼放到 src/test/java 目錄下;maven 會自動搜尋該目錄找到項目測試代碼; A2)測試代碼需要依賴 Junit,添加對JUnit的依賴,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.0http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.maven.chapter3</groupId><artifactId>service</artifactId><version>1.0-SNAPSHOT</version><name>service says hello maven.</name><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.7</version><scope>test</scope></dependency></dependencies> </project> 對以上代碼的分析(Analysis):? A1)以上xml文件有 scope元素,scope表示依賴范圍; A2)依賴范圍scope==test:則表明該依賴只對測試有效;換句話說,測試代碼中 import JUnit 代碼是沒有問題的,但如果在主代碼中 import JUnit,就會造成編譯錯誤; A3)依賴范圍(default)scope==compile:如果不聲明依賴訪問,默認是compile,這樣表示該依賴對主代碼和測試代碼都有效;
3)調用maven執行測試(mvn clean test)
【4】打包和運行 1)intro: 簡單地執行命令 mvn clean package 進行打包; D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn clean package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building service says hello maven. 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ service --- // clean task. [INFO] Deleting D:\classical_books\java_set\maven_in_action\mycode\chapter3\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service --- // resources task. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service --- // compile task. [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:\classical_books\java_set\maven_in_action\mycode\chapter3\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service --- // testResources task. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service --- // testCompile task. [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:\classical_books\java_set\maven_in_action\mycode\chapter3\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service --- // test task. [INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports-------------------------------------------------------T E S T S ------------------------------------------------------- Running com.maven.chapter3.service.HelloTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service --- [INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar // jar task 負責打包. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS // 構建成功. [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.599 s [INFO] Finished at: 2016-06-16T22:18:46+08:00 [INFO] Final Memory: 17M/168M [INFO] ------------------------------------------------------------------------
2)如何才能讓其他項目直接引用這個 jar呢? 執行mvn clean install 安裝 jar 到本地倉庫; D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn clean install [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building service says hello maven. 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ service --- [INFO] Deleting D:\classical_books\java_set\maven_in_action\mycode\chapter3\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service --- [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:\classical_books\java_set\maven_in_action\mycode\chapter3\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service --- [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:\classical_books\java_set\maven_in_action\mycode\chapter3\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service --- [INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports-------------------------------------------------------T E S T S ------------------------------------------------------- Running com.maven.chapter3.service.HelloTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.051 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service --- [INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ service --- Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom (3 KB at 1.3 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom (2 KB at 2.5 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom (5 KB at 10. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar (12 KB at 13.7 KB/sec) Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar (226 KB at 189.1 KB/se [INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar to C:\Users\lenovo\.m2\ ven\chapter3\service\1.0-SNAPSHOT\service-1.0-SNAPSHOT.jar [INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\pom.xml to C:\Users\lenovo\.m2\repository\com\maven\cha -SNAPSHOT\service-1.0-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.750 s [INFO] Finished at: 2016-06-16T22:24:09+08:00 [INFO] Final Memory: 19M/165M [INFO] ------------------------------------------------------------------------ 3)對執行命令的分析: A1)maven的命令列表有:mvn clean compile, mvn clean test, mvn clean install; A2)命令執行順序: 執行test之前要先執行 compile,執行package 之前要執行test,執行install 之前要執行packge;所以如果 鍵入?mvn clean install ,則執行順序是 compile->test->package->install ;
4) 生成可執行的jar 文件, 4.1)默認情況下:打包生成的 jar 是不能夠直接運行的,因為帶有main 方法的類信息不回添加到 manifest中; 4.2)生成可執行的jar 文件:?需要借助maven-shade-plugin,配置該插件如下; <?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.0http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.maven.chapter3</groupId><artifactId>service</artifactId><version>1.0-SNAPSHOT</version><name>service says hello maven.</name><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.7</version><scope>test</scope></dependency></dependencies><build><plugins> <!-- maven-shade-plugin configuration --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.5</source><target>1.5</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>1.2.1</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><transformers><transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>com.maven.chapter3.service.Hello</mainClass></transformer></transformers></configuration></execution></executions></plugin></plugins></build> </project> 4.3)執行 mvn clean install 并查看目錄樹 D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn clean install // 執行 mvn clean install [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.maven.chapter3:service:jar:1.0-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 22, column 15 [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 service says hello maven. 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.pom (6 KB at 3.1 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/13/maven-plugins-13.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/13/maven-plugins-13.pom (12 KB at 27.3 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.jar (67 KB at 112.0 KB/sec ) [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ service --- [INFO] Deleting D:\classical_books\java_set\maven_in_action\mycode\chapter3\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service --- [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:\classical_books\java_set\maven_in_action\mycode\chapter3\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service --- [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:\classical_books\java_set\maven_in_action\mycode\chapter3\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service --- [INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports-------------------------------------------------------T E S T S ------------------------------------------------------- Running com.maven.chapter3.service.HelloTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.045 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service --- [INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar [INFO] [INFO] --- maven-shade-plugin:1.2.1:shade (default) @ service --- Downloading: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.pom Downloaded: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.pom (436 B at 1.0 KB/sec) Downloading: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.pom Downloaded: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.pom (425 B at 1.1 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.pom (3 KB at 6.5 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.5/plexus-utils-1.5.5.jar Downloading: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.jar Downloading: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.jar Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.jar Downloaded: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.jar (32 KB at 72.6 KB/sec) Downloaded: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.jar (22 KB at 17.1 KB/sec) Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.jar (34 KB at 26.4 KB/sec ) Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.5/plexus-utils-1.5.5.jar (246 KB at 73.4 KB/sec) [INFO] Replacing original artifact with shaded artifact. [INFO] Replacing D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar with D:\classical_books\java_set\maven_in _action\mycode\chapter3\target\service-1.0-SNAPSHOT-shaded.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ service --- [INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar to C:\Users\lenovo\.m2\repository\com\ma ven\chapter3\service\1.0-SNAPSHOT\service-1.0-SNAPSHOT.jar [INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\pom.xml to C:\Users\lenovo\.m2\repository\com\maven\chapter3\service\1.0 -SNAPSHOT\service-1.0-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 11.789 s [INFO] Finished at: 2016-06-17T08:51:15+08:00 [INFO] Final Memory: 20M/172M [INFO] ------------------------------------------------------------------------D:\classical_books\java_set\maven_in_action\mycode\chapter3>tree /f // 查看目錄樹. 卷 軟件 的文件夾 PATH 列表 卷序列號為 0006-7799 D:. │ filelist.txt │ pom.xml │ pom.xml.bak │ ├─src │ ├─main │ │ └─java │ │ └─com │ │ └─maven │ │ └─chapter3 │ │ └─service │ │ Hello.java │ │ │ └─test │ └─java │ └─com │ └─maven │ └─chapter3 │ └─service │ HelloTest.java │ └─target│ original-service-1.0-SNAPSHOT.jar│ service-1.0-SNAPSHOT.jar│├─classes│ └─com│ └─maven│ └─chapter3│ └─service│ Hello.class│├─maven-archiver│ pom.properties│├─maven-status│ └─maven-compiler-plugin│ ├─compile│ │ └─default-compile│ │ createdFiles.lst│ │ inputFiles.lst│ ││ └─testCompile│ └─default-testCompile│ createdFiles.lst│ inputFiles.lst│├─surefire-reports│ com.maven.chapter3.service.HelloTest.txt│ TEST-com.maven.chapter3.service.HelloTest.xml│└─test-classes└─com└─maven└─chapter3└─serviceHelloTest.class 對以上目錄的分析(Analysis): A1)打開target,可以看到 有兩個jar 文件 original-service-1.0-SNAPSHOT.jar 和?service-1.0-SNAPSHOT.jar;前者是原始的jar, 后者是帶有 Main-class 信息的可運行jar; A2)打開可運行jar(service-1.0-SNAPSHOT.jar) 的 META-INF/MANIFEST.MF,可以看到如下信息: Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Built-By: lenovo Created-By: Apache Maven 3.3.9 Build-Jdk: 1.8.0_60 Main-Class: com.maven.chapter3.service.Hello // highlight line. 4.4)執行?service-1.0-SNAPSHOT.jar??文件 D:\classical_books\java_set\maven_in_action\mycode\chapter3>java -jar target\service-1.0-SNAPSHOT.jar hello maven. // 執行結果.
【5】使用 Archetype 生成項目骨架 0)intro to Archetype(骨架):我們把maven基本的目錄結構和 pom.xml 文件內容(見上述目錄樹) 稱為項目的骨架;(干貨——引入骨架) 1)problem + solution 1.1)problem:后續的項目需要用到 Hello 項目,不可能每次都重復上述steps 來構建 Hello 項目; 1.2)solution:使用 maven archetype 來創建該項目的骨架,以便于后續項目引用;
2)生成項目骨架命令:mvn archetype:generate,該命令實際上是運行插件 maven-archetype-plugin; 3)看個荔枝: 為 Hello 項目生成骨架 step1)輸入 mvn archetype:generate 生成骨架

step2)骨架生成后的文件樹 D:\classical_books\java_set\maven_in_action\mycode\archetype_test>tree /f 卷 軟件 的文件夾 PATH 列表 卷序列號為 0006-7799 D:. └─service│ pom.xml│└─src├─main│ └─java│ └─com│ └─maven│ └─chapter3│ └─service│ App.java│└─test└─java└─com└─maven└─chapter3└─serviceAppTest.java Attention) A1)要將某項目生成骨架,這之前要安裝該項目到本地倉庫; A2)退出當前項目文件,即建立一個空文件夾,然后再進入到該空文件夾 生成骨架(mvn archetype:generate)

總結

以上是生活随笔為你收集整理的maven(3)maven3.3.9使用入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。