在 IDEA 中导入外部库

有一个解析报文的 Java 程序, 里面封装好了可用的函数, 用来解析车辆上的原始报文, kafka 数据如下所示(rawData 字段为报文)。我们用它提供的 libs 来解析该报文。

1
{"keyId":"XXXXXXXXXXXXX","sampleTime":"1563348447814","msgType":"gbdata","dataList":{"msgType":"gbdata","vehicleModelCode":"1xd9","vehicleSeries":"A76","vin":"LLLLLLLLLLLLLL","rawData":"131302fe4c4e414132414131354b355331303236330101271307110f1b190102010100000000546d0eae26c063010f03e80000050006c3a1fd015f5e080601431018010c100801184a010344070000000000000000000801010eae26c0005c00015c100f101310111010101110101014100b10121010100f1008100e100c100c1010101210111013100e100c1010101310101015101310131015101410141013100e100c100e100d1010100f10131013101410131015100c100d100d100f100e100e101210161015101510431016101710131016101510151016100f101310131016101210101018101010151014101710141014101310131017100e10101013100e100f10111013101410141017101410181016101510161017090101002048474445454545454546444545454545454645464545494a4949464849484849bb","sampleTime":"1563348447814"}}

要在新的项目中使用它们的库, 需要做的是:

  • src 同级目录下, 创建一个 libs 目录, 把 xx.jar 复制到 libs 目录里面
  • src 同级目录下, 创建一个 out 目录, 用于 build Artifact 时存放最后的 jar 包文件
  • File -> Project Structure -> Artifacts -> + -> Extracted Directory -> 选择外部库.jar
  • File -> Project Structure -> Libraries -> + -> 选择 libs 目录和外部库.jar
  • File -> Project Structure -> Modules -> Dependencies -> 选择 libs

然后 Build -> Build Artifacts -> clean -> build

打开最后的 jar 包, 查看下里面是不是把外部库添加进来了。运行之前, 还要删除一下 META-INF 里面的一些东西:

1
zip -d ./result.jar 'META-INF/.SF' 'META-INF/.RSA' 'META-INF/*SF'