使用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. Mongodb的windows服务安装和卸载

    不用 InstallUtil.exe,直接用mongod.exe做就可以: 安装:mongod --dbpath "C:\mongodb\db" --logpath "C ...

  2. Go语言之进阶篇操作redis

    1.windows安装redis 软件包下载地址: https://github.com/MicrosoftArchive/redis/releases 1.1.安装--->下一步---> ...

  3. 初识EntityFramework6【转】

    http://www.cnblogs.com/wujingtao/p/5401132.html 什么是EF? EF是一种ORM(Object-relational mapping)框架,它能把我们在编 ...

  4. ElasticSearch客户端注解使用介绍

    The best elasticsearch highlevel java rest api-----bboss 1.ElasticSearch客户端bboss提供了一系列注解 @ESId  用于标识 ...

  5. C++模拟键盘消息

    实现功能:在现有DLL程序中向特定的EXE窗口中发送模拟键盘的消息 使用API根据窗口标题递归查找特定的窗口句柄,之后模拟调用. 注意:keybd_event函数不能在VS下使用,所以用SendInp ...

  6. 转:从头开始编写基于隐含马尔可夫模型HMM的中文分词器

    http://blog.csdn.net/guixunlong/article/details/8925990 从头开始编写基于隐含马尔可夫模型HMM的中文分词器之一 - 资源篇 首先感谢52nlp的 ...

  7. capwap学习笔记——初识capwap(二)

    2.5.1 AC发现机制 WTP使用AC发现机制来得知哪些AC是可用的,决定最佳的AC来建立CAPWAP连接. WTP的发现过程是可选的.如果在WTP上静态配置了AC,那么WTP并不需要完成AC的发现 ...

  8. (转)Unity3D研究院之Assetbundle的实战(六十三)

    上一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity3D研究院之Assetbundle的原理(六十一) 本篇文章我们将说说assetbundl ...

  9. React从0到1

    本篇将一直更新下去,写的多了,可能会拆成小章节,记录完整的学习笔记 github https://github.com/ae6623/ReactL

  10. PostBuildEvent

    <PostBuildEvent>CALL "%25VS90COMNTOOLS%25\vsvars32.bat" > NULL sn –Vr $(TargetFil ...