带有外部路径上的库的 Maven 可执行 jar
我的 jar 没有运行,我可以告诉它尝试运行,因为 log4j 文件设法创建日志文件夹,但什么也没有发生,日志为空白。
我的问题是我将 jar 文件放在一个名为 bin 的文件夹中,将库放在一个名为 lib 的文件夹中
我正在尝试这个:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<outputDirectory>${staging.dir}/bin</outputDirectory>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<mainClass>com.Main</mainClass>
<classpathPrefix>../lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
我
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${staging.dir}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
也尝试过使用 maven-assemble-plugin,但它打包了 jar 上的所有内容,我真的需要有文件夹 bin 和 lib
我需要设置什么才能使其正常工作?
编辑:META-INF 文件
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: me
Build-Jdk: 1.6.0_26
Main-Class: com.Main
Class-Path: ../lib/ojdbc6-11.2.0.jar ../lib/sqljdbc4-4.2.0.jar ../lib/
mysql-connector-java-5.1.17.jar ../lib/hibernate-core-3.6.5.Final.jar
../lib/antlr-2.7.6.jar ../lib/commons-collections-3.1.jar ../lib/dom
4j-1.6.1.jar ../lib/hibernate-commons-annotations-3.2.0.Final.jar ../
lib/hibernate-jpa-2.0-api-1.0.0.Final.jar ../lib/jta-1.1.jar ../lib/s
lf4j-api-1.6.1.jar ../lib/hibernate-entitymanager-3.6.5.Final.jar ../
lib/cglib-2.2.jar ../lib/asm-3.1.jar ../lib/javassist-3.12.0.GA.jar .
./lib/slf4j-log4j12-1.6.1.jar ../lib/log4j-1.2.16.jar ../lib/commons-
codec-1.5.jar ../lib/lablib-checkboxtree-3.3-20110114.141734-3.jar
解决方案
结果表明 META-INF 文件不正确。原因是 maven-archiver-plugin 使用时间戳重命名 SNAPSHOT 库作为默认行为
来覆盖该使用,如 Maven Archiver 文档所指示:
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<mainClass>com.Main</mainClass>
<classpathPrefix>../lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
除此之外,我希望人们一开始就发现 Maven 代码有用,因为它确实工作,请注意项目中的快照
My jar is not running, I can tell it tries to run as the log4j file manage to create the log folder but then nothing happens and the log is in blank.
My problem is I have the jar file in a folder called bin and the libraries in a folder called lib
I'm triying this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<outputDirectory>${staging.dir}/bin</outputDirectory>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<mainClass>com.Main</mainClass>
<classpathPrefix>../lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
and
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${staging.dir}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
I also tried with maven-assembly-plugin, but it packs everything on the jar and I really need to have the folders bin and lib
What do I need to setup to make it work correctly?
EDIT: META-INF file
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: me
Build-Jdk: 1.6.0_26
Main-Class: com.Main
Class-Path: ../lib/ojdbc6-11.2.0.jar ../lib/sqljdbc4-4.2.0.jar ../lib/
mysql-connector-java-5.1.17.jar ../lib/hibernate-core-3.6.5.Final.jar
../lib/antlr-2.7.6.jar ../lib/commons-collections-3.1.jar ../lib/dom
4j-1.6.1.jar ../lib/hibernate-commons-annotations-3.2.0.Final.jar ../
lib/hibernate-jpa-2.0-api-1.0.0.Final.jar ../lib/jta-1.1.jar ../lib/s
lf4j-api-1.6.1.jar ../lib/hibernate-entitymanager-3.6.5.Final.jar ../
lib/cglib-2.2.jar ../lib/asm-3.1.jar ../lib/javassist-3.12.0.GA.jar .
./lib/slf4j-log4j12-1.6.1.jar ../lib/log4j-1.2.16.jar ../lib/commons-
codec-1.5.jar ../lib/lablib-checkboxtree-3.3-20110114.141734-3.jar
SOLUTION
turns out the META-INF file is incorrect. The reason is that maven-archiver-plugin renames SNAPSHOT libraries with a timestamp as default behaviour
to override that use this, as instructed by the Maven Archiver doc:
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<mainClass>com.Main</mainClass>
<classpathPrefix>../lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
other than that, I hope people find useful the maven code at the start because it does work, just beware the SNAPSHOTS in your projects
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以上一切似乎都OK。以下是您可能想要尝试/确认/回答的一些事情/问题:
java -jar
或者您是否通过双击文件等来启动它?如果没有,请尝试从命令行运行它,看看会发生什么。System.out.println
而不是日志记录来确认它实际启动吗?日志大小为零可能是日志记录配置问题很大程度上取决于实际的应用程序代码,但希望上面的一些内容可以帮助您查明问题。
顺便说一句,您的主类真的是
com.Main
吗?如果是,我可以建议将其更改为更合适的内容 - 例如 com.yourdomain.yourapp.Main 或类似的内容。这并不是说这会改变上面的结果,只是一个风格上的评论。The above all seems OK. Here are some things/questions you may want to try/confirm/answer:
java -jar <your.jar>
or are you starting it e.g. by double-clicking the file, etc.? If not, try running it from the command line to see what happensmvn exec:java
to see if that starts your app (maven-)regularly. See http://mojo.codehaus.org/exec-maven-plugin/usage.html if you are not familiar with exec pluginSystem.out.println
instead of logging to confirm that it actually starts? Having a zero-size log might be a logging configuration issueA lot depends on the actual application code, but hope some of the above might help you pinpoint the issue.
As a side note, is your main class really
com.Main
? If yes, may I suggest to change that to something more appropriate - e.g.com.yourdomain.yourapp.Main
or something along these lines. Not that this will change the above result, just a stylistic comment.