使用maven-assembly-plugin打包zip工程

使用Maven对Web项目进行打包,默认为war包;但有些时候,总是希望打成zip包(亦或其他压缩包,类似tomcat的那种目录结构,直接执行bin/startup.sh就可以),maven-war-plugin插件就无能为力了,这时就用到了maven-assembly-plugin插件了


该插件能打包成指定格式分发包,更重要的是能够自定义包含/排除指定的目录或文件(遗留项目中,过滤配置文件时,或者仅仅需要发布图片或者CSS/JS等指定类型文件时,发挥作用)


一:创建maven项目,目录结构如下:

使用maven-assembly-plugin打包zip工程_第1张图片

我们要的结果就是:

把bin,conf,lib,logs,work目录整个打成一个zip目录(就想tomcat一样)lib里面放置的是自己的代码打包和一些依赖的jar

logs是日志目录,bin是启动脚本


二:pom.xml内容:


	4.0.0

	com.lala
	myjetty
	1.0.0
	jar

	myjetty
	http://maven.apache.org

	
		UTF-8
	

	
		
			junit
			junit
			4.12
			test
		
		
			org.eclipse.jetty
			jetty-webapp
			9.3.0.v20150612
		
	

	
		
			
				src/main/conf
			
			
				src/main/resources
			
		
		
			
				org.apache.maven.plugins
				maven-resources-plugin
				2.7
				
					UTF-8
				
			
			
				org.apache.maven.plugins
				maven-assembly-plugin
				2.5.5
				
					UTF-8
					false
					
						src/main/assemble/package.xml
					
				
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.3
				
					1.8
					1.8
					true
				
			
		
	



三:另外使用jetty写了一个嵌入式的demo,代码如下:

package com.lala.tomcat;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.eclipse.jetty.server.Server;

public class App 
{
	static Properties getSystemProps()
	{
		Properties props = new Properties();

		InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("server.properties");
		
		try {
			props.load(input);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return props;
	}
    public static void main( String[] args ) throws Exception
    {
    	Properties props = getSystemProps();
    	Object prot = props.get("server.port");
    	if(prot == null)
    	{
    		System.out.println("port is empty");
    		System.exit(1);
    	}
    	Server server = new Server(Integer.valueOf(prot.toString()));
    	server.setHandler(new BookHandler());
        server.start();
        server.join();
    }
}

package com.lala.tomcat;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

public class BookHandler extends AbstractHandler 
{
	private static final Logger LOG = Log.getLogger(BookHandler.class);
	
	public void handle(String target, Request baseRequest,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		
		LOG.info("request income : url=" + target);
		String msg = "";
		if("/".equals(target))
		{
			msg = "world";
		}
		else
		{
			msg = target;
		}

		response.setContentType("text/html;charset=utf-8");

		response.setStatus(HttpServletResponse.SC_OK);

		baseRequest.setHandled(true);

		response.getWriter().println("

Hello "+msg+"

"); } }

conf目录下的server.properties内容为:

server.port=9696


assemble目录下的package.xml内容为:


    package
    
        zip
    
    true
    
        
            src/main/bin
            bin
        
        
            src/main/conf
            conf
        
        
            src/main/logs
            logs
        
        
            src/main/work
            work
        
    
    
        
            lib
            runtime
        
    

bin目录下的myjetty.sh内容为:

#!/bin/bash
  
if [ "$JAVA_HOME" = "" ]; then
  echo "Error: JAVA_HOME is not set."
  exit 1
fi

bin=`dirname "$0"`

export MYJETTY_HOME=`cd $bin/../; pwd`

MYJETTY_CONF_DIR=$MYJETTY_HOME/conf
CLASSPATH="${MYJETTY_CONF_DIR}"

for f in $MYJETTY_HOME/lib/*.jar; do
  CLASSPATH=${CLASSPATH}:$f;
done

LOG_DIR=${MYJETTY_HOME}/logs

CLASS=com.lala.tomcat.App
nohup ${JAVA_HOME}/bin/java -classpath "$CLASSPATH" $CLASS > ${LOG_DIR}/myjetty.out 2>&1 < /dev/null &


五:

最后,执行

mvn clean assembly:assembly

就会在target目录下的生成myjetty-1.0.0.zip文件,拷贝到linux

unzip myjetty-1.0.0.zip

cd myjetty-1.0.0/bin

sh myjetty.sh即可启动项目


默认端口为:9696

在浏览器上访问

http://127.0.0.1:9696/admin 即可看到输出


你可能感兴趣的:(Apache,Maven实战,Apache,maven实战)