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