MANIFEST.MF属性读写

6 篇文章 0 订阅

本案例完整演示项目 https://download.csdn.net/download/cs4380/10835284

一、maven打包写入MANIFEST.MF属性

     本案例采用maven写入,可以采用其他工具写入

     在pom.xml文件添加打包插件,早manifestEntries标签中添加自定标签和值

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>2.3.1</version>
	<configuration>
		<archive>
			<manifestEntries>
				<projectName>
					项目名称
				</projectName>
				<version>
					v1.0
				</version>
			</manifestEntries>
		</archive>
	</configuration>
</plugin>

    使用maven打包后获取xxxx.jar文件,打包后MANIFEST.MF新增的属性值

二、Java读取MANIFEST.MF属性值

      获取当前jar中MANIFEST.MF文件的属性值,本案例仅提供读取工具类,通过io流获取字符串,获取字符串后解析得到配置。

import org.springframework.util.ClassUtils;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * Manifest 文件操作工具.
 */
public class ManifestUtil {

    /**
     * 获取Manifest文件配置属性值
     *
     * @return
     * @throws IOException
     */
    public static Map<String, String> getManifestProperty() {
        Map<String, String> property = new HashMap<>();
        JarFile jarFile = null;
        String content = "";
        try {
            // 获取jar的运行路径,因linux下jar的路径为”file:/app/.../test.jar!/BOOT-INF/class!/“这种格式,所以需要去掉”file:“和”!/BOOT-INF/class!/“
            String jarFilePath = ClassUtils.getDefaultClassLoader().getResource("").getPath().replace("!/BOOT-INF/classes!/", "");
            if (jarFilePath.startsWith("file")) {
                jarFilePath = jarFilePath.substring(5);
            }
            // 通过JarFile的getJarEntry方法读取META-INF/MANIFEST.MF
            jarFile = new JarFile(jarFilePath);
            JarEntry entry = jarFile.getJarEntry("META-INF/MANIFEST.MF");
            // 如果读取到MANIFEST.MF文件内容,则转换为string
            if (entry != null) {
                StringWriter writer = new StringWriter();
                FileCopyUtils.copy(new InputStreamReader(jarFile.getInputStream(entry), "utf-8"), writer);
                jarFile.close();
                content = writer.toString();
            } else {
                return property;
            }
        } catch (Exception ex) {
            return property;
        } finally {
            try {
                if (jarFile != null) {
                    jarFile.close();
                }
            } catch (IOException e) {
                return property;
            }
        }
        return getProperty(content);
    }

    /**
     * 通过字符解析,MANIFEST.MF 文件中的属性值
     *
     * @param content MANIFEST.MF 文件中的字符串
     * @return
     */
    private static Map<String, String> getProperty(String content) {
        Map<String, String> result = new HashMap<>();
        if ("".equals(content.trim())) {
            return result;
        }
        String[] propertys = content.split("\n");
        if (propertys == null || propertys.length == 0) {
            return result;
        }
        String[] temp;
        try {
            for (int i = 0; i < propertys.length; i++) {
                temp = propertys[i].split(":");
                if (temp.length == 2) {
                    result.put(temp[0].trim(), temp[1].trim());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

三、功能测试

     通过 java -jar   xxx.jar 命令执行jar包,输出配置项数据。只有打包后才会生成MANIFEST.MF文件,所有在项目中无法测试

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值