java - 使用 launch4j 和 maven 包装 java 命令行应用程序

标签 java maven launch4j

我想使用 maven 和 launch4j 将一个基于 java 的命令行应用程序及其所有依赖项包装到一个 *.exe 文件中。

现在我已经阅读了所有类似的问题,例如 this onethis但我无法让它工作。

任何人都可以提供一个简单的 pom.xml 片段,如何通过所有需要的依赖项来实现这一点。 顺便问一下,我应该在 Eclipse 运行配置中运行什么 Maven 构建目标?

这是我从 SO 复制的内容:

<!-- Launch4j -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached> <!-- Make the shaded artifact not the main one -->
                <shadedClassifierName>shaded</shadedClassifierName> <!-- set the suffix to the shaded jar -->
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.bluestemsoftware.open.maven.plugin</groupId>
            <artifactId>launch4j-plugin</artifactId>
            <version>1.5.0.0</version>
            <executions>

                <!-- Command-line exe -->
                <execution>
                    <id>l4j-cli</id>
                    <phase>package</phase>
                    <goals>
                        <goal>launch4j</goal>
                    </goals>
                    <configuration>
                        <headerType>console</headerType>
                        <outfile>target/importer.exe</outfile>
                        <jar>target/${artifactId}-${version}-shaded.jar</jar> <!-- 'shaded' is the value set on shadedClassifierName above -->
                        <errTitle>App Err</errTitle>
                        <classPath>
                            <mainClass>${mainClass}</mainClass>
                        </classPath>                
                        <jre>
                            <minVersion>1.5.0</minVersion>
                            <maxVersion>1.6.0</maxVersion>
                            <initialHeapSize>128</initialHeapSize>
                            <maxHeapSize>1024</maxHeapSize>
                        </jre>
                    </configuration>
                </execution>
            </executions>
        </plugin>  

当我在 Eclipse 中运行 launch4j:launch4j 目标时(如果这是正确的?)我得到:

Failed to execute goal org.bluestemsoftware.open.maven.plugin:launch4j-plugin:1.5.0.0:launch4j (default-cli) on project importer: The parameters 'headerType', 'jre' for goal org.bluestemsoftware.open.maven.plugin:launch4j-plugin:1.5.0.0:launch4j are missing or invalid -> [Help 1]

也许我只是启动了错误的目标......

最佳答案

德雷克!

我可以生成一个 .exe 文件,其配置与您的配置非常相似。遵循我的整个 pom:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>

    <properties>
        <mainClass>foo.App</mainClass>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <shadedArtifactAttached>true</shadedArtifactAttached> <!-- Make the shaded artifact not the main one -->
                    <shadedClassifierName>shaded</shadedClassifierName> <!-- set the suffix to the shaded jar -->
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <executions>
                    <!-- Command-line exe -->
                    <execution>
                        <id>l4j-cli</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <headerType>console</headerType>
                            <outfile>target/importer.exe</outfile>
                            <jar>target/${artifactId}-${version}-shaded.jar</jar> <!-- 'shaded' is the value set on shadedClassifierName above -->
                            <classPath>
                                <mainClass>${mainClass}</mainClass>
                            </classPath>
                            <jre>
                                <minVersion>1.5.0</minVersion>
                                <initialHeapSize>128</initialHeapSize>
                                <maxHeapSize>1024</maxHeapSize>
                            </jre>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>LATEST</version>
        </dependency>
    </dependencies>

</project>

我将插件的 groupId 和 artifactId 更改为 vorburger 的,但 alakai 的版本也应该可以工作。确保:

  1. 您配置了正确的 mainClass
  2. 您至少声明了一个依赖项(我过去在一些“非常小的” Artifact/零依赖项 Artifact 方面遇到了一些麻烦)
  3. 您可以从存储库下载插件

我刚刚使用简单的 Maven 原型(prototype)测试了这个 pom,所以我看不出有什么理由不能在你的机器上运行。如果您有任何问题,请在这里提问。

要生成 .exe 文件,我需要在终端上运行“mvn clean package”,或者在 Eclipse 中,右键单击您的项目,“运行为...”>“Maven 构建...”并在目标文本字段中输入“clean package”。

关于java - 使用 launch4j 和 maven 包装 java 命令行应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9818721/

相关文章:

java - 将部署的 jar 引用为 Maven 依赖项

maven - Atlassian SDK 错误

java - 是否可以配置 maven 来编译生成的源代码而不使用插件?

java.lang.UnsupportedOperationException : Not implemented by the DistributedFileSystem FileSystem implementation during FileSystem. 获取()

java - JSCH session 连接问题

maven - 使用Maven的liquibase插件错误

java - 打包到 jar/exe 后 SIGAR API 不工作

bundle - 部署-Java应用程序

ant - 使用 sign4j 和 jsign 在 ant 中签署 launch4j 可执行文件

java - 适用于TreeSet无区分字段时的比较器