Maven Launch4j 插件 - 如何配置以排除 .exe 使用的 jar?

Maven Launch4j Plugin - how to configure to exclude jars used by the .exe?

我正在开发一个应用程序,它接受一些参数,将一些数据发送到服务器,然后接收响应。它只会被另一个应用程序调用,并且只使用 CLI 界面。它的依赖关系由 Maven 解决。我需要将它打包成一个 .exe(遗留原因)。此外,我的应用程序的依赖项之一使用了一些 Bouncy Castle jar,它们无法使用 Maven 程序集插件正常打包,因为它们丢失了签名。

作为解决方案,suggested to me in another question that I use Maven's Launch4j plugin to both exclude the BC jars and build the exe. However, I can't for the life of me figure out how to configure it correctly. I don't even know where to begin to look for the way to exclude the BC jars and I have no idea how to cope with the classpath-related quirks of the plugin. The plugin's readme 由于我对 Maven 的有限掌握,它的文档太少了。

当前的 POM 既不构建 .exe,也不提供任何错误消息,这使得查明错误变得更加困难。 /target中.jar的大小和之前一样,暗示构建过程和添加插件之前似乎没有什么不同。

有人可以查看 POM 并提出一些改进建议以使应用程序按预期构建吗?非常感谢任何帮助,尤其是解释为什么将不正确的部分更改为原来的方式。

当前 POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>l4j-clui</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <dontWrapJar>true</dontWrapJar>
                            <headerType>console</headerType>
                            <jar>eet-demo-maven-1.0-SNAPSHOT-eet-sender.jar</jar>
                            <outfile>target\EETSender.exe</outfile>
                            <errTitle></errTitle>
                            <cmdLine></cmdLine>
                            <chdir>.</chdir>
                            <priority>normal</priority>
                            <downloadUrl>http://java.com/download</downloadUrl>
                            <supportUrl></supportUrl>
                            <stayAlive>true</stayAlive>
                            <restartOnCrash>true</restartOnCrash>
                            <manifest></manifest>
                            <icon></icon>
                            <singleInstance>
                                <mutexName>EETMutex</mutexName>
                                <windowTitle></windowTitle>
                            </singleInstance>
                            <classpath>
                                <mainClass>cz.tomasdvorak.eetdemo.Main</mainClass>
                                <postCp>\lib\bcprov-jdk15on-1.55.jar;\eet-demo-maven-1.0-SNAPSHOT-eet-sender.jar;\eet-demo-maven-1.0-SNAPSHOT.jar</postCp>
                            </classpath>
                            <jre>
                                <path></path>
                                <bundledJre64Bit>false</bundledJre64Bit>
                                <bundledJreAsFallback>false</bundledJreAsFallback>
                                <minVersion>1.6.0_1</minVersion>
                                <maxVersion></maxVersion>
                                <jdkPreference>preferJre</jdkPreference>
                                <runtimeBits>64/32</runtimeBits>
                            </jre>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>cz.tomasdvorak.eetdemo.Main</mainClass>
            </manifest>
        </archive>
        <descriptors>
            <descriptor>assembly.xml</descriptor>
         </descriptors>
    </configuration>
</plugin>

        </plugins>
    </build>

    <groupId>cz.tomasdvorak</groupId>
    <artifactId>eet-demo-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.github.todvora</groupId>
            <artifactId>eet-client</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

</project>

