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

标签 java maven jar pom.xml launch4j

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

作为解决方案,已经suggested在另一个问题中,我使用 Maven 的 Launch4j 插件来排除 BC jar 并构建 exe。但是,我终其一生都无法弄清楚如何正确配置它。我什至不知道从哪里开始寻找排除 BC jar 的方法,我也不知道如何处理插件的类路径相关的怪癖。插件的 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>

程序集.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 从文件夹加载 jar 作为 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>但不包括 org.bouncycastle:bcprov-jdk15on作为您的 com.github.todvora:eet-client 的依赖项的 Artifact .这样,您将拥有一个包含所有依赖项的胖 jar,但 BouncyCaSTLe 提供程序除外,它必须单独加载,然后在 launch4j 的类路径执行中。

我希望这种方式有效。

关于java - Maven Launch4j 插件 - 如何配置以排除 .exe 使用的 jar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39997888/

相关文章:

java - 创建标记时 Maven 发布插件失败

maven - 如何在 pom.xml 中定义条件属性?

android - 图书馆与图书馆项目与外部图书馆之间的区别

java - 将 JAR 资源作为 FileStream 打开

java - Maven 构建因缺少依赖项而失败 - 初学者

java - 使用正则表达式删除java中转义的unicode字符串

java - SLF4J:使用 log4j 时无法加载类 "org.slf4j.impl.StaticLoggerBinder"错误

java - 从具有特殊格式的 JAR 中提取类名

Java 8 检查其键的 HashMap 是否与包含键的列表相等,并返回它们相等的 HashMap 值

eclipse - m2eclipse 问题 ArtifactTransferException - 但是 pom.xml 从命令行编译/安装