博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Maven初步入门(二)
阅读量:5845 次
发布时间:2019-06-18

本文共 4974 字,大约阅读时间需要 16 分钟。

hot3.png

1. 什么是POM

Maven项目的核心是pom.xml, POM(Project Object Model)定义了项目的基本信息,用于描述项目如何构建、声明项目依赖等等。

首先我们使用Eclipse新建一个Maven项目,项目名为hello-world,如下图:

在pom.xml中最重要几个重要的元素有groupId、artifactId、version、dependencies等。

groupId:定义了项目属于哪个组,该组一般和项目所在的组织或公司有关。比如你在googlecode上建立一个名为myapp的项目,那么groupId为com.googlecode.myapp。

artifactId: 定义了当前Maven项目中中唯一的ID,比如前面的groupId为com.googlecode.myapp,我们可以为不同的子项目或者模块分配不同的artifactId,如myapp-util, myapp-dao,myapp-web等。

version: 指定了项目当前的版本。SNAPSHOT意为快照,表示项目还在开发中,还不稳定。

dependencies: 该元素下可以包含多个dependency以声明项目的依赖。这里添加了一个依赖--groupId为junit,artifactId为junit,version为4.10.有了这段声明Maven会自动从中央仓库下载junit-4.10.jar。

2. 编写代码

1) Maven项目的主代码位于src/main/java目录中,在上图中我们可以看到。代码编写完毕后,右键pom.xml --> Run as -->Maven build,在Goals输入 clean compile,会得到如下结果:

[INFO] Scanning for projects...[INFO]                                                                         [INFO] ------------------------------------------------------------------------[INFO] Building hello-world 0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---[INFO] Deleting D:\code\maven\hello-world\target[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\main\resources[INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world ---[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent![INFO] Compiling 1 source file to D:\code\maven\hello-world\target\classes[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 2.567s[INFO] Finished at: Thu Nov 21 22:13:29 CST 2013[INFO] Final Memory: 9M/101M[INFO] ------------------------------------------------------------------------
2) 测试代码位于独立的目录中,默认的测试代码目录是src/test/java,编写测试代码:
package com.alvinliang.maven;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 World", result);	}}
之后调用Maven执行测试,右键pom.xml --> Run as --> Maven build, 在goals中输入clean test:
[INFO] Scanning for projects...[INFO]                                                                         [INFO] ------------------------------------------------------------------------[INFO] Building hello-world 0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---[INFO] Deleting D:\code\maven\hello-world\target[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\main\resources[INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world ---[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent![INFO] Compiling 1 source file to D:\code\maven\hello-world\target\classes[INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\test\resources[INFO] [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hello-world ---[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent![INFO] Compiling 1 source file to D:\code\maven\hello-world\target\test-classes[INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---[INFO] Surefire report directory: D:\code\maven\hello-world\target\surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running com.alvinliang.maven.HelloWorldTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 5.214s[INFO] Finished at: Thu Nov 21 22:35:11 CST 2013[INFO] Final Memory: 11M/101M[INFO] ------------------------------------------------------------------------
这样我们就学会如何简单的使用Maven新建项目了,重点要理解POM的基本概念。

转载于:https://my.oschina.net/liangbo/blog/178171

你可能感兴趣的文章
ant命令总结
查看>>
Redis的过期策略和内存淘汰策略
查看>>
javascript写的ajax请求
查看>>
寄存器是什么 有什么作用
查看>>
我对javascript面向对象编程的理解---------继承
查看>>
管理大量定时任务,如果高效触发超时?
查看>>
转载 《Python爬虫学习系列教程》学习笔记
查看>>
Android中实现两次点击返回键退出本程序
查看>>
Flex组件的项目渲染器(ItemRenderer)使用总结
查看>>
网络学习笔记
查看>>
python - 列表
查看>>
Android开发笔记(成长轨迹)
查看>>
Android系统移植与调试之------->如何修改Android手机显示的4G信号强度的格子数
查看>>
linux 命令
查看>>
转 oracle 监控执行计划突然变化
查看>>
Hibernate中Criteria的完整用法2
查看>>
NGUI的输入框制作(attach- input filed script的使用)
查看>>
Java二十三设计模式之-----桥接模式
查看>>
二分查找C++实现
查看>>
[异常笔记] zookeeper集群启动异常: Cannot open channel to 2 at election address ……
查看>>