有的时候需要把单个类文件放到 Linux 环境上去运行,但是又不想引入 SpringBoot 相关的依赖以接口的形式来访问,如下介绍下使用 Maven 指定加载的类,打包到 Linux 环境运行
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());
}<?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>注意:这是普通 JAR 写 manifest 的传统方式。但当同时用 maven-shade-plugin 生成一个 shaded (fat) jar 时,最终的 shaded JAR 的 manifest 可能不会自动来自 maven-jar-plugin 的设置——因为 shade 重新生成了 JAR(替换了 jar 内容)。需要在 shade 配置中显式设置或使用 transformer 写入 Main-Class。
mvn clean packagejava -jar xxx.jar原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。