java - 将嵌入式 Glassfish 与 Maven 结合使用

标签 java maven glassfish glassfish-embedded

有人了解嵌入式 Glassfish 吗?我想运行一些 EJB 测试,但我不想每次运行测试时都启动和停止嵌入的 glassfish。

根据插件文档,我应该将其放入 POM 中:

            <plugin>
            <groupId>org.glassfish</groupId>
            <artifactId>maven-embedded-glassfish-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <app>target/ejbcontainer-1.0-SNAPSHOT.jar</app>
                <name>test</name>
                <ports>
                    <http-listener>8080</http-listener>
                    <https-listener>8181</https-listener>
                </ports>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>start</goal>  
                        <goal>deploy</goal>
                        <goal>undeploy</goal>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin> 
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <skip>true</skip>
            </configuration>
            <executions>
                <execution>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <skip>false</skip>
                    </configuration>
                </execution>
            </executions>
        </plugin>

这一切都很好。我可以“运行”这个嵌入式 glassfish,并在控制台中得到它,这证明它已启动并正在运行:

Information: test was successfully deployed in 1,124 milliseconds. PlainTextActionReporterSUCCESSDescription: deploy AdminCommandApplication deployed with name test. [name=test Dec 16, 2013 6:03:29 PM PluginUtil doDeploy Information: Deployed test Hit ENTER to redeploy, X to exit

但是,当我“运行”我的测试文件时,会创建嵌入 glassfish 的一个新实例

我的测试文件没有选择当前正在运行的容器。

这是一个测试文件(如果有帮助的话):

public class Test extends TestCase {

    private Context ctx;
    private EJBContainer ejbContainer;

    public Test(String testName) {
        super(testName);
    }

    @BeforeClass
    public void setUp() {
        ejbContainer = EJBContainer.createEJBContainer();

        System.out.println("Opening the container");

        ctx = ejbContainer.getContext();
    }

    @AfterClass
    @Override
    public void tearDown() {
        ejbContainer.close();
        System.out.println("Closing the container");
    }

    @org.junit.Test
    public void testApp() throws Exception {
        TemperatureConverter converter = (TemperatureConverter) ctx.lookup("java:global/classes/TemperatureConverter");
        assertNotNull(converter);
    }
}

最佳答案

刚刚发现这个问题,因为我自己一直在玩嵌入式 glassfish 并遇到一些配置问题,主要与日志输出有关。

我使用嵌入式 glassfish 进行测试的方式是将其绑定(bind)到 Maven 集成测试阶段,例如

          <executions>
            <!-- Start embedded GlassFish -->
            <execution>
                <id>start</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>start</goal>
                </goals>
            </execution>
            <execution>
                <id>deploy</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>deploy</goal>
                </goals>
            </execution>
            <execution>
                <id>stop</id>
                <phase>post-integration-test</phase>
                <goals>
                    <goal>undeploy</goal>
                    <goal>stop</goal>
                </goals>
            </execution>
          </executions>

并使用Maven Failsafe Plugin执行测试作为验证目标的一部分。不过,这些变得更像集成测试。如果您使用 IT 后缀命名测试文件,例如myTestFileIT.java 那么它们应该会被自动选取。

然后您可以通过执行以下 Maven 命令来运行测试:

 mvn verify

我最初使用的是嵌入式 Jetty,在这里我的设置工作得非常好,我发现 glassfish 有点复杂,而且要完全按照我的要求进行配置相当耗时。

希望这能在某种程度上帮助解决您的问题。

关于java - 将嵌入式 Glassfish 与 Maven 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20616873/

相关文章:

java - GlassFish v3 和 glassfish-maven-plugin (Mac)

java - 项目无法在装有最新版本的 Netbeans 和 Java SE 开发工具包的新 PC 上启动

java - 如果在多个属性文件中定义了一个属性,Spring 如何选择要使用的属性值?

java - 强制 maven 重新导入的 IntelliJ 快捷方式

java - 在响应中发送包含 blob 的实体对象

java - 有没有一种方法可以将配置文件添加到一个maven项目中并共享到其他maven项目中?

java - 如何使用 Spring 使用 Maven Java 元素链接到外部 css 样式表

ubuntu - Glassfish 4.1 和 MariaDB 的麻烦

java - 解析来自 .Net 网络服务的 JSON 响应

java - 是否可以获得abstract、extends、implements关键字的源代码?