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

该插件能打包成指定格式分发包,更重要的是可以自己定义包括/排除指定的文件夹或文件(遗留项目中,过滤配置文件时,或者只须要公布图片或者CSS/JS等指定类型文件时,发挥作用)

一:创建maven项目。文件夹结构例如以下:

我们要的结果就是:

把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就可以启动项目

默认port为:9696

在浏览器上訪问

http://127.0.0.1:9696/admin 就可以看到输出

使用maven-assembly-plugin打包zipproject的更多相关文章

  1. 记录一次maven打包时将test目录下的类打包到jar中,Maven Assembly Plugin的使用

    今天有人问我打包后找不到主类,运行的类写在test中.按照常规,test目录下的文件不会打包到jar包中.(但是我测试一个springboot工程就可以,这里之后再研究) 具体解决如下 第一步:在po ...

  2. maven assembly plugin使用

    使用场景 在使用maven来管理项目时,项目除了web项目,还有可能为控制台程序,一般用于开发一些后台服务的程序.最近在工作中也遇到了这种场景,使用quartz开发一个任务调度程序.程序中依赖很多ja ...

  3. 使用Maven Assembly plugin将依赖打包进jar

    一个Eclipse的工程,在pom中配置了若干依赖,需要将pom中所有的依赖全部打包进一个jar包中,可以选择的方案有maven-assembly-plugin和fatjar.以前采用fatjar进行 ...

  4. java工程打成jar包 - 使用maven assembly插件打包及手动打包

    在java工程打包的过程中遇到过不少问题,现在总结一下.一种是典型的maven工程打包,依赖的jar包全都在pom.xml中指定,这种方式打包很方便:另一种是依赖了本机jar包(不能通过pom.xml ...

  5. Maven Assembly插件介绍

    转自:http://blueram.iteye.com/blog/1684070 已经写得挺好的,就不用重写了. 你是否想要创建一个包含脚本.配置文件以及所有运行时所依赖的元素(jar)Assembl ...

  6. maven assembly 配置详解

    Maven Assembly插件介绍 博客分类: 项目构建   你是否想要创建一个包含脚本.配置文件以及所有运行时所依赖的元素(jar)Assembly插件能帮你构建一个完整的发布包. Assembl ...

  7. Maven Assembly打包提示[WARNING] transitive dependencies if any will not be available

    maven assembly打包出现错误 [WARNING] The POM for com.flink.xxr:0.0.1-SNAPSHOT is invalid, transitive depen ...

  8. Maven項目打包報錯:Plugin execution not covered by lifecycle configuration

    Maven項目打包報錯:Plugin execution not covered by lifecycle configuration 使用Eclipse导入一个新的maven项目时不时的会遇到这个错 ...

  9. maven-使用assembly自定义打包

    用maven管理项目引用依赖很方便,但是打包的时候如果是web包还好,会直接把依赖的jar包打到lib目录中,如果是jar包的话,依赖默认是不打入进去的 这样如果运行环境中没有依赖的jar包,就麻烦了 ...

  10. maven源码打包

    1.打包时附加外部Jar包 <!--编译+外部 Jar打包-->          <plugin>            <artifactId>maven-co ...

随机推荐

  1. DBS-Tally book(记账本)

    ylbtech-dbs:DBS-Tally book(记账本) -- =============================================-- 记账本-- 模仿小程序“记账e”业 ...

  2. BusinessUnit, User, Role 中常用的APIs

    前段时间为了做项目调研,写了一些测试API的例子.这些API主要涉及这些模块: BusinessUnit, User, Role.把它分享出来,希望对大家的工作有所帮助. APIs No Module ...

  3. 使用mmap可以方便地添加共享内存

    使用mmap添加的共享内存. 局限: 只能在有亲属关系的进程之间使用. #include <stdio.h> #include <stdlib.h> #include < ...

  4. ASP.NET中Session的个人浅谈

    看到博客园的一个哥们写的面试经历,想到了面试中常问到的Session,一时手痒就谈下自己对Session的理解,这东西最开始在用户登录登出的时候用到过,后来一直没怎么用过,里面还是有很多知识点值得注意 ...

  5. Hadoop视频教程汇总

    一 慕课网 1.Hadoop大数据平台架构与实践--基础篇(已学习) 链接:https://www.imooc.com/learn/391 2.Hadoop进阶(已学习) 链接:https://www ...

  6. 平均值(Mean)、方差(Variance)、标准差(Standard Deviation) (转)

    http://blog.csdn.net/xidiancoder/article/details/71341345 平均值 平均值的概念很简单:所有数据之和除以数据点的个数,以此表示数据集的平均大小: ...

  7. w3cscholl的在线代码编辑工具2

    https://www.w3cschool.cn/tryrun/runcode?lang=c

  8. Core Java-多线程-线程的生命周期

    0. 在介绍线程前我们先看一下什么是进程? 进程是线程的母亲,如果在大学计算机课程里读过操作系统一定不会陌生. 所谓进程,它是计算机程序关于某数据集上的一次活动,是系统进行资源分配和调度的基本单位,是 ...

  9. LevelDB初体验测试

    最近工作需要找一个能使用磁盘存储数据,对写要求比较苛刻,需要每秒达100000TPS,读的时候需要能10000TPS左右,不能占用太多内存.单节点满足这个要求的常见有Redis.Memcached等, ...

  10. Postgresql中的数据类型大全

    一.数值类型: 下面是PostgreSQL所支持的数值类型的列表和简单说明: 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字 ...