简体   繁体   中英

Apache Commons IO not getting added to jar

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM