[使用心得]maven2之m2eclipse使用手册之六使用Maven2插件创建一个简单的SSH2项目之glassfish篇(二)

glassfish在maven中其实有两种,一种使用已经安装好的glassfish来配置,另外一种则是嵌入式的glassfish,其命令跟glassfish是完全不一样的,对于旧版的glassfishv3.0的嵌入式教程则不多介绍了,想使用的话请参考以下地址:

http://weblogs.java.net/blog/arungupta/archive/2008/11/totd_56_simple.html

这篇文章主要介绍的是RESTful 中使用嵌入的glassfish配置web Service,而我这次带来的是则是目前新出的glassfishv3.0.1版本的嵌入式web应用程序.

使用嵌入式pom.xml则不需要maven-glassfish-plugin这个插件了,取而代之则使用maven-embedded-glassfish-plugin这个插件,本例子pom.xml如下:

<build>

		<plugins>

			<plugin>

				<groupId>org.apache.maven.plugins</groupId>

				<artifactId>maven-compiler-plugin</artifactId>

				<version>2.3.1</version>

				<configuration>

					<source>1.6</source>

					<target>1.6</target>

					<encoding>utf-8</encoding>

				</configuration>

			</plugin>

			<plugin>

				<groupId>org.glassfish</groupId>

				<artifactId>maven-embedded-glassfish-plugin</artifactId>

				<version>3.0</version>

				<configuration>

					<app>${project.build.directory}/${build.finalName}.war</app>

					<autoDelete>true</autoDelete>

					<port>8080</port>

					<contextRoot>SSH2</contextRoot>

				</configuration>

			</plugin>

		</plugins>

		<finalName>SSH2Maven</finalName>

	</build>

一个比较完整的嵌入式pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>

Line breaks in the following element are for readability purposes only

<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/maven-v4_0_0.xsd">



  <modelVersion>4.0.0</modelVersion>

  <groupId>org.glassfish</groupId>

  <artifactId>maven-glassfish-plugin-tester</artifactId>

  <version>3.0-74b</version>

  <name>Maven test</name>

  <build>

    <plugins>

      <plugin>

        <groupId>org.glassfish</groupId>

        <artifactId>maven-embedded-glassfish-plugin</artifactId>

        <version>3.0-74b</version>

        <configuration>

          <goalPrefix>embedded-glassfish</goalPrefix>

          <app>test.war</app>

          <port>8080</port> 

          <contextRoot>test</contextRoot> 

          <autoDelete>true</autoDelete>

       </configuration>

       <executions>

          <execution>

             <phase>install</phase>

             <goals>

           	    <goal>run</goal>

             </goals>

          </execution>

       </executions>

     </plugin>

    </plugins> 

  </build>

  <pluginRepositories>

      <pluginRepository>

          <id>maven2-repository.dev.java.net</id>

          <name>Java.net Repository for Maven</name>

          <url>http://download.java.net/maven/glassfish/</url>

      </pluginRepository>

  </pluginRepositories>

</project>

 

更详细配置的请参考:设置maven环境

对于<configuration>标签配置说明如下:

<app>:你的web应用的war目录位置,可以自定义,或者可以通过m2eclipse中的package命令打成war包,后写输出的路径.

<autoDelete>:自动删除,默认为false,如果设为true的话则当服务器停止的时候会删除实例的根目录.

<port>:端口号,运行web应用的时候的端口口

<contextRoot>:应用的名字,相当于输入完地址栏后的目录地址,如:http://localhost:8080/test 地址中的test

对于嵌入式的glassfish运行命令跟之前的已安装好的glassfish的命令是不一样,主要的命令如下(中英对照):

embedded-glassfish:run Goal :(启动服务器并且部署该应用,如果你更改了该应用里面的内容,你可以重新部署,这个应用可以是一个已经打成war包的或者是一个目录,但是目录里面必须要包含一个应用程序.)This goal starts the server and deploys an application. You can redeploy if you change the application. The application can be a packaged archive or a directory that contains an exploded application.
embedded-glassfish:start Goal:(启动服务器,但是不能使用glassfish控制台功能)This goal starts the server.
embedded-glassfish:deploy Goal:(部署应用程序,如果你更改了该应用里面的内容,你可以重新部署,这个应用可以是一个已经打成war包的或者是一个目录,但是目录里面必须要包含一个应用程序,)This goal deploys an application. You can redeploy if you change the application. The application can be a packaged archive or a directory that contains an exploded application.
embedded-glassfish:undeploy Goal:(移除已经部署的应用)This goal undeploys an application
embedded-glassfish:stop Goal:(停止服务器)This goal stops the server.

对于以上命令都有其参数,每个命令使用的参数请参考:参数说明,另外附上另外一位对这些命令的说明:嵌入式glassfish功能说明

对于测试代码还是继续沿用之前的,地址:[使用心得]maven2之m2eclipse使用手册之六使用Maven2插件创建一个简单的SSH2项目之jetty篇(二)

运行embedded-glassfish:run 命令(如何运行请参考:[使用心得]maven2之m2eclipse使用手册之六使用Maven2插件创建一个简单的SSH2项目之glassfish篇(一)

),在Console控制台出现扫描信息(如图1:扫描信息),等待一段时间后最后会出现让你选择的信息(如图2:部署成功),按回车则会重新部署,按X则会退出当前应用,在浏览器地址栏输入http://localhost:8080/SSH2/会如果出现相应的信息(如图3:运行成功界面),则证明嵌入式glassfish运行成功,而且后台也会打印出查询的语句(如图4:查询语句).

 

图1:扫描信息

 

图2:部署成功

图3:运行成功界面

图4:查询语句

 

 

注意:如果使用Struts的<s:debug>标签的话后台会出现报错,但是不影响使用,具体原因不明,目前正在查找当中,估计struts的实现方式跟glassfish有冲突,其他标签则无异常.

对于glassfish参考的文档比较多这里就不一一列出的,如何想更深一步了解嵌入式的glassfish详细教程可以查看oracle sun公司的文档,地址如下:

http://docs.sun.com/app/docs/doc/821-1208/gihus?l=en&a=view

你可能感兴趣的:(Glassfish)