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

时间:2016-10-12 11:50:00

标签: java maven jar pom.xml launch4j

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

作为一个解决方案,在我使用Maven的Launch4j插件来排除BC jar并构建exe的另一个问题中我已经suggested了。但是,我不能为我的生活弄清楚如何正确配置它。我甚至不知道从哪里开始寻找排除BC罐的方法,我不知道如何应对插件的类路径相关的怪癖。由于我对Maven的掌握有限,插件的readme太稀疏了。

POM目前既没有构建.exe,也没有提供任何错误消息,因此更难以查明错误。 / target中.jar的大小与之前的大小相同,暗示构建过程似乎与添加插件之前的大小不同。

有人可以看看POM并建议一些改进,以使应用程序按预期构建?任何帮助,特别是对不正确部件改变原因的解释,将不胜感激。

当前POM:

CREATE PROCEDURE

assembly.xml:

<?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>

现在,正确的配置是什么,以确保最终有一个.exe,当使用正确的参数运行时,它将执行其预期的功能?

2 个答案:

答案 0 :(得分:2)

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

然后你要避免在一个带有所有依赖关系的胖jar中扭曲bcprov.jar jar(这是包含JCE提供的jar并且必须要签名),因为这样就打破了< em> 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的依赖项。这样你将拥有一个包含所有依赖项的胖jar,不包括 BouncyCastle 提供程序,必须单独加载,然后在launch4j的类路径执行中加载。

我希望这种方式有效。

答案 1 :(得分:1)

经过一些试验以试验Michael-O的建议,我已经到了一个按要求执行的pom。使用&#34; mvn包&#34;构建。结果是所需的.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>