RobertoBifulco.it

  • topics
  • publications
  • il gatto
  • account
Home

How to build runnable JARs with Spring dependencies using Maven

Roberto Bifulco — Tue, 08/31/2010 - 09:16

 If you need to build a runnable spring-based JAR using maven, there could be some problems in the spring's configuration parsing. You know, to build a runnable JAR we have to include in the JAR all the dependencies, and Maven can help a lot in this, for example using the maven-assembly-plugin.

Here is a sample configuration of such plugin:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
                <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>2.2-beta-5</version>
                        <configuration>
                                <descriptorRefs>
                                        <descriptorRef>jar-with-dependencies</descriptorRef>
                                </descriptorRefs>
                                <archive>
                                        <manifest>
                                                <mainClass>fully.qualified.name.of.MainClass</mainClass>
                                        </manifest>
                                </archive>
                        </configuration>
                </plugin>

The plugin works well, but if you have more than the simple spring-core dependency, than you could have an Exception like: 
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/security]

The problem is that spring uses in each module's JAR two meta files:

  • META-INF/spring.handlers
  • META-INF/spring.schemas

such files are used in conjuction with configurations' XML Schemas to validate the application configuration even if the application is executed off-line. Because each Spring module brings these files, when the assemply plugin runs, it simply overwrites multiple times such files in the META-INF/ directory of the resulting JAR.
As result, we have that some configurations can't be validated and so the BeanDefinitionParsingException is thrown.

To avoid the problem we can use the maven-shade-plugin that performs some transformations to the resulting JAR's files. Here is an example configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
                <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>1.4</version>
                        <executions>
                                <execution>
                                        <phase>package</phase>
                                        <goals>
                                                <goal>shade</goal>
                                        </goals>
                                        <configuration>
                                                <transformers>
                                                        <transformer
                                                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                                                <mainClass>fully.qualified.name.of.MainClass</mainClass>
                                                        </transformer>
                                                        <transformer
                                                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                                                <resource>META-INF/spring.handlers</resource>
                                                        </transformer>
                                                        <transformer
                                                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                                                <resource>META-INF/spring.schemas</resource>
                                                        </transformer>
                                                </transformers>
                                        </configuration>
                                </execution>
                        </executions>
                </plugin>

As you can see we are specifying something more than the MainClass: we are also saying that when spring.handlers and spring.schemas files are found, their contents must be appended to already existing files in the META-INF directory.

Running the maven package goal, we'll now get a working spring-based runnable JAR! 

 

  • Java
  • Maven
  • Spring

nfl jerseys suppliers Fashion

Anonymous (not verified) — Mon, 01/02/2012 - 08:03

nfl jerseys suppliers Fashion brand
wholesale coach bags the trend of fashion
coach outlet Don't miss the chance it's very good
coach outlet store comfortable with it
wholesale designer handbags well known great
coach handbags outlet attractive and reasonable price
cheap coach online shopping
coach outlet store may most likely need
coach handbag outlet A good thing
authentic nfl jerseys for sale fashion designed
cheap authentic nfl jerseys together with lifestyle

  • reply

nfl jerseys

mostnfl (not verified) — Fri, 12/30/2011 - 14:15

Patriots Jerseys Sale
Premier New England Patriots Jerseys

  • reply

Super Bowl NFL Jersey Live

mostnfl (not verified) — Thu, 12/29/2011 - 09:17

Super Bowl NFL Jersey Live NFL scores, Green Bay Packers Jersey in-depth pro football Pittsburgh Steelers Jersey player and New Orleans Saints Jersey team news, Baltimore Ravens Jersey NFL videos, rumors ,San Francisco 49ers Jersey
updated stats, team schedules, fantasy football games,

  • reply

Coates (Coach) design

coach factory store (not verified) — Thu, 12/29/2011 - 07:13

Coates (Coach) design combines innovative fashion sense and attitude of the modern United States. Coates
[coach outlet] insist that every product has reached the highest level of technology. Coates (Coach) product size, shape, pockets as well as lace are carefully designed. Coates
[coach factory] leather handle has a fine tradition, Coates
[coach factory store] based on texture, toughness, fine features and textures carefully selected Coach of the highest quality leather. Immersed in the dye barrel after many days, after repeated severe tests to ensure functionality and durability in all aspects of the leather will become the standard Coates
[prada outlet] of raw materials.

  • reply

years christian louboutin

ugg boots uk (not verified) — Sat, 12/24/2011 - 05:49

years christian louboutin sale christian louboutin boots of christian louboutin heels teaching experience christian louboutin shoes summed up christian louboutin pumps by a unique set of louboutin sale teaching methods. in this remo...

  • reply

Thank you very much! After

Anuskha Mohan (not verified) — Fri, 10/21/2011 - 13:49

Thank you very much! After searching for hours, finally a solution that works!!

  • reply

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.
  • topics
  • publications
  • il gatto
  • account