用launch4j和maven包装一个java命令行应用程序 [英] Wrapping a java command line application with launch4j and maven
问题描述
我想使用 maven 和 launch4j 将基于 Java 的命令行应用程序及其所有依赖项包装到一个 *.exe 文件中.
I would like to wrap a java based command line app and all it's dependencies into a single *.exe file using maven and launch4j.
现在我已经阅读了关于 SO 的所有类似问题,例如 这个和这个,但我无法让它工作.
Now I have read all similar questions on SO like this one and this but I can not get it to work.
任何人都可以提供一个简单的 pom.xml 片段,如何使用所有需要的依赖项来实现这一点.顺便说一句,我应该在 Eclipses 运行配置中运行什么 Maven 构建目标?
Can anybody supply a simple pom.xml snippet, how to achieve this with all needed dependencies. And by the way, what maven build goal should I run in Eclipses run configuration?
这是我从 SO 复制的:
Here is what I have copied from 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 目标时(如果这是正确的目标?)我得到:
when I run the launch4j:launch4j goal in Eclipse (if this is the correct one?) I get:
未能执行目标org.bluestemsoftware.open.maven.plugin:launch4j-plugin:1.5.0.0:launch4j(default-cli) 在项目导入器上:参数headerType"、jre"为目标org.bluestemsoftware.open.maven.plugin:launch4j-plugin:1.5.0.0:launch4j丢失或无效 -> [帮助 1]
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]
也许我只是在启动错误的目标......
Maybe I'm just launching the false goal ...
推荐答案
Drejc!
我可以生成一个配置与您的非常相似的 .exe 文件.跟随我的整个 pom:
I could generate a .exe file with a configuration very similar to yours. follows my entire 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 的版本也应该可以工作.确保:
I changed the plugin's groupId and artifactId to the vorburger's one, but the alakai's version should work too. Make sure that:
- 您配置了正确的 mainClass
- 您至少声明了一个依赖项(我过去在处理一些非常小的"工件/零依赖项工件时遇到了一些麻烦)
- 您可以从存储库下载插件
我刚刚用简单的 maven 原型测试了这个 pom,所以我看不出有什么理由不能在你的机器上运行.如果您有任何问题,请在这里提问.
I just tested this pom with the simple maven archetype, so I can see no reason for this not working on your machine. If you have any trouble, just ask here.
要生成 .exe 文件,我需要在终端上运行mvn clean package",或者在 Eclipse 中,右键单击您的项目,Run as...">Maven build..."和在目标文本字段中输入clean package".
To generate the .exe file, I need to run a 'mvn clean package' on a terminal or, in Eclipse, Right click on your project, 'Run as...' > 'Maven build...' and type 'clean package' on the goal textfield.
这篇关于用launch4j和maven包装一个java命令行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!