Maven指定加载的类

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

文章目录

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

准备工作

java 复制代码
public static void main(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 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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 等)。

运行 & 打包(命令示例)

  • 本地打包:
bash 复制代码
mvn clean package
  • 运行:
bash 复制代码
java -jar xxx.jar
相关推荐
好好沉淀1 天前
ES 脚本核心语法:ctx._source [‘group_id‘]
java·elasticsearch·script
李慕婉学姐1 天前
【开题答辩过程】以《基于Spring Boot的疗养院理疗管理系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring boot·后端
tb_first1 天前
SSM速通2
java·javascript·后端
qq_12498707531 天前
基于协同过滤算法的运动场馆服务平台设计与实现(源码+论文+部署+安装)
java·大数据·数据库·人工智能·spring boot·毕业设计·计算机毕业设计
大飞哥~BigFei1 天前
自定义注解记录接口切面log日志入库优化
java
人道领域1 天前
javaWeb从入门到进阶(maven高级进阶)
java·spring·maven
一路向北⁢1 天前
Spring Boot 3 整合 SSE (Server-Sent Events) 企业级最佳实践(一)
java·spring boot·后端·sse·通信
风象南1 天前
JFR:Spring Boot 应用的性能诊断利器
java·spring boot·后端
爱吃山竹的大肚肚1 天前
微服务间通过Feign传输文件,处理MultipartFile类型
java·spring boot·后端·spring cloud·微服务
_周游1 天前
Java8 API文档搜索引擎_使用内存缓冲区优化
java·搜索引擎·intellij-idea