有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我试图创建一个.bat文件来运行生成的可执行JAR文件。我找到了创建.bat文件以运行项目的this方法。因此,我阅读了插件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

我做错了什么


共 (2) 个答案

  1. # 1 楼答案

    也许它与(来自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中,您阻止生成该存储库。因此,您需要确保项目中的工件被复制到预期的位置

    假设以下项目设置

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

    工件应该位于(脚本中的类路径设置bin/AutoConfigTest

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

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

  2. # 2 楼答案

    我发现了问题@SubOptimal指出主类对批处理文件不可见是正确的

    出于某种原因,测试JAR文件(包含主类)没有被添加到批处理文件的classpath变量中。因此,每当我运行批处理文件时,我都会遇到问题中提到的错误

    我回到文档中,找到了this

    Sometimes it happens that you have many dependencies which means having a very long classpath, and becomes too long (in particular on Windows based platforms). This option can help in such situation. If you activate this option, your classpath contains only a classpath wildcard (REPO/*). But be aware that this works only in combination with Java 1.6 and above and with repositoryLayout flat.

    因此,我没有将单个JAR文件添加到路径中,而是将整个lib目录添加到类路径中,方法是将以下内容添加到pom.xml

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>appassembler-maven-plugin</artifactId>
        <version>1.10</version>
        ...
        <configuration>
            ...
            <repositoryLayout>flat</repositoryLayout>
            <useWildcardClassPath>true</useWildcardClassPath>
            ...
        </configuration>
        ...
    </plugin>
    

    我可以这样做,因为lib的存储库布局已经是平面的。没有等级制度。无需进行其他更改。文件现在的行为与预期一致