简体   繁体   English

用launch4j和maven包装Java命令行应用程序

[英]Wrapping a java command line application with launch4j and maven

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. 我想使用maven和launch4j将基于Java的命令行应用程序及其所有依赖项包装到单个* .exe文件中。

Now I have read all similar questions on SO like this one and this but I can not get it to work. 现在,我已经阅读像这样所有类似的问题这一个这个 ,但我不能得到它的工作。

Can anybody supply a simple pom.xml snippet, how to achieve this with all needed dependencies. 任何人都可以提供一个简单的pom.xml代码段,以及如何使用所有必需的依赖项来实现这一点。 And by the way, what maven build goal should I run in Eclipses run configuration? 顺便说一句,我应该在Eclipse运行配置中运行哪个Maven构建目标?

Here is what I have copied from SO: 这是我从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>  

when I run the launch4j:launch4j goal in Eclipse (if this is the correct one?) I get: 当我在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] 无法在项目导入程序上执行目标org.bluestemsoftware.open.maven.plugin:launch4j-plugin:1.5.0.0:launch4j(default-cli):目标org.bluestemsoftware.open.maven的参数'headerType','jre' .plugin:launch4j-plugin:1.5.0.0:launch4j丢失或无效-> [帮助1]

Maybe I'm just launching the false goal ... 也许我只是在提出错误的目标...

Drejc! 德雷克!

I could generate a .exe file with a configuration very similar to yours. 我可以生成一个与您的配置非常相似的.exe文件。 follows my entire pom: 遵循我的整个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>

I changed the plugin's groupId and artifactId to the vorburger's one, but the alakai's version should work too. 我将插件的groupId和artifactId更改为vorburger的,但alakai的版本也应该起作用。 Make sure that: 确保:

  1. You configured the correct mainClass 您配置了正确的mainClass
  2. You have at least one dependency declared (I had some trouble in the past with some 'very small' artifacts/zero dependency artifacts) 您至少声明了一个依赖项(过去我遇到了一些“非常小的”工件/零依赖工件的麻烦)
  3. You could download the plugin from the repos 您可以从存储库下载插件

I just tested this pom with the simple maven archetype, so I can see no reason for this not working on your machine. 我只是用简单的Maven原型测试了这个pom,所以我看不出有什么理由不能在您的机器上运行。 If you have any trouble, just ask here. 如果您有任何问题,请在这里询问。

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. 要生成.exe文件,我需要在终端上运行“ mvn clean package”,或者在Eclipse中,右键单击项目,“ Run as ...”>“ Maven build ...”,然后键入“ clean”打包”放在目标文本字段上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM