使用代码打包一个maven项目

news/2024/7/7 19:24:54

平常开发使用maven工具将一个maven项目打包成war包或者jar包部署,做项目为了实现代码的自动化部署,需要用代码自动完成这些操作。
下面记录代码实现过程

引入maven打包工具依赖

 <!--        maven调用打包工具-->
        <dependency>
            <groupId>org.apache.maven.shared</groupId>
            <artifactId>maven-invoker</artifactId>
            <version>3.0.1</version>
        </dependency>

编写工具类


import org.apache.maven.shared.invoker.*;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;

/**
 * maven项目打包工具类
 */
public class MavenBuild {
    /**
     * 打包项目 会在项目根目录下生成target文件
     *
     * @param projectRootPath 要打包的项目的根路径
     * @param mavenPath       要使用的maven路径
     */
    public static void build(String projectRootPath, String mavenPath) {
        InvocationRequest request = new DefaultInvocationRequest();
        //设置根路径
        request.setBaseDirectory(new File(projectRootPath));
        //设置要执行的maven操作
        request.setGoals(Arrays.asList("clean", "package"));
        //调用工具
        Invoker invoker = new DefaultInvoker();
        //设置要使用的maven路径
        invoker.setMavenHome(new File(mavenPath));
        try {
            //开始执行maven构建
            InvocationResult res = invoker.execute(request);
            //返回状态码
            if (res.getExitCode() == 0) {
                //打包成功
            } else {
                //打包失败
            }
        } catch (MavenInvocationException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取打包之后的jar包或者war包
     *
     * @param projectRootPath 项目根路径
     * @return
     */
    public static File getPackage(String projectRootPath) {
        //读取根路径
        File rootFile = new File(projectRootPath);
        //读取target目录
        File targetFile = new File(rootFile, "target");
        //根据正则匹配查找jar包或者war包
        File[] files = targetFile.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                String grep = ".*\\.(war|jar)";
                return name.matches(grep);
            }
        });
        //返回找到的包 否则返回null
        return files != null ? files[0] : null;
    }
}

http://www.niftyadmin.cn/n/599446.html

相关文章

Scala用特征来实现混入(mix-in)式的多重继承

2019独角兽企业重金招聘Python工程师标准>>> Scala里相当于Java接口的是特征(Trait)。Trait的英文意思是特质和性状&#xff08;本文称其为特征&#xff09;&#xff0c;实际上他比接口还功能强大。与接口不同的是&#xff0c;它还可以定义属性和方法的实现。Scala中…

spring 配置文件 数据库引入

一.mysql数据库引入 <bean id"dataSource" class"com.mchange.v2.c3p0.ComboPooledDataSource"> <property name"driverClass" value"com.mysql.jdbc.Driver" /> <property name"jdbcUrl" value"jdbc:m…

PHP IDE,Visual Studio还是Eclipse?

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/46860.html" frameborder"0" width"468" scrolling"no" height"60"></iframe>每个程序员都有…

MSN中英双语聊天机器人上线,邀请测试

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/46860.html" frameborder"0" width"468" scrolling"no" height"60"></iframe>我业余做的一个…

httpwebrequest 请求压缩,接受压缩的字符流请求

请看图&#xff0c;客户端和服务端都使用gzip压缩。 客户端参数以json字符串形式gzip压缩&#xff0c;以二进制流发送到服务端&#xff1b;服务端接收到请求&#xff0c;解压缩gzip后&#xff0c;进行业务逻辑处理&#xff1b;处理完后在将返回值以json形式&#xff0c;gzip压缩…

weblab需求

需求文档 weblab需求文档 web端、后端、idea端、部署端 web端主要功能用于信息管理和维护 web端需要分学生和教师端 认证授权 采用springboot和redis,签发token 配置独立的redis服务器用于存放用户session 目前redis服务器地址&#xff1a;123.56.220.39:6379 root 911ABCabc …

小大端

2019独角兽企业重金招聘Python工程师标准>>> intel x86是小端(高位存放在高地址&#xff0c;和我们逻辑是一样的)&#xff0c;网路字节是大端&#xff08;低位放在高地址&#xff09; bool isBigEndian() {unsigned int x 0x01020304; // 低字节0x04return *((unsi…

weblab-task

任务 文件服务器班级管理、邀请和加入 项目的创建和管理 建立项目 队伍 一开始 每个人单独 队长可以把这git地址发给他的队员 老师可以筛选只看队长 组织管理 组织表、用户组织表、组织通知作业表 增删改查 在一个组织里&#xff1a;学生列表&#xff0c;查看学生信息&…