java - 如何使用运行 JAR 的 Maven Appassembler 创建 .bat 文件?

标签 java maven batch-file jar

我正在尝试创建一个 .bat 文件来运行生成的可执行 JAR 文件。我发现this创建用于运行项目的 .bat 文件的方法。所以,我阅读了插件 here并将以下内容添加到我的 pom.xml 中。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>assemble</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <assembleDirectory>${assembleDir}</assembleDirectory>
        <generateRepository>false</generateRepository>
        <repositoryName>lib</repositoryName>
        <configurationDirectory>conf</configurationDirectory>
        <copyConfigurationDirectory>false</copyConfigurationDirectory>
        <programs>
            <program>
                <mainClass>com.companyname.tests.TestRunner</mainClass>
                <id>AutoConfigTest</id>
            </program>
        </programs>
    </configuration>
</plugin>

是的,顾名思义,这个 JAR 包含 JUnit 测试用例。

我阻止插件解压 JAR 并创建 repo 文件夹,并将其设置为我已经生成的 lib 文件夹,其中包含所有 JAR(可执行文件和依赖项) 。正在生成 .bat 文件,但是在运行它时,出现以下错误。

Error: Could not find or load main class com.companyname.tests.TestRunner

此外,我希望命令提示符在执行后保留。在这种情况下,它会立即关闭。也许是因为我收到错误。我不确定。

于是,再次开始搜索,发现 this 。但正如已接受的答案所示,我的 pom.xml 已经包含 -

<packaging>jar</packaging>

组装后的目录为-

AutoConfigTest
 |
 |--bin
 |   `- contains the .bat file
 |--conf
 |   `- contains the property files and other configuration files
 |--lib
     `- contains all the JARs

我在这里做错了什么?

最佳答案

也许与(来自 README.md )有关

All dependencies and the artifact of the project itself are placed in a generated Maven repository in a defined assemble directory. All artifacts (dependencies + the artifact from the project) are added to the classpath in the generated bin scripts.

在您的 pom.xml 中,您可以阻止生成该存储库。因此,您需要确保项目中的 Artifact 被复制到预期的位置。

假设以下项目设置

<groupId>com.companyname</groupId>
<artifactId>Maven-AppAssembler</artifactId>
<version>0.0.1-SNAPSHOT</version>

Artifact 预计位于(脚本 bin/AutoConfigTest 中的 CLASSPATH 设置)

"$REPO"/com/companyname/Maven-AppAssembler/0.0.1-SNAPSHOT/Maven-AppAssembler-0.0.1-SNAPSHOT.jar

其中 $REPO 解析为 target/appassembler/lib

关于java - 如何使用运行 JAR 的 Maven Appassembler 创建 .bat 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37518895/

相关文章:

java - 如何有效获取 "AS"列名,如ResultSetMetaData?

maven - Robospice/Maven Intellij Android

java - Maven在尝试连接到Cloudera URL以获取CDH Jar位置时显示错误

batch-file - 我如何调用 concat : %variable% when the variable has internal spaces?

java - 对泛型参数类型的错误转换不会在 Java 中抛出 ClassCastException

java编译

java - IntelliJ IDEA 12 与 Play 2.0。没有 Java/Scaladocs

java - 在android项目中使用maven库

batch-file - dir/b 命令输出一行,加上DELIMITER 批处理文件编程

java - 批处理文件命令转powershell命令