使用maven-assembly-plugin打包zipproject
Posted gavanwanggw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用maven-assembly-plugin打包zipproject相关的知识,希望对你有一定的参考价值。
该插件能打包成指定格式分发包,更重要的是可以自己定义包括/排除指定的文件夹或文件(遗留项目中,过滤配置文件时,或者只须要公布图片或者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的主要内容,如果未能解决你的问题,请参考以下文章
使用maven-assembly-plugin打包zipproject
SpringBoot项目使用maven-assembly-plugin根据不同环境打包成tar.gz
通过 maven-assembly-plugin 打包在 jar 中包含 spring xml
maven-assembly-plugin打包可执行的jar包