带有外部路径上的库的 Maven 可执行 jar

发布于 2024-11-26 20:20:48 字数 3089 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2024-12-03 20:20:48

以上一切似乎都OK。以下是您可能想要尝试/确认/回答的一些事情/问题:

  • 您是从命令行运行的吗?即使用 java -jar 或者您是否通过双击文件等来启动它?如果没有,请尝试从命令行运行它,看看会发生什么。
  • 尝试使用 mvn exec:java 来查看是否定期启动您的应用程序 (maven-)。请参阅http://mojo.codehaus.org/exec-maven-plugin/usage。 html 如果您不熟悉 exec 插件
  • 您可以使用常规 Java System.out.println 而不是日志记录来确认它实际启动吗?日志大小为零可能是日志记录配置问题
  • 我看到您上面有一些数据库库。您能否在基本初始化周围放置一些 println (或更好的日志记录,但只有在您确认日志记录确实有效之后)语句,以确认您不只是停滞在那里(前提是正在发生的情况 - 您没有提到任何异常或其他问题具体来说)

很大程度上取决于实际的应用程序代码,但希望上面的一些内容可以帮助您查明问题。

顺便说一句,您的主类真的是 com.Main 吗?如果是,我可以建议将其更改为更合适的内容 - 例如 com.yourdomain.yourapp.Main 或类似的内容。这并不是说这会改变上面的结果,只是一个风格上的评论。

The above all seems OK. Here are some things/questions you may want to try/confirm/answer:

  • Are you running this from command line? I.e. using 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 happens
  • Try using mvn 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 plugin
  • Can you use regular Java System.out.println instead of logging to confirm that it actually starts? Having a zero-size log might be a logging configuration issue
  • I see you have some DB libraries above. Can you put some println (or better logging, but only after you confirm your logging actually works) statements around basic initialization, to confirm that you are not just stalling there (provided that's what's happening - you don't mention any exceptions or other issues in specific)

A 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文