maven-assembly-plugin生成的tar与python的tarfile不兼容(bsdtar可用)

1 投票
1 回答
679 浏览
提问于 2025-04-17 21:50

在我创建的一个自动化部署流程中,我使用 maven-assembly-plugin 制作了一个 tar.gz 文件,然后用 Python 的 tarfile 模块来解压这个文件。

但是解压的时候出错了,错误信息如下:

追踪(最近的调用在最前面): 文件 "tarfile-assembly-testcase/extract_tar.py",第 20 行,在 <模块> tarfile.open(fileobj=f, mode='r:gz') 文件 "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py",第 1676 行,在 open return func(name, filemode, fileobj, **kwargs) 文件 "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py",第 1725 行,在 gzopen **kwargs) 文件 "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py",第 1703 行,在 taropen return cls(name, mode, fileobj, **kwargs) 文件 "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py",第 1572 行,在 __init__ self.firstmember = self.next() 文件 "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py",第 2335 行,在 next raise ReadError(str(e)) ReadError: 无效的头部

这个问题在压缩和未压缩的 tar 文件中都会出现。而且同样的文件在命令行中可以正常解压。我用 bsdtar 2.8.3tar (GNU tar) 1.26 测试过。我使用的是 python 2.7

请试试我在 GitHub 上发布的 示例。这是一个 Maven 项目,运行 mvn package 后会生成一个包含项目源代码的 tar.gz 文件(只有 pom.xml)。里面的 Python 脚本尝试使用 tarfile 来解压。

有没有什么办法让 maven-assembly-plugin 和 Python 的 tarfile 能够顺利配合使用呢?

1 个回答

3

我找到了问题所在。为了让后人能找到解决办法,我把我在这里(看看“版本固定”那一段)找到的解决方案复制过来了。

从2.4版本开始,maven-assembly-plugin引入了一个有问题的plexus-archiver版本。强制maven-assembly-plugin使用最新的plexus-archiver解决了这个问题。正如链接中的帖子所建议的,我还升级了plexus-io

下面是代码

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-archiver</artifactId>
            <version>2.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-io</artifactId>
            <version>2.0.10</version>
        </dependency>
    </dependencies>
</plugin>

撰写回答