5

I've a simple program build in IntelliJ and using maven that uses the dependency io.netty. I've added to my POM file:

    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.0.Beta1</version>
    </dependency>

In order to compile and get a jar file I usually do:

  • Clean
  • Compile
  • Package

However I noticed that the dependency is not added to the jar, neither existing in the target folder (Or in any of it's sub folders) or added to the resources folder like usually happens.

In order to have the io.netty library to be added to the jar I have tried:

  • Setting the scope to provided and to compile.
  • Re-importing the pom file.
  • Deleting io.netty folder in the .m2/repository/ folder.

I have several other libraries linked including:

  • mysql-connector-java
  • slf4j-simple
  • trove4j

Thanks for reading.

Wesley
  • 855
  • 1
  • 9
  • 23
  • Is maven able to compile your code? – Chetan Kinger May 03 '15 at 14:38
  • 1
    Yes. When ran (java -jar JarFile.jar) it throws an ClassNotFoundException: http://pastebin.com/B51upLZB To be clear: - Maven is not throwing any errors. - The outcome is an .jar. - The .jar does NOT have the io.netty folder included thus throwing an ClassNotFoundException. – Wesley May 03 '15 at 14:49
  • have you had a look in intellij model setting / modules/ dependencies to see if it is a maven dependency? – Rebzie May 04 '15 at 02:52
  • How did you solve this @Wesley I am facing the same issue now. – Gowthami Reddy Nov 06 '17 at 05:11
  • Possible duplicate of [Including dependencies in a jar with Maven](https://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven) – shiramy Mar 08 '19 at 16:14

2 Answers2

2

For some odd reason I had changed my maven configuration a while ago. While I had not added any new libraries, the old ones still had their classes laying around therefor still being added to the jar.

I solved this issue by changing the build in my pom to:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.domain.Program</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Used as reference: http://mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/

Wesley
  • 855
  • 1
  • 9
  • 23
  • Hi, what if we have external jars in the project? I have a maven project having external jars, but they can't seem to be included in the final jar file. Can you help me with this? – Dhruv Singhal Sep 04 '18 at 13:52
0

Maven doesn't package all dependencies into a jar by default. You can use the assembly plugin to build a "jar with dependencies, as seen here: How can I create an executable JAR with dependencies using Maven?

Community
  • 1
  • 1
hugh
  • 2,237
  • 1
  • 12
  • 25
  • 1
    It worked fine with all other dependencies. Never had any trouble with it really. It is just that since I updated from the jboss.org library to the netty.io I'm having this problem. Also I have the set to maven-jar-plugin. How does maven decide what dependencies should be packaged? – Wesley May 03 '15 at 14:47
  • 1
    By default I think it should be just your code and some metadata. Not sure what you mean about - could you post that snippet of the pom? The whole build plugins section is likely to be useful. – hugh May 03 '15 at 15:15
  • My bad, I ment artifactId. pastebin.com/KhNsXq3A I think this all is due to the class files not being added to the resources folder like it normally does. – Wesley May 03 '15 at 16:28
  • Just a quick note, in IntelliJ everything runs fine. Just the compiled jar does not work. – Wesley May 03 '15 at 18:32