Maven - 实例-1-手工创建Maven项目
2023-09-14 09:00:40 时间
1- 根据包结构创建maven项目目录
TestMaven
- src
- src/main/java/anliven/testmaven01/HelloMaven.java
- src/test/java/anliven/testmanven01/HelloMavenTest.java
- pom.xml
2- 示例代码和pom文件
<groupId></groupId> :项目的包名
<artifactId></artifactId> : 模块名
<version></version> : 版本,遵循Maven版本管理规范
HelloMaven.java
package anliven.testmaven01;
public class HelloMaven {
public String sayHello() {
return "Hello Maven!";
}
}
HelloMavenTest.java
package anliven.testmaven01;
import org.junit.*;
import org.junit.Assert.*;
public class HelloMavenTest {
@Test
public void testMaven() {
System.out.println("Run test!");
Assert.assertEquals("Hello Maven!", new HelloMaven().sayHello());
}
}
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>anliven.testmaven01</groupId>
<artifactId>testmaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
3- 运行mvn compile
如果是第一次运行mvn compile等命令时,将会下载很多的第三方和maven所依赖的jar包。
在Maven项目根目录下,默认生成target目录:
target 构建过程中的默认生成的临时目录
target/classes/ 存放src/main/java目录下源文件编译出来的字节码文件(.class)
target/maven-status/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
[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:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.064 s
[INFO] Finished at: 2017-10-20T10:56:12+08:00
[INFO] Final Memory: 15M/292M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l
total 1
-rw-r--r-- 1 guowli 1049089 561 Oct 19 17:44 pom.xml
drwxr-xr-x 1 guowli 1049089 0 Oct 19 13:21 src/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 target/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ls -l target/classes/anliven/testmaven01/HelloMaven.class
-rw-r--r-- 1 guowli 1049089 406 Oct 20 10:56 target/classes/anliven/testmaven01/HelloMaven.class
4- 运行mvn test
生成如下目录:
target/surefire-reports/ 存放生成的测试报告
target/test-classes/ 存放src/test/java目录下源文件编译出来的字节码文件(.class)
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven ---
[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:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running anliven.testmaven01.HelloMavenTest
Run test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.064 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.286 s
[INFO] Finished at: 2017-10-20T11:01:06+08:00
[INFO] Final Memory: 16M/291M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 surefire-reports/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 test-classes/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/surefire-reports/
total 9
-rw-r--r-- 1 guowli 1049089 282 Oct 20 11:01 anliven.testmaven01.HelloMavenTest.txt
-rw-r--r-- 1 guowli 1049089 6398 Oct 20 11:01 TEST-anliven.testmaven01.HelloMavenTest.xml
5- 运行mvn package
生成如下目录及文件:
target/maven-archiver/
target/xxx-y.y.y-zzz.jar 生成的jar包
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 surefire-reports/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 test-classes/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven ---
[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:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running anliven.testmaven01.HelloMavenTest
Run test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ testmaven ---
[INFO] Building jar: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\testmaven-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.437 s
[INFO] Finished at: 2017-10-20T11:13:48+08:00
[INFO] Final Memory: 16M/213M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 4
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:13 maven-archiver/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 surefire-reports/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 test-classes/
-rw-r--r-- 1 guowli 1049089 2151 Oct 20 11:13 testmaven-0.0.1-SNAPSHOT.jar
相关文章
- Maven项目导入时,org .apache.maven.plugins依赖爆红报错
- maven编译报错:java.lang.ExceptionInInitializerError: com.sun.tools.javac.code.TypeTags[通俗易懂]
- Eclipse使用之导入Maven项目详解[通俗易懂]
- Eclipse使用Maven创建Web时错误:Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webap
- tomcat java_maven和tomcat的关系
- IDEA运行web项目需要的一些配置(tomcat,maven)
- Maven 菜鸟教程 1 安装和配置[通俗易懂]
- Maven docker-maven-plugin 插件 Push 413 错误
- SpringCloud (五) - 云服务器Centos7.6,安装JDK,Maven,Mysql,Redis
- maven阿里云仓库配置_阿里docker仓库
- 软件测试|SonarQube 安装、配置及 JaCoCo、Maven 集成
- maven常用dos命令详解程序员
- SSM项目频繁打成war包部署,使用tomcat和maven实现热部署配置
- maven项目在eclipse的library中没有Maven Dependencies详解程序员
- Spring+Mybatis+Maven+MySql搭建实例详解编程语言
- Maven pom.xml 配置详解编程语言
- IDEA执行Maven命令
- Maven使用GPG对文件进行签名加密
- Maven自定义插件的Mojo标记和参数
- Maven搭建SSH(Struts2+Spring+Hibernate)框架入门教程(附带实例)
- Maven搭建SSH(Struts2+Spring+Hibernate)框架入门教程(二)
- Linux系统快速部署Maven环境(linux安装maven)
- Maven与Redis完美结合实现高效管理Creating efficient Redis Integration using Maven(maven整合redis)
- 驱动Maven管理Oracle数据库驱动的安装与部署(maven的oracle)
- 技巧Redis联合Maven进阶实践配置技巧篇(redis配置maven)