Announcement Announcement Module
Collapse
No announcement yet.
How to create a Spring-based executable jar application? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to create a Spring-based executable jar application?

    I have a Maven based Spring-WS client project that I want to package as a single jar. In eclipse, everything runs properly. When I try to package it as an executable jar, I get ClassNotFound exceptions since the Spring jars are not included in my application jar.

    So I added the maven-shade-plugin to include all my dependencies in my application jar. When I look at my app jar, I see all the class files from all the dependencies included (all the library jar's are exploded).

    Code:
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
    
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.cws.cs.Client</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
    
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
    
            </plugin>
        </plugins>
    </build>
    My problem is that in the packaging process, my multiple spring dependencies have different META-INF/spring.schemas files that override each other. Consequently, my final jar has an incomplete spring.schemas file.

    So when I try to run my executable jar, I get Spring error messages that files cannot be found since the spring.schemas file is incomplete (the Spring-WS's jar has overriden the Spring-core's spring.schemas file).

    My executable jar's META-INF/spring.schemas:
    Code:
    http\://www.springframework.org/schema/web-services/web-services-1.5.xsd=/org/springframework/ws/config/web-services-1.5.xsd
    http\://www.springframework.org/schema/web-services/web-services-2.0.xsd=/org/springframework/ws/config/web-services-2.0.xsd
    http\://www.springframework.org/schema/web-services/web-services.xsd=/org/springframework/ws/config/web-services-2.0.xsd
    Instead of Spring-beans.jar META-INF/spring.schemas:

    Code:
    http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
    http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
    http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
    http\://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
    http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
    http\://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd
    http\://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd
    http\://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd
    http\://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd
    http\://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd
    http\://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd
    http\://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd
    http\://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd
    http\://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd
    http\://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd
    I'm stumped. Is this impossible? Can I not package everything as a single executable jar? I don't know if this is a shade-plugin configuration issue, or if I am trying to do something impossible. It would not seem correct that I would have to manually create my own spring.schemas file (a concatenation of the others).

    Any suggestions would be appreciated.

    Thanks,

    Eric

  • #2
    Try the one-jar plugin for maven: http://code.google.com/p/onejar-maven-plugin/

    It packages everything in a single jar, but unlike shade plugin, dependencies are not unpacked but copied as jar files inside a unique jar. I used it to package an app with spring, hibernate and several other libraries, and it worked like charm

    Code:
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <archive>
                            <manifest>                        
                                    <mainClass>foo.MainClass</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                
                <plugin>
                    <groupId>org.dstovall</groupId>
                    <artifactId>onejar-maven-plugin</artifactId>
                    <version>1.4.4</version>
                    <executions>
                        <execution>
                            <configuration>
                                <onejarVersion>0.97</onejarVersion>>
                                <attachToBuild>true</attachToBuild>
                                <classifier>onejar</classifier>
                            </configuration>
                            <goals>
                                <goal>one-jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

    Comment


    • #3
      Thanks. My only problem with the one-jar plugin is that it creates a separate artifact with a one-jar.jar extension, so that when you install/deploy, it still deploys the original jar file (and not the one-jar file with all the libs).

      Any ideas how to properly structure the pom to allow for the proper deployment?

      Thanks,

      Eric

      Comment

      Working...
      X