Maven使用二
一、maven的生命周期
maven有三套相互独立的生命周期:clean、default(build)、site
ps:所谓的生命周期其实就是maven起作用的时间段,低版本的maven管理生命周期是需要通过dos下的输入指令
才能完成的(如下图maven的生命周期的示意图.png),但是高版本的就不需要了
1、clean
Clean: 在进行真正的构建之前进行一些清理工作。简称 “清理”,且包含三个phase(阶段)。
1)pre-clean:执行清理前需要完成的工作
2)clean:清理上一次构建生成的文件
3)post-clean:执行清理后需要完成的工作
2、default
Default:定义了真正构建时所需要执行的所有步骤:,编译,测试,打包,部署等等。
简称"构建项目",且包含以下几个重要的阶段
1)validate:验证工程是否正确,所有需要的资源是否可用。
2)compile:编译项目的源代码。
3)test:使用合适的单元测试框架来测试已编译的源代码。这些测试不需要已打包和布署。
4)package:把已编译的代码打包成可发布的格式,比如jar。
5)integration-test:如有需要,将包处理和发布到一个能够进行集成测试的环境。
6)verify:运行所有检查,验证包是否有效且达到质量标准。
7)install:把jar包安装到maven本地仓库,可以被其他工程作为依赖来使用
(至于生成的jar包我们可以在本地的maven仓库中是能找到的)。
8)Deploy:在集成或者发布环境下执行,将最终版本的包拷贝到远程的repository,
使得其他的开发者或者工程可以共享。
3、site
Site:生成项目报告建立和发布项目站点,且包含以下几个重要的阶段
1)pre-site:生成项目站点之前需要完成的工作
2)site:生成项目站点文档
3)post-site:生成项目站点之后需要完成的工作
4)site-deploy:将项目站点发布到服务器
4、maven的常见指令
1.打包: mvn package
2.编译: mvn compile
3.清空: mvn clean(清除编译后目录,默认是target目录)
4.运行测试: mvn test
5.安装jar包到本地仓库中: mvn install
6.项目站点文档创建: mvn site
ps:如果在dos环境下是需要输入mvn +相应的指令的,但是如果你要是用eclipse的m2插件(就是单击右键之
后,run as-->maven build ...---->在Goals中输入:指令即可(不需要同时输入mvn))
二、maven的插件
1、导读
对于插件本身,为了能够复用代码,它往往能够完成多个任务。例如maven-dependency-plugin,它能基于
项目依赖做很多的事情,它能够分析项目依赖,帮助找出潜在的无用的依赖,他能列出依赖数,帮助分析依赖来
源,他能列出项目所有已解析的依赖等等,为每个这样的功能编写一个独立的插件显然是不可取的,因为这些任
务背后有很多的可以复用的代码,因此这些功能聚集在一个插件里,每个功能就是一个插件目标
ps:eclipse通过m2这个插件使得具有maven的功能,但是maven真正起作用是依靠其存在于本地仓库中
的各种插件的
2、插件绑定(内置的绑定)
Maven的生命周期与插件相互绑定,用以完成实际的构建任务。具体而言,是生命周期的阶段与
插件的目标相互绑定,以完成某个具体的构建任务
例如项目编译这一任务,它对应了default生命周期的compile这一阶段,而maven-compiler-plugin这一
插件的compile目标能够完成该任务。因此将它们绑定,就能实现项目编译的目的
简而言之:就是通过eclipse中的maven的插件与指令的绑定就可以实现不用再dos下输入指令了
3、eclipse中m2插件实现与maven功能绑定的项的功能都是什么意思
三、maven的高级用法
1、继承
maven的继承: 在面向对象中, 可以通过类继承实现复用. 在Maven中同样也可以创建
POM的父子结构, 通过在父POM中声明一些配置供子POM继承来实现复用与消除重复
1.创建一个父项目: Maven_Father,且在pom中的进行设定
<packaging>pom</packaging>
2.为了演示方便,在父项目的pom.xml中添加mysql驱动包的依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>3.1.13</version>
</dependency>
3.创建一个子项目: Maven_Son,且在pom.xml中实现maven的继承
<parent>
<groupId>com.rj.bd</groupId>
<artifactId>Maven_Father</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
4.为了演示maven子项目中的依赖不会因为继承了父项目而变的不能使用其他依赖了,为此我们再
添加一个额外的依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
5.复制一个dao实验一下能否查询出来数据
package com.rj.bd;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* 数据库连接对象
* @author HYZ
* @time 2021年1月7日
*/
public class Dao {
private String sDBDriver = "com.mysql.jdbc.Driver";
private String sConnStr = "jdbc:mysql://localhost:3306/testmaven?
useUnicode=true&characterEncoding=utf-8";//设置数据库名称为:pubs
private String username = "root"; //登录数据库用户名
private String password = "root"; //登录数据库密码
/**
* 建立连接
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName(sDBDriver); //指定JDBC数据库驱动程序
return DriverManager.getConnection(sConnStr,username,password);
}
/**
* 根据sql查询列表数据(查询一条),不支持预编译的方式
* @param sql
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public Map<String, Object> executeQueryForMap(String sql)throws
ClassNotFoundException, SQLException {
System.err.println("查询一条:"+sql);
Connection connect =this.getConnection();
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(sql);
List<Map<String, Object>> list = this.rsToList(rs);
if( !list.isEmpty() ){
return list.get(0);
}
this.releaseConnection(rs, stmt, connect);//关闭连接
return null;
}
/**
* 根据sql查询列表数据(查询一条),支持预编译的方式
* @param sql
* @param types
* @param values
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public Map<String, Object> executeQueryForMap(String sql, int[] types,Object[] values)
throws ClassNotFoundException, SQLException {
System.err.println("查询一条:"+sql);
this.print(values);
Connection connect = this.getConnection();
PreparedStatement pst = connect.prepareStatement(sql);
if( types != null ){
for(int i=0;i<types.length;i++){
switch( types[i] ){
case Types.VARCHAR:
pst.setString(i+1, String.valueOf( values[i] ) );
break;
case Types.INTEGER:
pst.setInt(i+1, Integer.parseInt( String.valueOf( values[i] ) ));
break;
}
}
}
ResultSet rs = pst.executeQuery();
List<Map<String, Object>> list = this.rsToList(rs);
if( !list.isEmpty() ){
return list.get(0);
}
this.releaseConnection(rs, pst, connect);
return null;
}
/**
* 根据sql查询列表数据,不支持预编译的方式
* @param sql
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public List<Map<String, Object>> executeQueryForList(String sql) throws
SQLException, ClassNotFoundException{
System.err.println("查询多条:"+sql);
Connection connect =this.getConnection();
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(sql);
List<Map<String, Object>> list = this.rsToList(rs);
this.releaseConnection(rs, stmt, connect);//关闭连接
return list;
}
/**
* 执行 增、删、改、等的操作,不支持预编译的方式
* @param sql
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public int executeUpdate(String sql) throws ClassNotFoundException, SQLException {
System.err.println("更新:"+sql);
Connection connect=this.getConnection();
Statement stmt=connect.createStatement();
int count=stmt.executeUpdate(sql);
this.releaseConnection(stmt, connect);//关闭连接
return count;
}
/**
* 根据sql查询列表数据,支持预编译的方式
* @param sql
* @param types
* @param values
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public List<Map<String, Object>> executeQueryForList
(String sql , int[] types,Object[] values) throws ClassNotFoundException, SQLException{
System.err.println("查询多条:"+sql);
this.print(values);
Connection connect = this.getConnection();
PreparedStatement pst = connect.prepareStatement(sql);
if( types != null ){
for(int i=0;i<types.length;i++){
switch( types[i] ){
case Types.VARCHAR:
pst.setString(i+1, String.valueOf( values[i] ) );
break;
case Types.INTEGER:
pst.setInt(i+1, Integer.parseInt( String.valueOf( values[i] ) ));
break;
}
}
}
ResultSet rs = pst.executeQuery();
List<Map<String, Object>> list = this.rsToList(rs);
this.releaseConnection(rs, pst, connect);
return list;
}
/**
* 预编译sql操作, 支持insert , update , delete 语句
* @param sql
* @param types
* @param values
* @return
* @throws SQLException
* @throws ClassNotFoundException
* @throws IOException
*/
public int executeUpdate(String sql , int[] types,Object[] values) throws
ClassNotFoundException, SQLException, IOException{
System.err.println("更新:"+sql);
this.print(values);
Connection connect = this.getConnection();
PreparedStatement pst = connect.prepareStatement(sql);
if( types != null ){
for(int i=0;i<types.length;i++){
switch( types[i] ){
case Types.VARCHAR:
pst.setString(i+1, String.valueOf( values[i] ) );
break;
case Types.INTEGER:
pst.setInt(i+1, Integer.parseInt( String.valueOf( values[i] ) ));
break;
case Types.BLOB:
InputStream in = new FileInputStream( (File)values[i] );
pst.setBinaryStream(i+1, in , in.available() );
break;
}
}
}
int count = pst.executeUpdate();
this.releaseConnection(pst, connect);
return count;
}
/**
* 查询一个整数,例如记录总数(不支持预编译)
* @param sql
* @param types
* @param values
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public int executeQueryForInt(String sql) throws
ClassNotFoundException, SQLException{
System.err.println("查询总数:"+sql);
Connection connect =this.getConnection();
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
return rs.getInt(1);
}
this.releaseConnection(rs, stmt, connect);
return 0;
}
/**
* 查询一个整数,例如记录总数(支持预编译)
* @param sql
* @param types
* @param values
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public int executeQueryForInt(String sql , int[] types,Object[] values)
throws ClassNotFoundException, SQLException{
System.err.println("查询总数:"+sql);
this.print(values);
Connection connect = this.getConnection();
PreparedStatement pst = connect.prepareStatement(sql);
if( types != null ){
for(int i=0;i<types.length;i++){
switch( types[i] ){
case Types.VARCHAR:
pst.setString(i+1, String.valueOf( values[i] ) );
break;
case Types.INTEGER:
pst.setInt(i+1, Integer.parseInt( String.valueOf( values[i] ) ));
break;
}
}
}
ResultSet rs = pst.executeQuery();
if(rs.next()){
return rs.getInt(1);
}
this.releaseConnection(rs, pst, connect);
return 0;
}
/**
* 将ResultSet中的结果包装成list中装Map的结构
* @param rs
* @return
* @throws SQLException
*/
private List<Map<String, Object>> rsToList( ResultSet rs ) throws SQLException{
List<Map<String, Object>> row = new ArrayList<Map<String, Object>>();
while (rs.next()) {
Map<String, Object> col = new HashMap<String, Object>();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
//System.out.println( rs.getMetaData().getColumnType(i) );
switch( rs.getMetaData().getColumnType(i) ){
case Types.VARCHAR:
col.put(rs.getMetaData().getColumnName(i), rs.getString(i));
break;
case Types.INTEGER:
col.put(rs.getMetaData().getColumnName(i), rs.getInt(i));
break;
case Types.BLOB:
InputStream in = rs.getBinaryStream(i);
col.put(rs.getMetaData().getColumnName(i), in );
break;
default:
col.put(rs.getMetaData().getColumnName(i), rs.getString(i));
break;
}
}
row.add(col);
}
return row;
}
/**
* 打印出所有的参数值
* @param values
*/
private void print(Object[] values){
if( values == null ) return;
System.out.println("参数值:\t---------------------");
for (int i = 0; i < values.length; i++) {
System.out.println( "\t["+i+"]=["+values[i]+"]" );
}
System.out.println("\t---------------------");
}
@SuppressWarnings("unused")
private void releaseConnection(Connection connect) throws SQLException{
try {
if (connect != null && !connect.isClosed()){
connect.close();
}
} catch (SQLException se){
System.out.println("Close the connection encounter error!\n" + se.getMessage());
throw new SQLException("关闭连接异常!");
}
}
private void releaseConnection(Statement stmt, Connection connect) throws SQLException{
try {
if (stmt != null){
stmt.close();
}
if (connect != null && !connect.isClosed()){
connect.close();
}
} catch (SQLException se){
System.out.println("Close the connection encounter error!\n" + se.getMessage());
throw new SQLException("关闭连接异常!");
}
}
private void releaseConnection(PreparedStatement pst, Connection connect)
throws SQLException{
try {
if (pst != null){
pst.close();
}
if (connect != null && !connect.isClosed()){
connect.close();
}
} catch (SQLException se){
System.out.println("Close the connection encounter error!\n" + se.getMessage());
throw new SQLException("关闭连接异常!");
}
}
private void releaseConnection(ResultSet rs, Statement stmt, Connection connect)
throws SQLException{
try {
if (rs != null){
rs.close();
}
if (stmt != null){
stmt.close();
}
if (connect != null && !connect.isClosed()){
connect.close();
}
} catch (SQLException se){
System.out.println("Close the connection encounter error!\n" + se.getMessage());
throw new SQLException("关闭连接异常!");
}
}
private void releaseConnection(ResultSet rs, PreparedStatement pst, Connection connect)
throws SQLException{
try {
if (rs != null){
rs.close();
}
if (pst != null){
pst.close();
}
if (connect != null && !connect.isClosed()){
connect.close();
}
} catch (SQLException se){
System.out.println("Close the connection encounter error!\n" + se.getMessage());
throw new SQLException("关闭连接异常!");
}
}
public static void main(String[] args) throws
ClassNotFoundException, SQLException, IOException {
Dao dao =new Dao();
List<Map<String, Object>> list = dao.executeQueryForList(" select * from user ");
System.out.println(list);
}
}
效果
2、聚合
Maven的聚合特性: (aggregation)能够使项目的多个模块聚合在一起构建
实际步骤