http://webtide.intalio.com/2012/10/jetty-9-updated-websocket-api/

Creating WebSockets in Jetty is even easier with Jetty 9!

While the networking gurus in Jetty have been working on the awesome improvements to the I/O layers in core Jetty 9, the WebSocket fanatics in the community have been working on making writing WebSockets even easier.

The initial WebSocket implementation in Jetty was started back in November of 2009, well before the WebSocket protocol was finalized.

It has grown in response to Jetty’s involvement with the WebSocket draft discussions to the finalization of RFC6455, and onwards into the changes being influenced on our design as a result of WebSocket extensions drafts such as x-webkit-perframe-deflatepermessage-deflate, fragment, andongoing mux discussions.

The Jetty 7.x and Jetty 8.x codebases provided WebSockets to developers required a complex set of knowledge about how WebSockets work and how Jetty implemented WebSockets.  This complexity was as a result of this rather organic growth of WebSocket knowledge around intermediaries and WebSocket Extensions impacted the original design.

With Jetty 9.x we were given an opportunity to correct our mistakes.

The new WebSockets API in Jetty 9.x

Note: this information represents what is in the jetty-9 branch on git, which has changed in small but important ways since 9.0.0.M0 was released.

With the growing interest in next generation protocols like SPDY and HTTP/2.0, along with evolving standards being tracked for Servlet API 3.1 and Java API for WebSockets (JSR-356), the time Jetty 9.x was at hand.  We dove head first into cleaning up the codebase, performing some needed refactoring, and upgrading the codebase to Java 7.

Along the way, Jetty 9.x started to shed the old blocking I/O layers, and all of the nasty logic surrounding it, resulting on a Async I/O focused Jetty core.  We love this new layer, and we expect you will to, even if you don’t see it directly.  This change benefits Jetty with a smaller / cleaner / easier to maintain and test codebase, along with various performance improvements such as speed, CPU use, and even less memory use.

In parallel, the Jetty WebSocket codebase changed to soak up the knowledge gained in our early adoption of WebSockets and also to utilize the benefits of the new Jetty Async I/O layers better.   It is important to note that Jetty 9.x WebSockets is NOT backward compatible with prior Jetty versions.

The most significant changes:

  • Requires Java 7
  • Only supporting WebSocket version 13 (RFC-6455)
  • Artifact Split

The monolithic jetty-websocket artifact has been to split up the various websocket artifacts so that developers can pick and choose what’s important to them.

The new artifacts are all under the org.eclipse.jetty.websocket groupId on maven central.

  • websocket-core.jar – where the basic API classes reside, plus internal implementation details that are common between server & client.
  • websocket-server.jar – the server specific classes
  • websocket-client.jar – the client specific classes
  • Only 1 Listener now (WebSocketListener)
  • Now Supports Annotated WebSocket classes
  • Focus is on Messages not Frames

In our prior WebSocket API we assumed, incorrectly, that developers would want to work with the raw WebSocket framing.   This change brings us in line with how every other WebSocket API behaves, working with messages, not frames.

  • WebSocketServlet only configures for a WebSocketCreator

This subtle change means that the Servlet no longer creates websockets of its own, and instead this work is done by the WebSocketCreator of your choice (don’t worry, there is a default creator).
This is important to properly support the mux extensions and future Java API for WebSockets (JSR-356)

Jetty 9.x WebSockets Quick Start:

Before we get started, some important WebSocket Basics & Gotchas

  1. A WebSocket Frame is the most fundamental part of the protocol, however it is not really the best way to read/write to websockets.
  2. A WebSocket Message can be 1 or more frames, this is the model of interaction with a WebSocket in Jetty 9.x
  3. A WebSocket TEXT Message can only ever be UTF-8 encoded. (it you need other forms of encoding, use a BINARY Message)
  4. A WebSocket BINARY Message can be anything that will fit in a byte array.
  5. Use the WebSocketPolicy (available in the WebSocketServerFactory) to configure some constraints on what the maximum text and binary message size should be for your socket (to prevent clients from sending massive messages or frames)

