10

I'm relatively new to Java, so I hope this isn't a dumb question.

I have a Web project in Eclipse that I'm trying to deploy to Tomcat. I have two dependent projects in Eclipse which are being compiled into .jar files and deployed to the /WEB-INF/lib directory.

This is fine, but unfortunately Spring doesn't scan for annotations in dependencies unless the class files from the .jars are extracted into the /WEB-INF/classes directory.

Is there an easy way to do this extraction at build time? I'm using Maven. I've written a batch file for the time being to do this (I'm developing on Windows, deploying on Ubuntu)

I do have questions about how to automate this for server deployment, though, am I doing something wrong? Surely I'm not the only one to wrestle with this problem.

Jason Kolb
  • 248
  • 1
  • 2
  • 9

4 Answers4

11

According to this Spring issue, if you're creating JAR files in Eclipse using the Export... > Java > JAR file wizard, then you have to check the Add directory entries checkbox (unchecked by default) for Spring's component scan to find components in the JAR files.

Chin Huang
  • 11,862
  • 4
  • 43
  • 44
6

I think spring scans the whole classpath, you just have to provide:

<context:component-scan base-package="org.example"/>

There is a note in the docs:

The scanning of classpath packages requires the presence of corresponding directory entries in the classpath. When you build JARs with Ant, make sure that you do not activate the files-only switch of the JAR task.

Bozho
  • 572,413
  • 138
  • 1,043
  • 1,132
0

@Chin Huang,

Thank you so much for your answer.

Was generating the .jar using Eclipse and never crossed my mind that that could be the problem.

I generated the .Jar using ANT and that fixed the issue. Sometimes having too many options confuse you, this was one of those cases.

Anoop Hallimala
  • 543
  • 1
  • 9
  • 22
0

Since you mentioned you are using annotations as well

 <context:annotation-config/> 
 <context:component-scan base-package="org.example"/>

is what you need in your example*.xml file

Some Java Guy
  • 4,774
  • 19
  • 66
  • 104
  • is not necessary when using context:component-scan since component-scan also scans for annotations – wilco Sep 13 '16 at 19:36