mvn archetype:create 建立Maven項目 mvn compile 編譯源代碼 mvn deploy 發佈項目 mvn test-compile 編譯測試源代碼 mvn test 運行應用程序中的單元測試 mvn site 生成項目相關信息的網站 mvn clean 清除項目目錄中的生成結果 mvn package 根據項目生成的jar mvn install 在本地Repository中安裝jar mvn eclipse:eclipse 生成eclipse項目文件 mvnjetty:run 啓動jetty服務 mvntomcat:run 啓動tomcat服務
一、編寫pom文件java
首先建立一個名爲hello-world的文件夾,打開該文件夾,新建一個名爲pom.xml的文件,輸入其內容如代碼以下:apache
<?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-world</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> <build> <plugins> <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.juvenxu.mvnbook.helloworld.HelloWorld</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
二、編寫主代碼tomcat
默認狀況下,Maven假設項目主代碼位於src/main/java目錄,咱們遵循Maven的約定,建立該目錄,而後在該目錄下建立文件com/juvenxu/mvnbook/helloworld/HelloWorld.java,其內容以下dom
package com.juvenxu.mvnbook.helloworld; public class HelloWorld { public String sayHello() { return "Hello Maven"; } public static void main(String[] args) { System.out.print( new HelloWorld().sayHello()); } }
這是一個簡單的Java類,它有一個sayHello()方法,返回一個String。同時這個類還帶有一個main方法,建立一個HelloWorld實例,調用sayHello()方法,並將結果輸出到控制檯。eclipse
在項目根目錄下運行命令 mvn clean compile ,咱們會獲得以下輸出maven
[root@wangmaster hello-world]# mvn clean compile [INFO] Scanning for projects... [INFO] [INFO] ------------------< com.juvenxu.mvnbook:hello-world >------------------- [INFO] Building Maven Hello World Project 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world --- [INFO] Deleting /opt/project/maven/hello-world/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /opt/project/maven/hello-world/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /opt/project/maven/hello-world/target/classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.659 s [INFO] Finished at: 2018-06-13T05:44:12+08:00 [INFO] ------------------------------------------------------------------------
三、編寫測試代碼 單元測試
Maven項目中默認的主代碼目錄是src/main/java,對應地,Maven項目中默認的測試代碼目錄是src/test/java。所以,在編寫測試用例以前,咱們先建立該目錄。測試
在該目錄下添加HelloWorldTest.java文件,內容以下網站
package com.juvenxu.mvnbook.helloworld; import static org.junit.Assert.assertEquals; import org.junit.Test; public class HelloWorldTest { @Test public void testSayHello() { HelloWorld helloWorld = new HelloWorld(); String result = helloWorld.sayHello(); assertEquals( "Hello Maven", result ); } }
測試用例編寫完畢以後就能夠調用Maven執行測試,運行 mvn clean test :ui
[root@wangmaster hello-world]# mvn clean test [INFO] Scanning for projects... [INFO] [INFO] ------------------< com.juvenxu.mvnbook:hello-world >------------------- [INFO] Building Maven Hello World Project 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world --- [INFO] Deleting /opt/project/maven/hello-world/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /opt/project/maven/hello-world/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /opt/project/maven/hello-world/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /opt/project/maven/hello-world/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-world --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /opt/project/maven/hello-world/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world --- [INFO] Surefire report directory: /opt/project/maven/hello-world/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.212 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.599 s [INFO] Finished at: 2018-06-13T05:44:24+08:00 [INFO] ------------------------------------------------------------------------
四、打包及運行
Hello World的POM中沒有指定打包類型,使用默認打包類型jar,咱們能夠簡單地執行命令 mvn clean package 進行打包。
[root@wangmaster hello-world]# mvn clean package [INFO] Scanning for projects... [INFO] [INFO] ------------------< com.juvenxu.mvnbook:hello-world >------------------- [INFO] Building Maven Hello World Project 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.pom (5.8 kB at 1.9 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/13/maven-plugins-13.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/13/maven-plugins-13.pom (12 kB at 18 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.jar Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.jar (68 kB at 63 kB/s) [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world --- [INFO] Deleting /opt/project/maven/hello-world/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /opt/project/maven/hello-world/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /opt/project/maven/hello-world/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /opt/project/maven/hello-world/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-world --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /opt/project/maven/hello-world/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world --- [INFO] Surefire report directory: /opt/project/maven/hello-world/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.142 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world --- [INFO] Building jar: /opt/project/maven/hello-world/target/hello-world-1.0-SNAPSHOT.jar [INFO] [INFO] --- maven-shade-plugin:1.2.1:shade (default) @ hello-world --- Downloading from central: https://repo.maven.apache.org/maven2/asm/asm/3.1/asm-3.1.pom Downloaded from central: https://repo.maven.apache.org/maven2/asm/asm/3.1/asm-3.1.pom (278 B at 515 B/s) Downloading from central: https://repo.maven.apache.org/maven2/asm/asm-parent/3.1/asm-parent-3.1.pom Downloaded from central: https://repo.maven.apache.org/maven2/asm/asm-parent/3.1/asm-parent-3.1.pom (4.2 kB at 8.4 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.pom Downloaded from central: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.pom (436 B at 807 B/s) Downloading from central: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.pom Downloaded from central: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.pom (425 B at 863 B/s) Downloading from central: https://repo.maven.apache.org/maven2/jdom/jdom/1.0/jdom-1.0.pom Downloaded from central: https://repo.maven.apache.org/maven2/jdom/jdom/1.0/jdom-1.0.pom (1.2 kB at 2.2 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.pom (2.9 kB at 5.2 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-components/8/maven-shared-components-8.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-components/8/maven-shared-components-8.pom (2.7 kB at 4.7 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/7/maven-parent-7.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/7/maven-parent-7.pom (21 kB at 31 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/2.0.8/maven-project-2.0.8.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/2.0.8/maven-project-2.0.8.pom (2.7 kB at 4.8 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0.8/maven-2.0.8.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0.8/maven-2.0.8.pom (12 kB at 19 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/6/maven-parent-6.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/6/maven-parent-6.pom (20 kB at 28 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-settings/2.0.8/maven-settings-2.0.8.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-settings/2.0.8/maven-settings-2.0.8.pom (2.1 kB at 3.5 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.0.8/maven-model-2.0.8.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.0.8/maven-model-2.0.8.pom (3.1 kB at 5.5 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.4.6/plexus-utils-1.4.6.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.4.6/plexus-utils-1.4.6.pom (2.3 kB at 4.0 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/2.0.8/maven-profile-2.0.8.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/2.0.8/maven-profile-2.0.8.pom (2.0 kB at 4.2 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact-manager/2.0.8/maven-artifact-manager-2.0.8.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact-manager/2.0.8/maven-artifact-manager-2.0.8.pom (2.7 kB at 5.3 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-repository-metadata/2.0.8/maven-repository-metadata-2.0.8.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-repository-metadata/2.0.8/maven-repository-metadata-2.0.8.pom (1.9 kB at 4.3 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0.8/maven-artifact-2.0.8.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0.8/maven-artifact-2.0.8.pom (1.6 kB at 2.8 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-registry/2.0.8/maven-plugin-registry-2.0.8.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-registry/2.0.8/maven-plugin-registry-2.0.8.pom (2.0 kB at 4.2 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.5/plexus-utils-1.5.5.jar Downloading from central: https://repo.maven.apache.org/maven2/asm/asm/3.1/asm-3.1.jar Downloading from central: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.jar Downloading from central: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.jar Downloading from central: https://repo.maven.apache.org/maven2/jdom/jdom/1.0/jdom-1.0.jar Downloaded from central: https://repo.maven.apache.org/maven2/asm/asm/3.1/asm-3.1.jar (43 kB at 38 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.jar Downloaded from central: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.jar (22 kB at 13 kB/s) Downloaded from central: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.jar (33 kB at 14 kB/s) Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.jar (34 kB at 15 kB/s) Downloaded from central: https://repo.maven.apache.org/maven2/jdom/jdom/1.0/jdom-1.0.jar (153 kB at 57 kB/s) Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.5/plexus-utils-1.5.5.jar (251 kB at 75 kB/s) [INFO] Replacing original artifact with shaded artifact. [INFO] Replacing /opt/project/maven/hello-world/target/hello-world-1.0-SNAPSHOT.jar with /opt/project/maven/hello-world/target/hello-world-1.0-SNAPSHOT-shaded.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 25.081 s [INFO] Finished at: 2018-06-13T05:45:01+08:00 [INFO] ------------------------------------------------------------------------
咱們還須要一個安裝的步驟,執行 mvn clean install:
[root@wangmaster hello-world]# mvn clean install [INFO] Scanning for projects... [INFO] [INFO] ------------------< com.juvenxu.mvnbook:hello-world >------------------- [INFO] Building Maven Hello World Project 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world --- [INFO] Deleting /opt/project/maven/hello-world/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /opt/project/maven/hello-world/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /opt/project/maven/hello-world/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /opt/project/maven/hello-world/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-world --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /opt/project/maven/hello-world/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world --- [INFO] Surefire report directory: /opt/project/maven/hello-world/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.171 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world --- [INFO] Building jar: /opt/project/maven/hello-world/target/hello-world-1.0-SNAPSHOT.jar [INFO] [INFO] --- maven-shade-plugin:1.2.1:shade (default) @ hello-world --- [INFO] Replacing original artifact with shaded artifact. [INFO] Replacing /opt/project/maven/hello-world/target/hello-world-1.0-SNAPSHOT.jar with /opt/project/maven/hello-world/target/hello-world-1.0-SNAPSHOT-shaded.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-world --- [INFO] Installing /opt/project/maven/hello-world/target/hello-world-1.0-SNAPSHOT.jar to /root/.m2/repository/com/juvenxu/mvnbook/hello-world/1.0-SNAPSHOT/hello-world-1.0-SNAPSHOT.jar [INFO] Installing /opt/project/maven/hello-world/pom.xml to /root/.m2/repository/com/juvenxu/mvnbook/hello-world/1.0-SNAPSHOT/hello-world-1.0-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.932 s [INFO] Finished at: 2018-06-13T05:45:13+08:00 [INFO] ------------------------------------------------------------------------
如今,咱們在項目根目錄中執行該jar文件:
[root@wangmaster hello-world]# java -jar target/hello-world-1.0-SNAPSHOT.jar Hello Maven
參考文章:https://blog.csdn.net/u010523770/article/details/70168353