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

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


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


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

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

我们要的结果就是:

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

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


二:pom.xml内容:

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

	<groupId>com.lala</groupId>
	<artifactId>myjetty</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>

	<name>myjetty</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-webapp</artifactId>
			<version>9.3.0.v20150612</version>
		</dependency>
	</dependencies>

	<build>
		<resources>
			<resource>
				<directory>src/main/conf</directory>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.7</version>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.5.5</version>
				<configuration>
					<encoding>UTF-8</encoding>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptors>
						<descriptor>src/main/assemble/package.xml</descriptor>
					</descriptors>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<verbose>true</verbose>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


三:另外使用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("<h1>Hello "+msg+"</h1>");
	}
}

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

server.port=9696


assemble目录下的package.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>bin</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/conf</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/logs</directory>
            <outputDirectory>logs</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/work</directory>
            <outputDirectory>work</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

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 即可看到输出