質問

Trying to deploy a war with a bean file in a Fuse Servicemix (version 4.3.1). I'm using maven to build my war. I can't seem to get this to work. Can anyone provide a website that can tell me how to do this?

This website tells me what to put in the web.xml file but doesn't explain the rest.

http://fusesource.com/docs/esbent/7.0/esb_deploy_osgi/BuildWar-Spring.html.

I've tried several solutions and methods over the course of 19 days. Everyone seems to skin this cat differently but none of them work for me.

fat war (SOLVED):

See answer below


skinny war:

Seems impossible in osgi. Need to manually import too many packages. This link appears to solve it but seems there a lot of nasty side effects.

http://davidvaleri.wordpress.com/2011/08/17/deploying-spring-mvc-based-web-applications-to-osgi-using-apache-servicemix/

役に立ちましたか?

解決 2

Fat War solution

This is the minimum viable solution that worked for me. I played around trying to remove things and it broke as soon as I did, often without even posting an error message.


directory structure:

src/main/java/test/Test.java
src/main/webapp/WEB-INF/web.xml
src/main/webapp/WEB-INF/applicationContext.xml

pom.xml

...
    <groupId>test</groupId>
    <artifactId>war-bean-test</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>3.0.5.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework.osgi</groupId>
          <artifactId>spring-osgi-web</artifactId>
          <version>1.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version>
                <executions>
                  <execution>
                    <id>bundle-manifest</id>
                    <phase>process-classes</phase>
                    <goals>
                      <goal>manifest</goal>
                    </goals>
                  </execution>
                </executions>
                <configuration>
                  <supportedProjectTypes>
                    <supportedProjectType>jar</supportedProjectType>
                    <supportedProjectType>bundle</supportedProjectType>
                    <supportedProjectType>war</supportedProjectType>
                  </supportedProjectTypes>
                  <instructions>
                    <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
                    <Bundle-Version>${project.version}</Bundle-Version>

                    <!-- IMPORTANT resolution:=optional fixes bug where bundle fails to load unnecessary packages such as bsh. You also need javax.servlet. In Servicemix 4.3.1 it is provided by geronimo servlet. -->
                    <Import-Package>
                        javax.servlet
                        *; resolution:=optional
                    </Import-Package>
                    <Export-Package></Export-Package>

                    <!-- IMPORTANT explicitly adding the jars fixes the numerous CassNotFoundExceptions -->
                    <Bundle-ClassPath>
                        .,WEB-INF/classes,{maven-dependencies}
                    </Bundle-ClassPath>
                    <Web-ContextPath>warbeantest</Web-ContextPath>
                    <Webapp-Context>warbeantest</Webapp-Context>

                    <!-- adding inline=true to Embed-Dependency causes {maven-dependencies} to not work and you will have to add every jar by hand -->
                    <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
                    <Embed-Transitive>true</Embed-Transitive>
                    <Embed-Directory>WEB-INF/lib</Embed-Directory>
                  </instructions>
                </configuration>
              </plugin>

              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                  <archive>
                    <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                  </archive>
                </configuration>
              </plugin>
        </plugins>
    </build>
</project>

web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee">
    <display-name>war-bean-test</display-name>
    <description>war-bean-test</description>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <!-- If you remove this then the spring beans will still work, but you wont be able to fetch services and resources from other osgi bundles -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="test" class="test.Test">
        <property name="value" value="1" />
    </bean>
</beans>

Test.java

package test;
public class Test {
    private int value = 0;
    public TestImpl() { }

    public void setValue(int value) {
        // Should print to console when you load into Fuse Servicemix
        System.out.println("testing...");
        this.value = value;
    }

    public int getValue() { return value; }
}

他のヒント

You need to add the Spring OSGi ContextLoaderListener to your web.xml otherwise it doesn't work. You'll also need dependencies to Spring-DM 1.2.1. Take a look at Pax Web Spring sample and especially the web.xml in it. It's a working example on how to use Spring in Karaf / Fuse-ServiceMix ...

I guess I pointed you to the wrong sample. You need t use the following.

contextClass
org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top