Apache Commons IO 未添加到 jar 中

发布于 2024-12-26 09:32:36 字数 497 浏览 1 评论 0原文

我最近将 Apache Commons IO 添加到我的一个小项目中,以便我可以跟踪日志文件。在我的 IDE (IntelliJ) 中一切都运行良好,但是当我创建可执行 jar 时,Commons IO 不在那里,所以我得到:

线程“main”中出现异常java.lang.NoClassDefFoundError: org/apache/commons/io/input/TailerListener。

Commons IO 已添加到我的 POM 中:

<依赖>;
     commons-io;
     commons-io
     <版本>2.1

我以前从未遇到过像这样添加依赖项的问题。我缺少什么?

I've recently added Apache Commons IO to a small project of mine so that I can tail a log file. Everything works great in my IDE (IntelliJ), but when I create the executable jar, Commons IO isn't in there, so I get :

Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/io/input/TailerListener.

Commons IO has been added to my POM:

<dependency>
     <groupId>commons-io</groupId>
     <artifactId>commons-io</artifactId>
     <version>2.1</version>
</dependency>

I've never had issues with added dependencies like this before. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

远昼 2025-01-02 09:32:36

这并不完全是依赖关系的工作方式。依赖项只是告诉其他项目要引入什么。它们不会自动包含在您的 jar 中。

这样做的原因是因为在某些情况下这是浪费的。假设您有两个 jar:a.jar 和 b.jar。两者都依赖于 apache commons。简单地将每个库分开并从公共库目录加载可能会更有效(在空间方面)。

如果你想要一个所谓的“fat jar”(或者任何将所有依赖项放在一个 jar 中的正确术语),你需要使用像这样的插件:

  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2.1</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.test.App</mainClass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

That's not exactly how dependencies work. Dependencies are just there to tell other projects what to pull in. They don't become automatically included in your jar.

The reason for this is because it's wasteful under certain circumstances. Imagine you have two jars: a.jar and b.jar. Both rely on apache commons. It would probably be more efficient (space-wise) to simply have each separated and loaded from a common library directory.

If you want a so-called "fat jar" (or whatever the proper term for having all of your dependencies in one jar is), you need to use a plugin like this one:

  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2.1</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.test.App</mainClass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文