使用winstone嵌入web Application

      winstone是一个嵌入式的web服务器,体积只有320KB,它可以嵌入到我们的web应用程序里面。平时我们要发布一个war包的时候需要把war包放到tomcat或者jetty的webapps文件夹里面进行发布,然而使用winstone进行嵌入时,则可以把winstone加入到war包里面,简单的执行一行命令之后就可以打开浏览器输入地址进行访问了。

 

     例如我的web应用程序的名字为:myweb,则执行 java -jar myweb.war就可以看到页面。大名鼎鼎的CI工具 jenkins就采用的是这种方式。当然,除了这用方式之外还有另外一种打包成jar的方式。本文将介绍这两种不同的打包。

 

winstone的网址: http://winstone.sourceforge.net/

 

jar方式:

    该方式打成的jar包具有一定的局限性,不能在tomcat或者其他的web容器中部署。

     1、 使用 maven构建一个web项目

 

扫描二维码关注公众号,回复: 1176530 查看本文章

 

     2、修改pom.xml文件, 在<bulid>元素里加入:

       <plugins>
            <plugin>
                <groupId>net.sf.alchim</groupId>
                <artifactId>winstone-maven-plugin</artifactId>

                <executions>
                    <execution>
                        <goals>
                            <goal>embed</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                </executions>
                
            </plugin>
        </plugins>

 

     3、执行  mvn install 命令构建jar包。(默认生成的是  *-standalone.jar 一个文件)

 

     4、执行 java -jar myweb-standalone.jar (在浏览器中输入 http://localhost:8080 就可以看到效果了,当然端口可以自己定义,这里不再赘述)

 

 

 

 

war方式

 

为了省事和简单,在这里只做一个演示,直接使用jenkins的配置文件。在新的web工程当中,复制以下内容到 dependencies元素内:

<dependency>
   <groupId>org.jenkins-ci</groupId>
   <artifactId>executable-war</artifactId>
   <version>1.20</version>
   <scope>provided</scope>
  </dependency>

  <dependency>
   <groupId>org.jenkins-ci</groupId>
   <artifactId>winstone</artifactId>
   <version>0.9.10-jenkins-26</version>
   <scope>test</scope>
  </dependency>

 

 

然后再复制下面的内容到plugins元素中:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
     <archive>
      <manifest>
       <mainClass>Main</mainClass>
      </manifest>
     </archive>
    </configuration>
   </plugin>


   <plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <!-- version specified in grandparent pom -->
    <executions>
     <execution>
      <id>executable-war-header</id>
      <phase>generate-resources</phase>
      <goals>
       <goal>unpack-dependencies</goal>
      </goals>
      <configuration>
       <includeGroupIds>org.jenkins-ci</includeGroupIds>
       <includeArtifactIds>executable-war</includeArtifactIds>
       <includeScope>provided</includeScope>
       <includes>**/*.class</includes>
       <outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
      </configuration>
     </execution>
     <execution>
      <id>resgen</id>
      <phase>generate-resources</phase>
      <goals>
       <goal>copy</goal>
      </goals>
      <configuration>
       <artifactItems>
        <!-- dependencies that goes to unusual locations -->
        <artifactItem>
         <groupId>org.jenkins-ci</groupId>
         <artifactId>winstone</artifactId>
         <outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
         <destFileName>winstone.jar</destFileName>
        </artifactItem>
       </artifactItems>
       <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/plugins</outputDirectory>
       <stripVersion>true</stripVersion>
       <overWriteIfNewer>true</overWriteIfNewer>
       <overWriteReleases>false</overWriteReleases>
       <overWriteSnapshots>true</overWriteSnapshots>
      </configuration>
     </execution>
    </executions>
   </plugin>

 

 

执行mvn install完成打包。

 

NOTE:如果要是在真实的项目中使用,代码是必须要改的,可以到 jenkins  的官网把源代码下载下来然后进行编辑,不过提醒一下,需要使用GIT,不支持SVN。

仓库地址:https://github.com/jenkinsci/jenkins.git

也可以直接下载 :  https://nodeload.github.com/jenkinsci/jenkins/zipball/jenkins-1.425/jenkinsci-jenkins-jenkins-1.425-0-g3150431.zip

或者:http://mirrors.jenkins-ci.org/

 

猜你喜欢

转载自five.iteye.com/blog/1719971