Maven assembly plugin and Spring

by kinabalu on March 2, 2009

Here at Mystic, we do a lot of different types of development. It’s not all web development. I found myself writing a simple document parser that takes files uploaded by automated process, processes them by adding to a database, and finishes. One of the components we used to tie everything together, the Spring Framework, it’s great for that.

Since this would be a command line app running via cron most likely, we pulled Maven and the assembly plugin from our bag-of-tricks. Simply adding something like so:

            <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <configuration>
                  <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
                  <archive>
                    <manifest>
                      <mainClass>com.your.main.class.file</mainClass>
                    </manifest>
                  </archive>
              </configuration>
            </plugin>

All was right with the world, until we ran it from the dependency included jar on the server, and BAM!

Exception in thread “main” org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace http://www.springframework.org/schema/context

Ouch, how frustrating, this worked in the IDE “Works on my machine ™”. After a bit of googling I realized, we were using separate dependencies for the Spring jars that we needed, and the contents of META-INF/ were getting overwritten (only a single JAR remember?). So the solution was to swap out all the separate Spring dependencies in Maven, and use the all-in-one like so:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>${spring.version}</version>
        </dependency>
Share this:
  • Digg
  • DZone
  • Facebook
  • Technorati
  • LinkedIn
  • del.icio.us
  • Google Bookmarks
  • E-mail this story to a friend!
  • TwitThis

{ 4 comments… read them below or add one }

ChrisNo Gravatar March 7, 2009 at 8:53 am

Can you use the maven plugin for creating the manifest to write out the correct information?

Andrew SwanNo Gravatar March 10, 2010 at 9:00 pm

Nice solution, but what if you need Spring 3.x, which doesn’t have the all-in-one JAR file?

NicholasNo Gravatar April 16, 2010 at 7:22 am

Thank you, thank you, thank you!

RomanNo Gravatar May 24, 2010 at 10:38 am

@Andrew Swan: I use the onejar-maven-plugin from http://code.google.com/p/onejar-maven-plugin/
with Spring 3.0 without problems. It puts all your dependency-JARs and your own maven created JAR file in one big JAR and appends some classpath magic …

Leave a Comment

Previous post:

Next post: