为什么要练习这个?
主要是想熟悉一下maven 的工程结构,本身也不是做Java开发的,能从代码里把逻辑扒出来就行 --!
这个练习参考《Maven实战》 许晓斌 著。
创建一个learnmvn目录,用来存放不同阶段的项目目录。
然后创建hello-mvn目录,这是项目目录。
目录结构
learnmvn/hello-mvn
文件路径 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>
必备的配置项
默认情况下,按照maven约定,项目的主代码应该放到 src/main/java 目录下,项目的测试代码应该放到src/test/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());
}
}
在项目根目录下打开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] ------------------------------------------------------------------------
单元测试的三个步骤:
<?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>
按照maven约定,项目的测试代码应该放到src/test/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"
在项目根目录下打开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>
执行编译、测试之后,下一个步骤是打包。
在项目根目录下打开cmd , 运行 mvn clean test
可以发现mvn会依次执行 clean,resources,compile,testResources,testCompile,test,jar
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] ------------------------------------------------------------------------
对于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] ------------------------------------------------------------------------
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] ------------------------------------------------------------------------
原文:https://www.cnblogs.com/fooobabar/p/14216017.html