Dienstag Nov 10, 2009

trouble with spring and maven assembly plugin

I've built a couple of small java tools recently that do some background processing and get run from cron. These tools are too complex for a maintainable groovy or python script (at least for my taste) and I therefore implement them in Java using spring for its wonderful JDBC template, database connection handling and some other niceties. Now to make for a simple deploy I pack them up using mavens assembly plugin and here is where the fun starts. If you do this you will get an error like this:

Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace
The reason is that the assembly plugin copies stuff from META-INF dirs of dependency jars into the META-INF dir of the resulting jar and quite a few of the spring jars have spring.handlers and spring.schemas files in their META-INF directory. Now when the assembly plugin packages all these together only the files of teh last jar it deals with end up in the resulting jar which causes above error when you try to run the jar. After some googling I found a couple of hints on the web, e.g. http://forum.springsource.org/showthread.php?p=190246#post190246 and http://forum.springsource.org/showthread.php?p=195655#post195655 and cooked up a fix that works ok for me. I first concatenated the contents of all spring.handlers files into a new spring.handlers file all spring.schemas into a new spring.schemas and put the resulting files into my src/main/resources directory. I then created the file src/main/assembly/my-jar-with-dependencies.xml with the following contents:
<assembly>
  <id>my-jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <scope>runtime</scope>
      <unpackOptions>
	      <excludes>
	      	<exclude>**/spring.handlers</exclude>
	      	<exclude>**/spring.schemas</exclude>
	      </excludes>
      </unpackOptions>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <directory>${project.build.outputDirectory}</directory>
    </fileSet>
    <fileSet>
		<directory>target/classes</directory>
		<outputDirectory>META-INF</outputDirectory>
		<includes>
			<include>spring.handlers</include>
			<include>spring.schemas</include>
		</includes>
	</fileSet>
  </fileSets>
</assembly>
and added this to the plugins section in my pom.xml
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptors>
      <descriptor>src/main/assembly/my-jar-with-dependencies.xml</descriptor>
     </descriptors>
     <archive>
       <manifest>
         <mainClass>my.main.Class</mainClass>
       </manifest>
     </archive>
  </configuration>
</plugin>
If I now run 'mvn assembly:assembly' it created the file 'target/myProject-my-jar-with-dependencies.jar' which I can run without getting the 'Unable to locate …' message any longer.
P.S. There is also a JIRA issue on this: MASSEMBLY-360

Kommentare:

Senden Sie einen Kommentar:
  • HTML Syntax: Eingeschaltet