【问题标题】:Maven Launch4j Plugin - how to configure to exclude jars used by the .exe?Maven Launch4j 插件 - 如何配置排除 .exe 使用的 jars?
【发布时间】:2017-02-21 05:07:17
【问题描述】:

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

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

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,当使用正确的参数运行时,它会执行其预期的功能?

【问题讨论】:

  • 你为什么要使用胖罐子?你可以简单地把你所有的库放到lib,它就可以工作了。
  • 我将如何一步一步地做到这一点?项目中的任何地方都没有库文件夹。无论如何,我会下载所有库及其依赖项吗?以前从未这样做过,Maven 刚刚为我弄到了它们。

标签: java maven jar pom.xml launch4j


【解决方案1】:

在尝试了 Michael-O 的建议后,我得到了一个按要求执行的 pom。使用“mvn 包”构建。结果是所需的 .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>

【讨论】:

  • 正确,解决问题的方法很简单。
【解决方案2】:

正如您所说,BouncyCastle 的问题在于,它使用 oracle 颁发的证书进行签名,以便它可以作为 JCE 提供者工作。

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

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

为此,请执行以下操作:

添加&lt;dontWrapJar&gt;true&lt;/dontWrap&gt; 并同时使用:

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

我希望它可以这样工作。

【讨论】:

  • 我正在找出放置所有这些东西的正确位置,等等。另外,关于最后一点,我将如何将其写入 Maven?
  • 还有一点,插件的文档说它不需要 ,而是有一些不同的标签。我将如何使用这些来编写它? github.com/lukaszlenart/launch4j-maven-plugin/blob/master/src/…
  • 感谢您的回答。适应和测试,等等。
  • 好吧,还有两个问题: 1. 它抛出一个“no main manifest attribute”。我之前解决了这个问题,我可以复制解决方案并希望它会起作用,但我认为&lt;mainClass&gt; 标签可以解决这个问题。怎么没有呢?那个标签到底是做什么的? 2. 如何在 Maven 中最好地获取jar-with-dep.jar?有什么方法可以告诉它从 /lib 中获取除 BC 之外的所有 jar 并将它们打包成一个?
  • @Sargon1 最后,我直接尝试您的 pom.xml 以避免让您尝试/错误...使用我在上次编辑中显示的配置,它适用于我生成包含所有依赖项的胖 jar,不包括 bc-prov 的依赖项。
猜你喜欢
  • 2010-11-05
  • 2016-12-09
  • 1970-01-01
  • 2013-12-21
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-06
相关资源
最近更新 更多