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

使用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内容:

 

[html]  view plain  copy
 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>com.lala</groupId>  
  6.     <artifactId>myjetty</artifactId>  
  7.     <version>1.0.0</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>myjetty</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <dependencies>  
  18.         <dependency>  
  19.             <groupId>junit</groupId>  
  20.             <artifactId>junit</artifactId>  
  21.             <version>4.12</version>  
  22.             <scope>test</scope>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>org.eclipse.jetty</groupId>  
  26.             <artifactId>jetty-webapp</artifactId>  
  27.             <version>9.3.0.v20150612</version>  
  28.         </dependency>  
  29.     </dependencies>  
  30.   
  31.     <build>  
  32.         <resources>  
  33.             <resource>  
  34.                 <directory>src/main/conf</directory>  
  35.             </resource>  
  36.             <resource>  
  37.                 <directory>src/main/resources</directory>  
  38.             </resource>  
  39.         </resources>  
  40.         <plugins>  
  41.             <plugin>  
  42.                 <groupId>org.apache.maven.plugins</groupId>  
  43.                 <artifactId>maven-resources-plugin</artifactId>  
  44.                 <version>2.7</version>  
  45.                 <configuration>  
  46.                     <encoding>UTF-8</encoding>  
  47.                 </configuration>  
  48.             </plugin>  
  49.             <plugin>  
  50.                 <groupId>org.apache.maven.plugins</groupId>  
  51.                 <artifactId>maven-assembly-plugin</artifactId>  
  52.                 <version>2.5.5</version>  
  53.                 <configuration>  
  54.                     <encoding>UTF-8</encoding>  
  55.                     <appendAssemblyId>false</appendAssemblyId>  
  56.                     <descriptors>  
  57.                         <descriptor>src/main/assemble/package.xml</descriptor>  
  58.                     </descriptors>  
  59.                 </configuration>  
  60.             </plugin>  
  61.             <plugin>  
  62.                 <groupId>org.apache.maven.plugins</groupId>  
  63.                 <artifactId>maven-compiler-plugin</artifactId>  
  64.                 <version>3.3</version>  
  65.                 <configuration>  
  66.                     <source>1.8</source>  
  67.                     <target>1.8</target>  
  68.                     <verbose>true</verbose>  
  69.                 </configuration>  
  70.             </plugin>  
  71.         </plugins>  
  72.     </build>  
  73. </project>  



 

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

 

[java]  view plain  copy
 
  1. package com.lala.tomcat;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6.   
  7. import org.eclipse.jetty.server.Server;  
  8.   
  9. public class App   
  10. {  
  11.     static Properties getSystemProps()  
  12.     {  
  13.         Properties props = new Properties();  
  14.   
  15.         InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("server.properties");  
  16.           
  17.         try {  
  18.             props.load(input);  
  19.         } catch (IOException e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.           
  23.         return props;  
  24.     }  
  25.     public static void main( String[] args ) throws Exception  
  26.     {  
  27.         Properties props = getSystemProps();  
  28.         Object prot = props.get("server.port");  
  29.         if(prot == null)  
  30.         {  
  31.             System.out.println("port is empty");  
  32.             System.exit(1);  
  33.         }  
  34.         Server server = new Server(Integer.valueOf(prot.toString()));  
  35.         server.setHandler(new BookHandler());  
  36.         server.start();  
  37.         server.join();  
  38.     }  
  39. }  
[java]  view plain  copy
 
  1. package com.lala.tomcat;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.eclipse.jetty.server.Request;  
  10. import org.eclipse.jetty.server.handler.AbstractHandler;  
  11. import org.eclipse.jetty.util.log.Log;  
  12. import org.eclipse.jetty.util.log.Logger;  
  13.   
  14. public class BookHandler extends AbstractHandler   
  15. {  
  16.     private static final Logger LOG = Log.getLogger(BookHandler.class);  
  17.       
  18.     public void handle(String target, Request baseRequest,  
  19.             HttpServletRequest request, HttpServletResponse response)  
  20.             throws IOException, ServletException {  
  21.           
  22.         LOG.info("request income : url=" + target);  
  23.         String msg = "";  
  24.         if("/".equals(target))  
  25.         {  
  26.             msg = "world";  
  27.         }  
  28.         else  
  29.         {  
  30.             msg = target;  
  31.         }  
  32.   
  33.         response.setContentType("text/html;charset=utf-8");  
  34.   
  35.         response.setStatus(HttpServletResponse.SC_OK);  
  36.   
  37.         baseRequest.setHandled(true);  
  38.   
  39.         response.getWriter().println("<h1>Hello "+msg+"</h1>");  
  40.     }  
  41. }  


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

 

server.port=9696

 

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

 

[html]  view plain  copy
 
  1. <assembly xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/assembly-1.0.0.xsd">  
  3.     <id>package</id>  
  4.     <formats>  
  5.         <format>zip</format>  
  6.     </formats>  
  7.     <includeBaseDirectory>true</includeBaseDirectory>  
  8.     <fileSets>  
  9.         <fileSet>  
  10.             <directory>src/main/bin</directory>  
  11.             <outputDirectory>bin</outputDirectory>  
  12.         </fileSet>  
  13.         <fileSet>  
  14.             <directory>src/main/conf</directory>  
  15.             <outputDirectory>conf</outputDirectory>  
  16.         </fileSet>  
  17.         <fileSet>  
  18.             <directory>src/main/logs</directory>  
  19.             <outputDirectory>logs</outputDirectory>  
  20.         </fileSet>  
  21.         <fileSet>  
  22.             <directory>src/main/work</directory>  
  23.             <outputDirectory>work</outputDirectory>  
  24.         </fileSet>  
  25.     </fileSets>  
  26.     <dependencySets>  
  27.         <dependencySet>  
  28.             <outputDirectory>lib</outputDirectory>  
  29.             <scope>runtime</scope>  
  30.         </dependencySet>  
  31.     </dependencySets>  
  32. </assembly>  


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

 

 

[plain]  view plain  copy
 
  1. #!/bin/bash  
  2.     
  3. if [ "$JAVA_HOME" = "" ]; then  
  4.   echo "Error: JAVA_HOME is not set."  
  5.   exit 1  
  6. fi  
  7.   
  8. bin=`dirname "$0"`  
  9.   
  10. export MYJETTY_HOME=`cd $bin/../; pwd`  
  11.   
  12. MYJETTY_CONF_DIR=$MYJETTY_HOME/conf  
  13. CLASSPATH="${MYJETTY_CONF_DIR}"  
  14.   
  15. for f in $MYJETTY_HOME/lib/*.jar; do  
  16.   CLASSPATH=${CLASSPATH}:$f;  
  17. done  
  18.   
  19. LOG_DIR=${MYJETTY_HOME}/logs  
  20.   
  21. CLASS=com.lala.tomcat.App  
  22. 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 即可看到输出

猜你喜欢

转载自fengbin2005.iteye.com/blog/2400443