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
我做错了什么
# 1 楼答案
也许它与(来自README.md)有关
在
pom.xml
中,您阻止生成该存储库。因此,您需要确保项目中的工件被复制到预期的位置假设以下项目设置
工件应该位于(脚本中的类路径设置
bin/AutoConfigTest
)其中
$REPO
解析为target/appassembler/lib
# 2 楼答案
我发现了问题@SubOptimal指出主类对批处理文件不可见是正确的
出于某种原因,测试JAR文件(包含主类)没有被添加到批处理文件的classpath变量中。因此,每当我运行批处理文件时,我都会遇到问题中提到的错误
我回到文档中,找到了this
因此,我没有将单个JAR文件添加到路径中,而是将整个
lib
目录添加到类路径中,方法是将以下内容添加到pom.xml
我可以这样做,因为
lib
的存储库布局已经是平面的。没有等级制度。无需进行其他更改。文件现在的行为与预期一致