如何使用Maven构建和部署远程EJB?

时间:2013-05-22 06:57:36

标签: java maven ejb jboss7.x

我想使用Maven来构建和部署远程EJB。稍后,我希望能够将此EJB注入并使用到单独的WAR项目中。

所以我想我需要一个定义接口的jar文件,并在两个项目中使用jar(ejb和war)。

我在名为example:api的Maven项目中有这个接口,打包为jar:

package example;

import javax.ejb.Remote;

@Remote
public interface Bean {

    void process();
}

我在另一个名为example:ejb的Maven项目中实现了上述接口,打包为ejb:

package example;

import javax.ejb.Stateless;

@Stateless
public class Ejb implements Bean {

    @Override
    public void process() {
    }
}

ejb项目使用maven-ejb-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ejb-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <ejbVersion>3.1</ejbVersion>
    </configuration>
</plugin>

这两个项目都取决于javaee-api

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

现在将它们放在一起,我制作了第三个Maven项目,名为example:ear,打包为ear

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>example</groupId>
    <artifactId>ear</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>ear</packaging>
    <name>ear</name>

    <dependencies>
        <dependency>
            <groupId>example</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>example</groupId>
            <artifactId>ejb</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>ejb</type>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <configuration>
                    <modules>
                        <ejbModule>
                            <groupId>example</groupId>
                            <artifactId>ejb</artifactId>
                        </ejbModule>
                        <jarModule>
                            <groupId>example</groupId>
                            <artifactId>api</artifactId>
                        </jarModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

问题是我无法部署这个耳朵项目。我收到以下错误:

09:39:39,702 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 2) JBAS015003: Found ear.ear in deployment directory. To trigger deployment create a file called ear.ear.dodeploy
09:39:39,713 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-6) JBAS015876: Starting deployment of "ear.ear"
09:39:39,773 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "ejb-0.0.1-SNAPSHOT.jar"
09:39:39,793 WARN  [org.jboss.modules] (MSC service thread 1-8) Failed to define class example.Ejb in Module "deployment.ear.ear.ejb-0.0.1-SNAPSHOT.jar:main" from Service Module Loader: java.lang.LinkageError: Failed to link example/Ejb (Module "deployment.ear.ear.ejb-0.0.1-SNAPSHOT.jar:main" from Service Module Loader)
    at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:396)
[...]
Caused by: java.lang.NoClassDefFoundError: example/Bean
    at java.lang.ClassLoader.defineClass1(Native Method) [rt.jar:1.7.0_03]
[...]
Caused by: java.lang.ClassNotFoundException: example.Bean from [Module "deployment.ear.ear.ejb-0.0.1-SNAPSHOT.jar:main" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)

我正在使用JBoss AS 7.1.1,如果它有所不同。

你知道我做错了吗?

1 个答案:

答案 0 :(得分:3)

我更改了maven-ear-plugin

的配置
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ear-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <version>6</version>
        <defaultLibBundleDir>lib</defaultLibBundleDir>
    </configuration>
</plugin>

这种方式有效。