使用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项目

 

 

     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/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Winston是一个非常流行的Node.js日志库,可以轻松地在Vue项目中使用它来记录日志。以下是在Vue项目中使用Winston的步骤: 1. 安装Winston:在终端中使用以下命令安装Winston: ``` npm install winston ``` 2. 创建一个日志文件:在你的Vue项目根目录下创建一个logs文件夹,并在其中创建一个名为app.log的日志文件。 3. 创建Winston实例:在你的Vue项目中创建一个名为logger.js的文件,并在其中创建一个Winston实例,配置日志记录器。以下是一个示例配置: ``` const winston = require('winston'); const path = require('path'); const logger = winston.createLogger({ level: 'info', format: winston.format.combine( winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), winston.format.printf(info => `${info.timestamp} [${info.level.toUpperCase()}] ${info.message}`) ), transports: [ new winston.transports.File({ filename: path.join(__dirname, '../logs/app.log') }) ] }); module.exports = logger; ``` 此配置将日志记录级别设置为info,并将日志记录到我们之前创建的app.log文件中。它还使用Winston的格式化选项,以便在日志中包含时间戳。 4. 在Vue组件中使用日志记录器:在Vue组件中导入logger.js文件,并在需要记录日志的地方使用logger实例。例如: ``` import logger from './logger'; export default { name: 'MyComponent', created() { logger.info('Component created'); }, methods: { handleClick() { logger.warn('Button clicked'); } } }; ``` 在此示例中,我们在组件的created钩子中记录了一条“Component created”消息,并在按钮的单击事件处理程序中记录了一条“Button clicked”消息。这些消息将被记录到我们之前创建的app.log文件中。 这就是在Vue项目中使用Winston记录日志的基本步骤。你可以根据需要调整Winston配置,以满足你的特定需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值