assembly.xml:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>eet-sender</id>
    <formats>
        <format>.jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/package</directory>
            <outputDirectory>/target</outputDirectory>
            <includes>
                <include>*.exe</include>
            </includes>
            <excludes>
                <exclude>org/bouncycastle/*.*</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>

那么,什么是正确的配置以确保最后有一个 .exe,当 运行 具有正确的参数时将执行其预期的功能?

正如您所说,BouncyCastle 的问题是使用 oracle 颁发的证书签名,以便它可以作为 JCE提供商。

然后你必须避免将 bcprov.jar jar(这是包含提供的 JCE 且必须对其进行签名的 jar)扭曲到具有所有依赖项的胖 jar 中,因为这样它会破坏BouncyCastle JCE jar 签名。

所以事情可能是为了避免将 bc-prov.jar 扭曲成 jar-with-dependencies 并告诉 install4j 从文件夹加载 jars 作为 java 执行的类路径资源。

为此,请执行以下操作:

添加 <dontWrapJar>true</dontWrap> 并使用:

<classpath>
    <mainClass>cz.tomasdvorak.eetdemo.Main</mainClass>
    <postCp>\lib\to\yourBc\bc-prov.jar;lib\to\yourJar\eet-demo-maven-1.0-SNAPSHOT-eet-sender.jar</postCp>
</classpath>

确保您的 eet-demo-maven-1.0-SNAPSHOT-eet-sender.jar 不包含 BouncyCastle JCE jar;您必须更改 pom.xml 以使 maven-assembly-plugin 指向您的 assembly.xml:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>cz.tomasdvorak.eetdemo.Main</mainClass>
            </manifest>
        </archive>
        <descriptors>
            <descriptor>/path/to/assembly.xml</descriptor>
         </descriptors>
    </configuration>
</plugin>

然后在您的 assembly.xml 中尝试排除 BouncyCastle 提供商 类:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>eet-sender</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
     <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <excludes>
                <exclude>org.bouncycastle:bcprov-jdk15on</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/package</directory>
            <outputDirectory>/target</outputDirectory>
            <includes>
                <include>*.exe</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

请注意,此 assembly.xml 基本上与 <descriptorRef>jar-with-dependencies</descriptorRef> 一样工作,但不包括作为 com.github.todvora:eet-client 依赖项的 org.bouncycastle:bcprov-jdk15on 工件。这样,您将拥有一个包含所有依赖项的胖 jar,但 BouncyCastle 提供程序除外,它必须单独加载,然后在 launch4j 的类路径执行中执行。

我希望这样能奏效。

在对 Michael-O 的建议进行一些试验之后,我得到了一个可以按要求执行的 pom。使用 "mvn package" 构建。结果是所需的 .exe 以及目标文件夹中的所有依赖项 jar。将它们放在 lib 文件夹中将是一个更标准和优雅的解决方案,但这是一个非常简单的应用程序,而且我的时间很紧,所以我将保持原样。获胜者是:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>l4j-clui</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <dontWrapJar>true</dontWrapJar>
                            <headerType>console</headerType>
                            <jar>eet-demo-maven-1.0-SNAPSHOT.jar</jar>
                            <outfile>target\EETSender.exe</outfile>
                            <errTitle></errTitle>
                            <cmdLine></cmdLine>
                            <chdir>.</chdir>
                            <priority>normal</priority>
                            <downloadUrl>http://java.com/download</downloadUrl>
                            <supportUrl></supportUrl>
                            <stayAlive>true</stayAlive>
                            <restartOnCrash>true</restartOnCrash>
                            <manifest></manifest>
                            <icon></icon>
                            <singleInstance>
                                <mutexName>EETMutex</mutexName>
                                <windowTitle></windowTitle>
                            </singleInstance>
                            <classpath>
                                <mainClass>cz.tomasdvorak.eetdemo.Main</mainClass>
                            </classpath>
                            <jre>
                                <path></path>
                                <bundledJre64Bit>false</bundledJre64Bit>
                                <bundledJreAsFallback>false</bundledJreAsFallback>
                                <minVersion>1.6.0_1</minVersion>
                                <maxVersion></maxVersion>
                                <jdkPreference>preferJre</jdkPreference>
                                <runtimeBits>64/32</runtimeBits>
                            </jre>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>cz.tomasdvorak.eetdemo.Main</mainClass>
            </manifest>
        </archive>
        <descriptors>
            <descriptor>assembly.xml</descriptor>
         </descriptors>
    </configuration>
</plugin>

</plugins>
    </build>

     <groupId>cz.tomasdvorak</groupId>
    <artifactId>eet-demo-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.github.todvora</groupId>
            <artifactId>eet-client</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

</project>