如何利用大模型快速了解项目的技术栈?

前言

最近啊,闲来无事,想着看看现在做的项目都有用到哪些技术?

翻来翻去,我觉着应该从 pom.xml 入手!

乍一看,整个项目 70+ 个 pom.xml,我嘞个天呐,一个一个看的话,这要看到什么时候?

于是乎,动了动脑瓜子,想着要不写个程序直接整出来看?

介绍

hutool:Hutool是一个Java工具包,提供了丰富的功能和便捷的API,用于简化Java开发。其主要包括:数据类型转换、日期处理、文件操作、加密解密、编码解码、网络请求等模块,能够极大地提高开发效率和代码质量。

maven-model:maven-model是Maven的核心API之一,用于定义和处理Maven项目对象模型(POM)。它提供了访问和操作POM的API,包括依赖关系、构建生命周期、插件等。

easyexcel:EasyExcel是一款基于Java的简单、高效、处理大量数据的 Excel 读写库,支持复杂的 Excel 数据处理和存储。它采用了全新的注解解析模式,能够快速实现对 Excel 数据的读取和写入,并且提供了丰富的API和示例代码,方便开发者进行二次开发。同时,EasyExcel还支持大数据量的批量导入导出,能够有效地提高数据处理效率。

依赖

<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-all</artifactId>
	<version>5.8.25</version>
</dependency>
<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-model</artifactId>
    <version>3.6.3</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.3.3</version>
</dependency>

