【问题标题】:Maven Assembly Plugins corrupts exe dllMaven 程序集插件损坏 exe dll
【发布时间】:2017-02-08 17:09:11
【问题描述】:

我正在尝试创建一个 zip 文件,其中包含我的 Java 项目的其他内容,一个 .NET x64 EXE + 它的清单 + 一个 .NET DLL 依赖项。看起来 maven 程序集插件损坏了 EXE 和 DLL。事实上,如果我在提取后尝试执行文件,我会得到“此应用程序无法在这台 PC 上运行”(无效的 x64 Windows 应用程序),但如果我复制原始文件,它们可以正常工作。

我试图用谷歌搜索一个没有成功的解决方案。我在 Maven 文件中遗漏了什么吗?

pom.xml 中的插件声明是:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>
            <descriptor>src/main/assembly/windows.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <finalName>${bundle.name}</finalName>
            </configuration>
        </execution>
    </executions>
</plugin>

windows.xml 的内容是:

<?xml version="1.0"?>
<assembly
  xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  <id>windows</id>

  <formats>
    <format>zip</format>
  </formats>

  <files>
    <file>
      <source>${launcher.dir}/GetMachineId.exe</source>
      <outputDirectory>bin/utils</outputDirectory>
      <destName>GetMachineId.exe</destName>
    </file>
    <file>
      <source>${launcher.dir}/GetMachineId.exe.config</source>
      <outputDirectory>bin/utils</outputDirectory>
      <destName>GetMachineId.exe.config</destName>
    </file>
    <file>
      <source>${launcher.dir}/MessagingToolkit.QRCode.dll</source>
      <outputDirectory>bin/utils</outputDirectory>
      <destName>MessagingToolkit.QRCode.dll</destName>
    </file>
  </files>
</assembly>

【问题讨论】:

  • 你能分享整个异常吗?
  • 来自事件查看器、Windows 日志 > 应用程序:The program or feature "\??\&lt;dir&gt;\GetMachineId.exe cannot start or run due to incompatibility with 64-bit versions of Windows. Please contact the software vendor to ask if a 64-bit Windows compatible version is available.(来源:Wow64 仿真层)。如果我用原始文件替换 GetMachineId.exe 并重试,我会收到 Microsoft .NET Framework 错误对话框报告...BadImageFormatException: Could not load file or assembly 'MessagingToolkit.QRCode...

标签: dll exe maven-assembly-plugin


【解决方案1】:

发现问题。

我实际上也在${launcher.dir} 上使用maven-resources-plugin 进行过滤。

从过滤中排除二进制文件解决了这个问题。

更新: 在我的 pom.xml 中,maven-resources-plugin 的配置如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>filtering-launcher-resources</id>
            <phase>process-resources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${launcher.dir}</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/launcher</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

src/main/launcher 错误地包含文本文件(实际上需要过滤)和二进制文件(GetMachineId.exeGetMachineId.exe.configMessagingToolkit.QRCore.dll)。

为了解决这个问题,我将这些二进制文件移到了不同​​的文件夹 (utils) 并像这样修改了程序集文件:

<?xml version="1.0"?>
<assembly
  xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  <id>windows</id>

  <formats>
    <format>zip</format>
  </formats>

  <files>
    <file>
      <source>utils/GetMachineId.exe</source>
      <outputDirectory>bin/utils</outputDirectory>
      <destName>GetMachineId.exe</destName>
    </file>
    <file>
      <source>utils/GetMachineId.exe.config</source>
      <outputDirectory>bin/utils</outputDirectory>
      <destName>GetMachineId.exe.config</destName>
    </file>
    <file>
      <source>utils/MessagingToolkit.QRCode.dll</source>
      <outputDirectory>bin/utils</outputDirectory>
      <destName>MessagingToolkit.QRCode.dll</destName>
    </file>
    <file>
        <source>${launcher.dir}/config.xml</source>
        <outputDirectory>bin</outputDirectory>
        <destName>config.xml</destName>
    </file>
  </files>
</assembly>

这种方式maven-resources-plugin 不会过滤二进制文件,因此它们不会被损坏。看起来过滤过程总是将文件视为文本文件,因此它会修改二进制文件以防止其执行。

另一个最近的策略可能是这个:https://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html

【讨论】:

  • 这里有同样的问题。我会很感激关于如何从过滤中排除二进制文件的简短句子。但是搜索机很快就会告诉我。
  • 我遇到了完全相同的问题:maven 正在以文本模式读取 dll,并且文件在被 maven-resources-plugin 复制到目标文件夹后被损坏。将 dll 移动到单独的文件夹并将过滤设置为 false 就可以了!
【解决方案2】:

我找到了一个更简单的解决方案,只需使用 resourcesexcludes 和 includes 标记来修改 pom.xml em> -> 资源.

例子:

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>**/*.dll</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <includes>
                <include>**/*.dll</include>
            </includes>
        </resource>
    </resources>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 2010-11-01
    • 2010-12-12
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多