This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
So I have a Maven project named myProj in Eclipse.
Amongst all the other files in the project there exists myProj/src/main/resources/ApplicationContext.xml .
There also exists class myProj/src/main/java/foo/model/TestQueries that contains the main() method.
Within Eclipse I can run TestQueries just fine. So I have a Run Configuration named TestQueriesRunC.
I can successfully create a runnable jar using File->Export->Runnable Jar -> Launch Configuration (select TestQueriesRunC) --LibraryHandling (extract required libraries into generated JAR)
Examining the resulting myProj.jar shows it contains a META-INF/MANIFEST.MF file
and also resources/ApplicationContext.xml
So far so good.
But attempted to execute this from the command line
fails with the following:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document
from class path resource [ApplicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource
[ApplicationContext.xml] cannot be opened because it does not exist
Well it obviously does, so there's some little tweak I need to do when creating the runnable jar but I see no place in the process to put
such a tweak. Or is Eclipse' create-runnable-jar process known to be broken when dealing with Maven projects and I need to use a
different process (and if so, what is this new process?) .
An alternate plan could be to use Maven to create the runnable jar using this in the pom:
which looks promising. But I'd like to be able to get Eclipse to do it on its own.
As always, all suggestions, comments, constructive critisms and especially examples are all welcome.
Since you are using maven anyway, I would go with using the Assembly plugin to create the runtime JAR (like you showed in your post). Then when you need to create the JAR, in Eclipse right-click the project, choose Run As and then choose "mvn package".
It is better to have only one way to build things, and not two ways. And having a build that can be performed from a command line is much preferred over having one that requires someone to click buttons in an IDE. By making your build in Maven, and having Eclipse invoke that, you are getting the best of both.
Plan B seems to be working ( I get a different error now but the context.xml is getting read now and the main class starts executing) so I guess I'll run with that.
But I'd still like an answer for Plan A - I managed to get Eclipse' way to work a year ago, long before I incorporated Maven into my Eclipse and Spring into my app. And whatever notes I took are long gone.
Oh well, thanks for your reply!
Joined: Oct 02, 2008
here is my complete solution for having Maven create a runnable jar for a project that uses the Spring framework.
maven-assembly-plugin doesn't work for projects using the Spring framework because each of Spring's jars contain a spring.handlers, spring.schemas and perhaps a spring.tooling file and all these files wind up being packaged into the META-INF directory. So now you have a directory containing a bunch of files with the same names - not good! - for when you try to execute the jar all these files get loaded IN SEQUENCE and only the *LAST* occurance is used. This is why you get the same "unable to locate Spring NamespaceHandler" error but pointing to DIFFERENT namespaces for different projects, depending on the order of namespaces listed in the projects' context files.
So the solution is to merge the contents of these files so you wind up with only one spring.handlers, one spring.schemas and one spring.tooling file in the META-INF directory. Other posts on the 'net tell you to use maven-assembly-plugin then merge these manually - ugh! maven-shade-plugin is tailor-made for handling this exact situation AND will let you set the main-class and class-path in the manifest.mf file as well - everything you need for "mvn package" to create a runnable jar when you enter "java -jar myProj.jar" on the command line.