news 2026/4/16 15:54:41

Maven指定加载的类

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Maven指定加载的类

有的时候需要把单个类文件放到 Linux 环境上去运行,但是又不想引入 SpringBoot 相关的依赖以接口的形式来访问,如下介绍下使用 Maven 指定加载的类,打包到 Linux 环境运行

文章目录

      • 准备工作
      • maven-jar-plugin(写入 Main-Class)
      • maven-shade-plugin(打包 fat-jar)
      • 运行 & 打包(命令示例)

准备工作

  • 拿这篇文章的类【打破 OS 壁垒:Java 跨平台硬件信息采集的“终极方案”
    】中的 SystemInfoCollector.java 来举例
  • 首先需要把 main 方法给加上,在 SystemInfoCollector 中增加如下:
publicstaticvoidmain(String[]args){System.out.println("version: "+SystemInfoCollector.getVersion());System.out.println("systemName: "+SystemInfoCollector.getSystemName());System.out.println("localIp: "+SystemInfoCollector.getLocalIp());System.out.println("mac: "+SystemInfoCollector.getMac());System.out.println("cpuSerial: "+SystemInfoCollector.getCpuSerial());System.out.println("hardSerial: "+SystemInfoCollector.getHardSerial());System.out.println("drive: "+SystemInfoCollector.getDrive());System.out.println("fileSystem: "+SystemInfoCollector.getFileSystem());System.out.println("partitionSize: "+SystemInfoCollector.getPartitionSize());System.out.println("systemDisk: "+SystemInfoCollector.getSystemDisk());System.out.println("pcName: "+SystemInfoCollector.getPcName());System.out.println("pcSerial: "+SystemInfoCollector.getPcSerial());}
  • pom 加上 maven-jar-plugin(写入 Main-Class) 、maven-shade-plugin插件,如:
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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><parent><groupId>xxxx</groupId><artifactId>xxx</artifactId><version>xxx</version><relativePath>xxx/pom.xml</relativePath></parent><artifactId>xxx</artifactId><name>xxx</name><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId><scope>test</scope></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><scope>test</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.32</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.1.0</version><configuration><archive><manifest><!-- 这里指定类名 --><mainClass>com.xdr630.util.SystemInfoCollector</mainClass></manifest></archive></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.2.4</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><createDependencyReducedPom>false</createDependencyReducedPom></configuration></execution></executions></plugin></plugins></build></project>

maven-jar-plugin(写入 Main-Class)

注意:这是普通 JAR 写 manifest 的传统方式。但当同时用 maven-shade-plugin 生成一个 shaded (fat) jar 时,最终的 shaded JAR 的 manifest 可能不会自动来自 maven-jar-plugin 的设置——因为 shade 重新生成了 JAR(替换了 jar 内容)。需要在 shade 配置中显式设置或使用 transformer 写入 Main-Class。

maven-shade-plugin(打包 fat-jar)

  • createDependencyReducedPom=false:默认 shade 会生成一个 dependency-reduced-pom(去除那些已经合并进 fat-jar 的依赖),把它关掉可避免对后续部署/发布流程造成影响(但也可能在某些场景下导致多余依赖声明)。常见做法:保留或关闭取决于你是否要把生成的 POM 用作发布到仓库的 POM。

  • 缺点:当前配置没有设置 transformers(例如 ManifestResourceTransformer),因此生成的 shaded jar 可能没有 Main-Class。还未处理依赖冲突 (relocations) 和服务文件合并(META-INF/services 等)。

运行 & 打包(命令示例)

  • 本地打包:
mvn clean package
  • 运行:
java -jar xxx.jar
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 11:13:35

jina-embeddings-v3 现已在 Elastic Inference Service 上可用

作者&#xff1a;来自 Elastic Sean Handley 及 Ranjana Devaji 亲身体验 Elasticsearch&#xff1a;深入了解我们的示例 notebooks&#xff0c;开始免费的 cloud 试用&#xff0c;或立即在本地机器上试用 Elastic。 今天&#xff0c;我们很高兴在 Elastic Inference Service&a…

作者头像 李华
网站建设 2026/4/16 14:27:37

AI人体骨骼识别边缘计算部署:树莓派运行实测案例

AI人体骨骼识别边缘计算部署&#xff1a;树莓派运行实测案例 1. 引言&#xff1a;AI 人体骨骼关键点检测的现实意义 随着人工智能在计算机视觉领域的深入发展&#xff0c;人体姿态估计&#xff08;Human Pose Estimation&#xff09;已成为智能健身、动作捕捉、人机交互和安防…

作者头像 李华
网站建设 2026/4/15 19:53:56

UVC协议驱动开发中端点配置图解说明

UVC驱动开发中的端点配置&#xff1a;从协议到实战的完整图解指南你有没有遇到过这样的场景&#xff1f;摄像头插上电脑&#xff0c;系统识别为“未知设备”&#xff0c;或者虽然能枚举成功&#xff0c;但一打开OBS或VLC就卡顿、花屏甚至崩溃。问题往往不在于传感器或多线程处理…

作者头像 李华
网站建设 2026/4/16 12:25:47

AI行为识别:MediaPipe Pose应用实例

AI行为识别&#xff1a;MediaPipe Pose应用实例 1. 引言&#xff1a;AI人体骨骼关键点检测的现实价值 随着人工智能在计算机视觉领域的深入发展&#xff0c;人体姿态估计&#xff08;Human Pose Estimation&#xff09;已成为智能交互、运动分析、安防监控和虚拟现实等场景中…

作者头像 李华
网站建设 2026/4/16 12:25:24

如何让SillyTavern提示词不再“答非所问“?

如何让SillyTavern提示词不再"答非所问"&#xff1f; 【免费下载链接】SillyTavern LLM Frontend for Power Users. 项目地址: https://gitcode.com/GitHub_Trending/si/SillyTavern 你是否遇到过这样的场景&#xff1a;明明精心设计了提示词&#xff0c;AI却…

作者头像 李华
网站建设 2026/4/16 12:25:52

38种语言自由切换:HY-MT1.5-1.8B翻译模型使用避坑指南

38种语言自由切换&#xff1a;HY-MT1.5-1.8B翻译模型使用避坑指南 1. 引言 在全球化加速的今天&#xff0c;高质量、低延迟的机器翻译已成为企业出海、内容本地化和跨语言协作的核心基础设施。腾讯混元团队推出的 HY-MT1.5-1.8B 模型&#xff0c;作为一款参数量仅1.8B&#x…

作者头像 李华