Maven - 实例-1-手工创建Maven项目

时间:2021-08-04 03:25:03

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 - 实例-1-手工创建Maven项目的更多相关文章

  1. Maven - 实例-3-自动创建Maven目录骨架

    archetype插件用于创建符合maven规定的目录骨架 方式一:根据提示设置相关参数 guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/Eclips ...

  2. 使用Maven在Eclipse中创建Web项目&lbrack;转&rsqb;

    一.新建 Maven Web项目 1.新建Maven Project new project-->选择 Maven Project --> 下一步 选择工作空间 -->下一步 在Fi ...

  3. Maven - 在Eclipse中创建Maven项目

    本文的前提条件: windows7-64bit jdk1.8.0 Maven-3.5.0 1- 更新Eclipse中Maven配置 1.1- 修改Eclipse根目录下eclipse.ini文件 D: ...

  4. Maven 梳理 -eclipse中创建Maven的web项目

    已验证成功: 1.创建Maven的Web工程 新建Maven工程:File -> New -> Maven Project,具体如下所示: 点击Next进入下述页面,下方的窗口是我们选择所 ...

  5. 用maven在eclipse中创建Web项目

    使用eclipse插件创建一个web project 首先创建一个Maven的Project如下图 我们勾选上Create a simple project (不使用骨架) 这里的Packing 选择 ...

  6. Maven&lpar;二&rpar;使用eclipse创建maven多模块项目

    maven作为一种自动化构建工具,在现在的企业应用开发中运用非常普遍. 企业项目一般都比较大,多采用maven管理的多模块项目,下面直接上创建步骤 一.创建一个maven项目

  7. maven环境搭建及创建maven项目

    Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 1.maven下载地址http://maven.apache.org/download.cgi ...

  8. Idea 创建maven web项目(手工创建)

    参考链接:https://www.cnblogs.com/justuntil/p/7511787.html 话不多说,直接上图: 1.创建maven项目 创建项目完成,项目结构如下: 2.项目部署配置 ...

  9. IntelliJ IDEA 15 创建maven项目

    说明 创建Maven项目的方式:手工创建 好处:参考IntelliJ IDEA 14 创建maven项目二(此文章描述了用此方式创建Maven项目的好处)及idea14使用maven创建web工程(此 ...

随机推荐

  1. Unity C&num;最佳实践(上)

    本文为<effective c#>的读书笔记,此书类似于大名鼎鼎的<effective c++>,是入门后提高水平的进阶读物,此书提出了50个改进c#代码的原则,但是由于主要针 ...

  2. BigDecimal 详细解析

    参加工作之后发现公司项目的数据库有关数值的字段类型,很少是之前常用的float和double, 而是一个没有接触过的decimal,在Java中表示的类型为BigDecimal, 而在业务中常常有关B ...

  3. WebApp:如何让安卓的webview缓存webapp的html、js和图片等资源

    一.开发环境     客户端:安卓+webview(vuejs)     服务器端:tomcat 8.0 二.问题     使用安卓原生+web(基于webpack+vuejs)的方式开发了一个安卓应 ...

  4. 从github上面拷贝源码

    http://www.cnblogs.com/xing901022/p/4287064.html

  5. spring security源码分析之web包分析

    Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案.一般来说,Web 应用的安全性包括 ...

  6. delphi TOpenDialog

        TOpenDialog     procedure TForm1.Button1Click(Sender: TObject);begin  with TOpenDialog.Create(ni ...

  7. 黑马程序员 Java基础&lt&semi;九&gt&semi;---&gt&semi; 多线程

    ASP.Net+Android+IOS开发..Net培训.期待与您交流! 多线程 一.概述: 1.线程是什么 说到线程,我们就得先说说进程.所谓进程,就是一个正在执行(进行)中的程序,每一个进程执行都 ...

  8. iOS 10权限崩溃问题

    手机升级到iOS10 Beta版本后,再次进行真机调试时会遇见权限崩溃问题,崩溃原因如下:This app has crashed because it attempted to access pri ...

  9. URLEncode和URLDecoder作用

    一.基本原理 对于URL传递到后台,会对其中的有些字符进行编码,以下是我百度到的一些资料. 网页中的表单使用POST方法提交时,数据内容的类型是 application/x-www-form-urle ...

  10. light oj 1152 Hiding Gold

    题目: You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x ...