【问题标题】:Maven: unzip os-based zip file to WEB-INF/libMaven:将基于 os 的 zip 文件解压缩到 WEB-INF/lib
【发布时间】:2020-05-25 14:06:57
【问题描述】:

我有 2 个 .zip 文件:shared-lib-windows.zip 和 shared-lib-linux.zip。 maven 依赖 jar 在 WEB-INF/lib/ 下查找 shared-lib 目录,并期望 .exe 或 .out 文件存在于 shared-lib 目录中。

如何在我的 pom.xml 中设置 .zip 文件,以便拾取适当的 zip 文件,解压缩到重命名的(共享库,不带 -os)目录?

我已经将 maven 依赖项(jar)设置为:

    <profiles>
        <profile>
            <id>Linux</id>
            <dependencies>
                <dependency>
                    <groupId>com.dependency</groupId>
                    <artifactId>depedency-linux</artifactId>
                    <version>0.0.9</version>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>Windows</id>
            <dependencies>
                <dependency>
                    <groupId>com.dependency</groupId>
                    <artifactId>dependency-windows</artifactId>
                    <version>0.0.9</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

现在我只想让 Maven 从 src/main/resources 或 .m2/.../ 中提取 .zip 文件并施展魔法!

我查看了建议使用 maven-ant-plugin 的答案,但有人提到应该是 uncool

[编辑:添加信息] 我有一个打包为 .war 并部署到 Tomcat 的 Maven Web 项目。 此外,.exe/.out 文件是第 3 方依赖项,大小分别为 120/150 MB。由于我的 git 不支持大文件并且二进制文件不会更新,因此我将其压缩并添加到我的 Git 管理的 Maven 网络项目中。

【问题讨论】:

  • 你有一个WEB-INF目录?这是否意味着您有一个 Web 应用程序?哪个部署到哪里?雄猫?
  • 为什么 zip 文件是 Maven 工件??
  • @khmarbaise 和 Thorbjørn Ravn Andersen,我已经用要求的信息更新了问题。

标签: java maven zip pom.xml


【解决方案1】:

我浏览了有关 Maven 配置文件、阶段和属性的官方文档。 我想出了一个使用maven-antrun-plugin 的解决方案。我还在寻找不依赖它的解决方案。

在 Linux 上运行以下 pom.xml:mvn package -P Linux

<profiles>
    <profile>
        <id>Linux</id>
        <properties>
            <binary.zip>${project.basedir}/src/main/resources/binary-linux.zip</binary.zip>
            <unzip.folder>${project.build.directory}/${project.build.finalName}/WEB-INF/lib/binary</unzip.folder>
        </properties>
        <dependencies>
            <dependency>
                <groupId>com.dependency</groupId>
                <artifactId>depedency-linux</artifactId>
                <version>0.0.9</version>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>Windows</id>
        <properties>
            <binary.zip>${project.basedir}/src/main/resources/binary-windows.zip</binary.zip>
            <unzip.folder>${project.build.directory}/${project.build.finalName}/WEB-INF/lib/binary</unzip.folder>
        </properties>
        <dependencies>
            <dependency>
                <groupId>com.dependency</groupId>
                <artifactId>dependency-windows</artifactId>
                <version>0.0.9</version>
            </dependency>
        </dependencies>
    </profile>
</profiles>
...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>unzip-binary</id>
            <phase>prepare-package</phase>
            <configuration>
                <tasks>
                    <echo message="unzip the binary" />
                    <unzip src="${binary.zip}" dest="${unzip.folder}" />
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这会将 .zip 解压缩到 ..WEB-INF/lib/binary 目录(在准备包阶段),然后使用 maven-war-plugin 插件将其归档到 .war(在包阶段)。

【讨论】:

    猜你喜欢
    • 2012-02-14
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    相关资源
    最近更新 更多