0

1) I included a Spring Context dependency in my pom.xml project in Eclipse with Maven.

2) I ran the 'Install' phase on the project and it built properly, and the project was installed to my local .m2 repository

3) When I unzipped my .JAR, I only saw my single class that I created custom.

This brings up two questions:

1) Are external, dependency classes only included in your final built jars if a class from it is physically instantiated within your class?

and

2) How come, when I imported the SpringContextAnnotationConfig class into my class, and instantiated an instance of it, and installed my project, I STILL only saw my custom class when I unzipped my .JAR. Is this unusual? Or is the SpringContextAnnotationConfig now just written into the .class binary so that when I deploy my .jar to another JVM, it hass all its dependencies within my custom built .class binary?

Thanks

SOLUTION:

The problem was that I was expecting maven to do the same for a JAR output as it would for a WAR. When using the webapp archetype to generate a WAR file, Maven automatically packaged the required dependency jars into the WEB-INF directory.

I was basically trying to understand how a container on a remote, brand new server would run my classes without having the dependency binaries. I was attempting to run a maven built to produce a JAR file, which did not end up including my dependencies. However, when I ran maven install to build a WAR file, my dependencies were all there ready for deployment.

TyRyDurden
  • 331
  • 4
  • 15
  • 1) yes and 2) unless you actually create a executable jar or a war file, the libraries won't be included. it just registers the dependencies but only includes them in a packaged .war or .jar – rptmat57 Mar 31 '17 at 18:54
  • jar will be created only for your code base. since external dependencies are jar's by itself no need to create again. you can bundle all using zip . while running add all dependencies to classpath – jos Mar 31 '17 at 18:55

1 Answers1

2
  1. No, they are never included (unless you use a special plugin which does that).

  2. See 1.

If you add this artifact as a dependency to some other project, its dependencies (and their dependencies, etc.) will be automatically added (this is controllable, so you can e.g. exclude them or change the version). But they are taken from pom.xml, not from the .jar itself. This also allows not to download same libraries a huge number of times.

For building fat jars, see e.g. How can I create an executable JAR with dependencies using Maven?. Specifically for Spring you may want Spring Boot.

Community
  • 1
  • 1
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487