代码

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Test {

	// 使用 Map 和 Set 便于去重
    static Map<String, Set<String>> dependencyMap = new HashMap<>();

    public static void main(String[] args) {
        // 创建 Maven POM 文件读取器,用于解析 POM 文件
        MavenXpp3Reader reader = new MavenXpp3Reader();
        // 获取当前目录及子目录中名称为 pom.xml 的所有文件
        List<File> files = FileUtil.loopFiles("E:\\IdeaProjects\\RuoYi-Cloud", file -> "pom.xml".equals(file.getName()));

        for (File file : files) {
            try (FileInputStream inputStream = new FileInputStream(file)) {
                // 读取文件内容
                Model model = reader.read(inputStream);
                /*
                    这里之所以既要读取 dependencyManagement 标签内容又要读取 dependencies 中的内容,
                    是因为有的项目的 pom.xml 两个标签都有可能存在(即并列存在)
                 */
                // 获取 pom.xml 文件中的 dependencyManagement 标签内容
                DependencyManagement dependencyManagement = model.getDependencyManagement();
                if (dependencyManagement != null) {
                    // 将 dependencyManagement 中的依赖添加到 map 中
                    addDependency(dependencyManagement.getDependencies());
                }
                // 将 dependencies 中依赖添加到 map 中
                addDependency(model.getDependencies());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        System.out.println(JSONUtil.toJsonStr(dependencyMap));
    }

    private static void addDependency(List<Dependency> dependencies) {
        for (Dependency dependency : dependencies) {
            String groupId = dependency.getGroupId();
            String artifactId = dependency.getArtifactId();

            if (dependencyMap.containsKey(groupId)) {
                // 存在则直接添加
                dependencyMap.get(groupId).add(artifactId);
            } else {
                // 不存在则创建一个 Set
                dependencyMap.put(groupId, CollectionUtil.newHashSet(artifactId));
            }

        }
    }
}

后续

上面把依赖都揪出来了,但是直接输出到控制台貌似有点不好看?

Emmm,要查还得一个个复制,有没有其他法子?

诶,有啦有啦,最近大模型不是很火?

我也凑凑热闹!

那直接输出到控制台?不好看叭!咋办捏?

有了,就直接塞到 Excel 里面吧!

这里使用的是百度的大模型,需要先创建应用,获取 API Key 和 Secret Key
具体操作流程查看官方文档:应用接入

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class BaiduUtils {

    private BaiduUtils() {}

    public static final String host = "https://aip.baidubce.com";

    /**
     * API Key
     */
    private static final String clientId = "";

    /**
     * Secret Key
     */
    public static final String clientSecret = "";


    public static String askQuestion(String content) {
        String path = "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie_bot_8k?access_token=" + getAccessToken();
        Map<String, Object> messageMap = new HashMap<>();
        messageMap.put("role", "user");
        messageMap.put("content", content);

        Map<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("messages", Collections.singletonList(messageMap));
        bodyMap.put("disable_search", false);
        bodyMap.put("enable_citation", false);

        JSONObject jsonObject = JSONUtil.parseObj(request(path, JSONUtil.toJsonStr(bodyMap)));
        return (String) jsonObject.get("result");
    }


    public static String getAccessToken() {
        String path = "/oauth/2.0/token?client_id=" + clientId + "&client_secret=" + clientSecret + "&grant_type=client_credentials";
        JSONObject jsonObject = JSONUtil.parseObj(request(path, null));
        return (String) jsonObject.get("access_token");
    }


    private static String request(String path, String body) {
        String url = host + path;
        HttpResponse response = HttpRequest.post(url)
                .body(body)
                .execute();
        return response.body();
    }

}
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

import java.io.File;
import java.io.FileInputStream;
import java.util.*;

@Slf4j
public class Test {

    static Map<String, Set<String>> dependencyMap = new HashMap<>();

    public static void main(String[] args) {
        // 创建 Maven POM 文件读取器,用于解析 POM 文件
        MavenXpp3Reader reader = new MavenXpp3Reader();
        // 获取当前目录及子目录中名称为 pom.xml 的所有文件
        List<File> files = FileUtil.loopFiles("E:\\IdeaProjects\\RuoYi-Cloud", file -> "pom.xml".equals(file.getName()));

        for (File file : files) {
            try (FileInputStream inputStream = new FileInputStream(file)) {
                // 读取文件内容
                Model model = reader.read(inputStream);
                /*
                    这里之所以既要读取 dependencyManagement 标签内容又要读取 dependencies 中的内容,
                    是因为有的项目的 pom.xml 两个标签都有可能存在(即并列存在)
                 */
                // 获取 pom.xml 文件中的 dependencyManagement 标签内容
                DependencyManagement dependencyManagement = model.getDependencyManagement();
                if (dependencyManagement != null) {
                    // 将 dependencyManagement 中的依赖添加到 map 中
                    addDependency(dependencyManagement.getDependencies());
                }
                // 将 dependencies 中依赖添加到 map 中
                addDependency(model.getDependencies());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        int index = 0;
        String answer;
        List<DependencyModel> dependencyModels = new ArrayList<>();
        // 通过百度大模型获取依赖介绍
        for (Map.Entry<String, Set<String>> entry : dependencyMap.entrySet()) {
            for (String component : entry.getValue()) {
                answer = BaiduUtils.askQuestion("简单介绍一下(50-100字)" + entry.getKey() + ":" + component);
                dependencyModels.add(new DependencyModel(entry.getKey(), component, answer));
            }
            index++;
            log.info("当前执行到{}/{}", index, dependencyMap.size());
        }

        EasyExcel.write("E:\\Desktop\\projectDependency.xlsx", DependencyModel.class)
                // 设置表头
                .head(DependencyModel.class)
                // 设置文件类型
                .excelType(ExcelTypeEnum.XLSX)
                // 设置表名称
                .sheet("依赖信息")
                .doWrite(dependencyModels);
    }

    @Data
    @AllArgsConstructor
    // 设置列宽
    @ColumnWidth(20)
    // 设置自动换行、居中对齐
    @ContentStyle(wrapped = BooleanEnum.TRUE, verticalAlignment = VerticalAlignmentEnum.CENTER,
            horizontalAlignment = HorizontalAlignmentEnum.CENTER)
    static class DependencyModel {
        /**
         * 组织名称,即 groupId
         */
        @ExcelProperty("组织名称")
        private String org;
        /**
         * 组件名称,即 artifactId
         */
        @ExcelProperty("组件名称")
        private String component;
        /**
         * 描述
         */
        @ExcelProperty("描述")
        @ColumnWidth(150)
        @ContentStyle(wrapped = BooleanEnum.TRUE, verticalAlignment = VerticalAlignmentEnum.CENTER,
                horizontalAlignment = HorizontalAlignmentEnum.LEFT)
        private String desc;
    }


    private static void addDependency(List<Dependency> dependencies) {
        for (Dependency dependency : dependencies) {
            String groupId = dependency.getGroupId();
            String artifactId = dependency.getArtifactId();

            if (dependencyMap.containsKey(groupId)) {
                // 存在则直接添加
                dependencyMap.get(groupId).add(artifactId);
            } else {
                // 不存在则创建一个 Set
                dependencyMap.put(groupId, CollectionUtil.newHashSet(artifactId));
            }

        }
    }
}

导出效果

在这里插入图片描述

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

正则表达式1951

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值