First, we need the servlet to provide the glue.

We’ll be overriding the configure(WebSocketServerFactory) here to configure a basic MyEchoSocket to run when an incoming request to upgrade occurs.

package examples;  import org.eclipse.jetty.websocket.server.WebSocketServerFactory; import org.eclipse.jetty.websocket.server.WebSocketServlet;  public class MyEchoServlet extends WebSocketServlet {     @Override     public void configure(WebSocketServerFactory factory)     {         // register a socket class as default         factory.register(MyEchoSocket.class);     } }

The responsibility of your WebSocketServlet class is to configure theWebSocketServerFactory.     The most important aspect is describing how WebSocket implementations are to be created when request for new sockets arrive.  This is accomplished by configuring an appropriate WebSocketCreator object.  In the above example, the default WebSocketCreator is being used to register a specific class to instantiate on each new incoming Upgrade request.

If you wish to use your own WebSocketCreator implementation, you can provide it during this configure step.

Check the examples/echo to see how this is done with factory.setCreator() andEchoCreator.

Note that request for new websockets can arrive from a number of different code paths, not all of which will result in your WebSocketServlet being executed.  Mux for example will result in a new WebSocket request arriving as a logic channel within the MuxExtension itself.

As for implementing the MyEchoSocket, you have 3 choices.

  1. Implementing Listener
  2. Using an Adapter
  3. Using Annotations

Choice 1: implementing WebSocketListener interface.

Implementing WebSocketListener is the oldest and most fundamental approach available to you for working with WebSocket in a traditional listener approach (be sure you read the other approaches below before you settle on this approach).
It is your responsibility to handle the connection open/close events appropriately when using the WebSocketListener. Once you obtain a reference to the WebSocketConnection, you have a variety of NIO/Async based write() methods to write content back out the connection.

package examples;  import java.io.IOException;  import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.websocket.core.api.WebSocketConnection; import org.eclipse.jetty.websocket.core.api.WebSocketException; import org.eclipse.jetty.websocket.core.api.WebSocketListener;  public class MyEchoSocket implements WebSocketListener {     private WebSocketConnection outbound;      @Override     public void onWebSocketBinary(byte[] payload, int offset,                                   int len)     {         /* only interested in text messages */     }      @Override     public void onWebSocketClose(int statusCode, String reason)     {         this.outbound = null;     }      @Override     public void onWebSocketConnect(WebSocketConnection connection)     {         this.outbound = connection;     }      @Override     public void onWebSocketException(WebSocketException error)     {         error.printStackTrace();     }      @Override     public void onWebSocketText(String message)     {         if (outbound == null)         {             return;         }          try         {             String context = null;             Callback callback = new FutureCallback<>();             outbound.write(context,callback,message);         }         catch (IOException e)         {             e.printStackTrace();         }     } }

Choice 2: extending from WebSocketAdapter

Using the provided WebSocketAdapter, the management of the Connection is handled for you, and access to a simplified WebSocketBlockingConnection is also available (as well as using the NIO based write signature seen above)

package examples;  import java.io.IOException; import org.eclipse.jetty.websocket.core.api.WebSocketAdapter;  public class MyEchoSocket extends WebSocketAdapter {     @Override     public void onWebSocketText(String message)     {         if (isNotConnected())         {             return;         }          try         {             // echo the data back             getBlockingConnection().write(message);         }         catch (IOException e)         {             e.printStackTrace();         }     } }

Choice 3: decorating your POJO with @WebSocket annotations.

This the easiest WebSocket you can create, and you have some flexibility in the parameters of the methods as well.

package examples;  import java.io.IOException;  import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.websocket.core.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.core.annotations.WebSocket; import org.eclipse.jetty.websocket.core.api.WebSocketConnection;  @WebSocket(maxTextSize = 64 * 1024) public class MyEchoSocket {     @OnWebSocketMessage     public void onText(WebSocketConnection conn, String message)     {         if (conn.isOpen())         {             return;         }         try         {             conn.write(null,new FutureCallback(),message);         }         catch (IOException e)         {             e.printStackTrace();         }     } }

The annotations you have available:
@OnWebSocketMessage: To receive websocket message events.

Examples:

  @OnWebSocketMessage   public void onTextMethod(String message) {      // simple TEXT message received   }    @OnWebSocketMessage   public void onTextMethod(WebSocketConnection connection,                             String message) {      // simple TEXT message received, with Connection       // that it occurred on.   }    @OnWebSocketMessage   public void onBinaryMethod(byte data[], int offset,                               int length) {      // simple BINARY message received   }    @OnWebSocketMessage   public void onBinaryMethod(WebSocketConnection connection,                               byte data[], int offset,                               int length) {      // simple BINARY message received, with Connection       // that it occurred on.   }

@OnWebSocketConnect: To receive websocket connection connected event (will only occur once).

Example:

  @OnWebSocketConnect   public void onConnect(WebSocketConnection connection) {      // WebSocket is now connected   }

@OnWebSocketClose: To receive websocket connection closed events (will only occur once).

Example:

  @OnWebSocketClose   public void onClose(int statusCode, String reason) {      // WebSocket is now disconnected   }    @OnWebSocketClose   public void onClose(WebSocketConnection connection,                        int statusCode, String reason) {      // WebSocket is now disconnected   }

@OnWebSocketFrame: To receive websocket framing events (read only access to the rawFrame details).

Example:

  @OnWebSocketFrame   public void onFrame(Frame frame) {      // WebSocket frame received   }    @OnWebSocketFrame   public void onFrame(WebSocketConnection connection,                        Frame frame) {      // WebSocket frame received   }

One More Thing … The Future

We aren’t done with our changes to Jetty 9.x and the WebSocket API, we are actively working on the following features as well…

  • Mux Extension

The multiplex extension being drafted will allow for multiple virtual WebSocket connections over a single physical TCP/IP connection.  This extension will allow browsers to better utilize their connection limits/counts, and allow web proxy intermediaries to bundle multiple websocket connections to a server together over a single physical connection.

  • Streaming APIs

There has been some expressed interest in providing read and write of text or binary messages using the standard Java IO Writer/Reader (for TEXT messages) and OutputStream/InputStream (for BINARY messages) APIs.

Current plans for streamed reading includes new @OnWebSocketMessage interface patterns.

  // In the near future, we will have the following some Streaming   // forms also available.  This is a delicate thing to    // implement and currently does not work properly, but is    // scheduled.    @OnWebSocketMessage   public void onTextMethod(Reader stream) {      // TEXT message received, and reported to your socket as a      // Reader. (can handle 1 message, regardless of size or       // number of frames)   }    @OnWebSocketMessage   public void onTextMethod(WebSocketConnection connection,                             Reader stream) {      // TEXT message received, and reported to your socket as a      // Reader. (can handle 1 message, regardless of size or       // number of frames).  Connection that message occurs      // on is reported as well.   }    @OnWebSocketMessage   public void onBinaryMethod(InputStream stream) {      // BINARY message received, and reported to your socket      // as a InputStream. (can handle 1 message, regardless      // of size or number of frames).   }    @OnWebSocketMessage   public void onBinaryMethod(WebSocketConnection connection,                               InputStream stream) {      // BINARY message received, and reported to your socket      // as a InputStream. (can handle 1 message, regardless      // of size or number of frames).  Connection that       // message occurs on is reported as well.   }

And for streaming writes, we plan to provide Writer and OutputStream implementations that simply wrap the provided WebSocketConnection.

  • Android Compatible Client Library

While Android is currently not Java 7 compatible, a modified websocket-client library suitable for use with Android is on our TODO list.

  • Support Java API for WebSocket API (JSR356)

We are actively tracking the work being done with this JSR group, it is coming, but is still some way off from being a complete and finished API (heck, the current EDR still doesn’t support extensions). Jetty 9.x will definitely support it, and we have tried to build our Jetty 9.x WebSocket API so that the the Java API for WebSockets can live above it.

posted @ 2014-06-10 11:55 小马歌 阅读(858) | 评论 (0)编辑 收藏
 

from:http://blog.csdn.net/marshalchen/article/details/12401597

Go语言的hello world!代码:

  1. package main  
  2.   
  3. import "fmt"  
  4.   
  5. func main() {  
  6.     fmt.Println("Hello, 世界")  
  7. }  


接下来为大家带来,Go开发环境的安装。

  首先是安装Go,这里有很详细的安装说明,http://code.google.com/p/golang-china/wiki/Install 或者http://golang.org/doc/install

下面我们在window下面安装,google有提供win安装包,对于新手还是非常简单的!

https://code.google.com/p/go/downloads/list

直接下一步.......安装非常简单!

  安装好Go以后,我们就可以搭建开发环境了,这里我用的是 Sublime Text 2 + GoSublime + gocode。对于不了解Sublime Text 2的朋友,可以看看官网:http://www.sublimetext.com/(总的来说是一个轻量级,用起来很方便的工具)

1. 下载 Sublime Text 2,地址如下:http://www.sublimetext.com/

2. 解压以后,双击 sublime_text,就可以使用 Sublime Text 2 了。

3. 安装 Package Control,在打开 Sublime Text 2以后,按下快捷键 Ctrl + `,打开命令窗行,这个按键在Tab键的上面,我刚开始还没找到,呵呵。输入以下内容,并回车:

  import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'

4. 重启Sublime Text 2后,就可以发现在 Preferences菜单下,多出一个菜单项 Package Control。

5.现在安装GoSublime插件了,按住Ctrl+Shilft+p会弹出一个对话框


输入install回车弹出一个安装包的对话框


如入GoSublime选择GoSublime回车

输入Go build选中回车(这个属于可选)

搞定,GoSublime安装成功。

6.下面安装gocode

打开控制台,输入以下内容:

    go get github.com/nsf/gocode

    go install github.com/nsf/gocode

也可以去github下载https://github.com/nsf/gocode.git(要安装google的git版本管理工具)

  安装完成后,我们可以在 go/bin 目录下,发现多出了个 gocode 文件。(一定要放在bin目录下)

7. 修改GoSublime配置:在 Preferences菜单下,找到Package Settings,然后找到 GoSublime,再往下找到 Settings - Default。再打开的文件中,添加如下配置,并保存:


好了,到目前为止,开发环境搭建完成。

打开 Sublime Text 2,新建 helloworld.go,编写代码如下:

见证Go代码自动提示的时刻了

输入一个p


回车(enter键)


main方法,包自动给你生成了。

下面是一个打印的例子:




按下快捷键 Ctrl + b 界面下方会出现如下界面:



输入 go build hello.go


运行,同样 按下快捷键 Ctrl + b 界面下方会出现如下界面,输入 hello回车 。如图:


好了,到现在,开发环境就搭建完毕了,希望大家也来学习Go这门语言。


新手入门参考:

新手入门api是我见过的最好的新手入门文档,希望go能发扬光大。


上述内容转载http://blog.csdn.net/love_se/article/details/7754274 



但是linux下需要再变通的配置了

环境变量部分:

修改/etc/profile或者.bashrc或者.bash_profile


如同JDK般加入

export JAVA_HOME=/usr/lib/jvm/java-7-sun  

export JRE_HOME=${JAVA_HOME}/jre  
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib  
export PATH=${JAVA_HOME}/bin:$PATH 

posted @ 2014-05-20 16:45 小马歌 阅读(246) | 评论 (0)编辑 收藏
 
     摘要: It is a well known fact that HTTP(Hypertext Transfer Protocol) is a stateless request-response protocol. This simple design of the HTTP protocol makes it very scalable but inefficient and un...  阅读全文
posted @ 2014-05-19 13:38 小马歌 阅读(230) | 评论 (0)编辑 收藏
 
* Java-WebSocket 
http://java-websocket.org/
"A barebones WebSocket client and server implementation written in 100% Java"
* jWebSocket  
http://jwebsocket.org/
"jWebSocket is a pure Java/JavaScript high speed bidirectional communication solution for the Web  server and various clients"
* Grizzly 
http://grizzly.java.net/
Framework for building web servers of various scales and types incl Web Socket client & server apis.
* Apache Tomcat 7
Java Web Container
http://tomcat.apache.org/download-70.cgi
new api & sup[port in 7.0.7: http://tomcat.apache.org/tomcat-7.0-doc/api/index.html?org/apache/catalina/websocket/package-summary.html
* Glassfish
http://glassfish.java.net/
WebSockets via Grizzly and latterly WebSocket SDK also.
* Autobahn  
http://autobahn.ws/developers
Including Android web socket client
* WeberKnecht
http://code.google.com/p/weberknecht/ 
Project with Java SE / Android clients
* Jetty
http://www.eclipse.org/jetty/
Java Web Container including WebSockets support, client and server apis.
JBoss
Java EE App server
https://github.com/mikebrock/jboss-websockets
(in development)
* Caucho Resin
Java Server including web container with WebSockets support. 
see for example: http://www.caucho.com/resin-4.0/examples/websocket-java/index.xtp
* Kaazing WebSocket Gateway
Gateway products, includes Java developer APIs for web sockets.
e.g. http://tech.kaazing.com/documentation/html5/3.3/o_dev_java.html
* WebSocket SDK
Prototyping high level APIs & annotations for Web Sockets.
http://java.net/projects/websocket-sdk
* Webbit http://webbitserver.org/
A Java event based WebSocket and HTTP server
http://groups.google.com/group/webbit  / http://webbitserver.org/
* Servlet 3.1
Proposing API for server side of Http Upgrade mechanism to enable websockets support to be build on the Servlet Container
e.g.: http://java.net/projects/servlet-spec/lists/jsr340-experts/archive/2012-02/message/8
*Atmosphere:
"The only Portable WebSocket/Comet Framework supporting Scala, Groovy and Java"
https://github.com/Atmosphere/atmosphere
*websockets4j:
"Websockets4j Is a simple implementation of the WebSockets protocol. Currently supports the plain text version of Drafts 75 and 76 of the protocol."
http://code.google.com/p/websockets4j/
*GNU WebSocket4J:
"GNU WebSocket4J is a WebSocket protocol implementation in Java. It allows you to build Web applications that interact with applications running in a JVM. GNU WebSocket4J implements both server and client side of the protocol, so it can be used to build both WebSocket servers and clients."
http://maarons.alwaysdata.net/software/GNU/WebSocket4J/
* Netty
Asynchronous event-driven network application framework, including websockets amongst many other things.
 http://netty.io/ / http://netty.io/docs/stable/api/
TorqueBox
Ruby application server built on JBoss AS7 and JRuby.
http://torquebox.org/documentation/
http://torquebox.org/news/2011/08/15/websockets-stomp-and-torquebox/
posted @ 2014-05-19 13:20 小马歌 阅读(290) | 评论 (0)编辑 收藏
 
     摘要: from:http://www.oracle.com/technetwork/articles/java/jsr356-1937161.htmlJSR 356, Java API for WebSocketby Johan VosLearn how to integrate WebSockets into your applications.Published April 2013For many...  阅读全文
posted @ 2014-05-19 13:18 小马歌 阅读(531) | 评论 (0)编辑 收藏
 
     摘要: Profiles是maven的一个很关键的术语:profile是用来定义一些在build lifecycle中使用的environmental variations,profile可以设置成在不同的环境下激活不同的profile(例如:不同的OS激活不同的profile,不同的JVM激活不同的profile,不同的dabase激活不同的profile等等)。 定义Profiles&nbs...  阅读全文
posted @ 2014-05-16 12:55 小马歌 阅读(257) | 评论 (0)编辑 收藏
 
     摘要: 概述继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能。现在你无须让 Controller 继承任何接口,无需在 XML 配置文件中定义请求和 Controller 的映射关系,仅仅使用注解就可以让一个 POJO 具有 Controller 的绝大部分功能 —— Spring MVC 框架...  阅读全文
posted @ 2014-05-14 17:06 小马歌 阅读(283) | 评论 (0)编辑 收藏
 

如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码。然而,Eclipse工作空间(workspace)的缺省字符编码是操作系统缺省的编码,简体中文操作系统 (Windows XP、Windows 2000简体中文)的缺省编码是GB18030,在此工作空间中建立的工程编码是GB18030,工程中建立的java文件也是GB18030。

如果要使新建立工程、java文件直接使UTF-8则需要做以下工作: 

1、windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右侧Text file encoding,选择Other,改变为UTF-8,以后新建立工程其属性对话框中的Text file encoding即为UTF-8。 

2、windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Content Types,右侧Context Types树,点开Text中每一个子项,并将其编码设置为"UTF-8",点update!

 其他java应用开发相关的文件如:properties、XML等已经由Eclipse缺省指定,分别为ISO8859-1,UTF-8,如开发中确需改变编码格式则可以在此指定。 

3、window-->preference-->MyEclipse-->Files and Editors,将每个子项的"Encoding"改为"ISO 10645/Unicode(UTF-8)",点Apply! 

4、经过上述三步,新建java文件即为UTF-8编码,Eclipse编译、运行、调试都没问题,但是做RCP应用的Product输出时、或者插件输出时,则总是出错,要么不能编译通过(输出时要重新compile)、要么输出的插件运行时中文显示乱码。此时需要再RCP应用、或插件Plugin工程的build.properties中增加一行,javacDefaultEncoding.. = UTF-8。让输出时编译知道java源文件时UTF-8编码。这个设置需要保证所有的java源文件时UTF-8编码格式,如果不全是,可以参考 Eclipse帮中(Plug-in Development Environment Guide > Reference > Feature and Plug-in Build configuration),建议全部java源文件是UTF-8编码。 

如果插件开发、RCP应用开发原来基于其他编码,如GB18030,想转换为UTF-8,则首先,做以上工作;然后通过查找编码转换工具,如基于 iconv的批量转换工具,将原编码转换

posted @ 2014-05-13 21:07 小马歌 阅读(25727) | 评论 (0)编辑 收藏
 
  1. <build>  
  2.         <sourceDirectory>src/main/java</sourceDirectory>  
  3.         <plugins>  
  4.             <plugin>  
  5.                 <groupId>org.apache.maven.plugins</groupId>  
  6.                 <artifactId>maven-compiler-plugin</artifactId>  
  7.                 <configuration>  
  8.                     <defaultLibBundleDir>lib</defaultLibBundleDir>  
  9.                     <source>1.5</source>  
  10.                     <target>1.5</target>  
  11.                     <encoding>UTF-8</encoding>  
  12.                 </configuration>  
  13.             </plugin>  
  14.             <plugin>  
  15.                 <groupId>org.apache.maven.plugins</groupId>  
  16.                 <artifactId>maven-jar-plugin</artifactId>  
  17.                 <configuration>  
  18.                     <archive>  
  19.                         <manifest>  
  20.                             <addClasspath>true</addClasspath>  
  21.                             <classpathPrefix></classpathPrefix>  
  22.                             <mainClass>com.xx.xx.xx</mainClass>  
  23.                         </manifest>  
  24.                     </archive>  
  25.                 </configuration>  
  26.             </plugin>  
  27.             <plugin>  
  28.                 <groupId>org.apache.maven.plugins</groupId>  
  29.                 <artifactId>maven-dependency-plugin</artifactId>  
  30.                 <executions>  
  31.                     <execution>  
  32.                         <id>copy</id>  
  33.                         <phase>install</phase>  
  34.                         <goals>  
  35.                             <goal>copy-dependencies</goal>  
  36.                         </goals>  
  37.                         <configuration>  
  38.                             <outputDirectory>  
  39.                                 ${project.build.directory}  
  40.                             </outputDirectory>  
  41.                         </configuration>  
  42.                     </execution>  
  43.                 </executions>  
  44.             </plugin>  
  45.             <plugin>  
  46.                 <groupId>org.apache.maven.plugins</groupId>  
  47.                 <artifactId>maven-resources-plugin</artifactId>  
  48.                 <version>2.2</version>  
  49.                 <configuration>  
  50.                     <encoding>UTF-8</encoding>  
  51.                 </configuration>  
  52.             </plugin>  
  53.         </plugins>  
  54.     </build>  
posted @ 2014-05-13 17:36 小马歌 阅读(338) | 评论 (0)编辑 收藏
 

参考官方网站:http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

方法一:将pom.xml引入的jar包打到zip文件夹中

  1、pom.xml的配置

<!-- 打包配置 start --> 	<build> 		<plugins> 			<plugin> 				<!-- NOTE: We don't need a groupId specification because the group is  					org.apache.maven.plugins ...which is assumed by default. --> 				<artifactId>maven-assembly-plugin</artifactId> 				<version>2.4</version> 				<configuration> 					<descriptors> 						<descriptor>src/main/assembly/src.xml</descriptor> 					</descriptors> 					<descriptorRefs> 						<descriptorRef>jar-with-dependencies</descriptorRef> 					</descriptorRefs> 				</configuration> 				<executions> 					<execution> 						<id>make-assembly</id> <!-- this is used for inheritance merges --> 						<phase>package</phase> <!-- bind to the packaging phase --> 						<goals> 							<goal>single</goal> 						</goals> 					</execution> 				</executions> 			</plugin> 		</plugins> 	</build> 

  2、src.xml文件内容

<assembly 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/assembly-1.0.0.xsd">     <id>package</id>     <formats>         <format>zip</format>     </formats>     <includeBaseDirectory>true</includeBaseDirectory>     <fileSets>         <fileSet>             <directory>src/main/bin</directory>             <outputDirectory>/</outputDirectory>         </fileSet>         <fileSet>             <directory>src/main/config</directory>             <outputDirectory>config</outputDirectory>         </fileSet>     </fileSets>     <dependencySets>         <dependencySet>             <outputDirectory>lib</outputDirectory>             <scope>runtime</scope>         </dependencySet>     </dependencySets> </assembly> 

  方法1能把所有引入的包打成jar包,方便自己保存和引入。

  3、最后一步

  cmd进入在项目根目录下,键入“mvn package”,OK完事!

方法二:将pom引入的jar包打进你的项目工程jar包内

  1、pom.xml的配置

<!-- 打包配置 start --> 	<build> 		<!-- <finalName>im-dal-service</finalName> 		<sourceDirectory>src/main/java</sourceDirectory> 		<resources> 			控制资源文件的拷贝 			<resource> 				<directory>src/main/resources</directory> 				<targetPath>${project.build.directory}</targetPath> 			</resource> 		</resources> --> 		<plugins> 			<plugin> 				<artifactId>maven-assembly-plugin</artifactId> 				<configuration> 					<descriptors> 						<descriptor>src/main/assembly/src.xml</descriptor> 					</descriptors> 				</configuration> 			</plugin> 		</plugins>  	</build> 

  2、src.xml

<?xml version="1.0" encoding="UTF-8"?> <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">   <id>jar-with-dependencies</id>   <formats>     <format>jar</format>   </formats>   <includeBaseDirectory>false</includeBaseDirectory>   <dependencySets>     <dependencySet>       <unpack>false</unpack>       <scope>runtime</scope>     </dependencySet>   </dependencySets>   <fileSets>     <fileSet>       <directory>/lib</directory>     </fileSet>   </fileSets> </assembly> 

  3、最后一步

  cmd进入在项目根目录下,键入“mvn assembly:assembly”,OK完事!

分享到:
posted @ 2014-05-13 17:35 小马歌 阅读(892) | 评论 (0)编辑 收藏
仅列出标题
共95页: First 上一页 19 20 21 22 23 24 25 26 27 下一页 Last