验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

怎么使用MAVEN打JAR包

阅读:844 来源:乙速云 作者:代码code

怎么使用MAVEN打JAR包

使用MAVEN打JAR包

一、简单的方法:

首先在pom.xml里面添加:

  
      
        
        maven-assembly-plugin  
          
            
            jar-with-dependencies  
           
            
              
              com.qunar.piao.data.integration.Boot  
              
            
                     
          
        
      
  

然后执行:mvn assembly:assembly 

最后执行:java   -jar target/ticket-data-integration-0.0.1-SNAPSHOT-jar-with-dependencies.jar 

二、执行任意main方法

两个类,Boot类:

package com.qunar.check.integration;

public class Boot {

    public static void main(String[] args){
        System.out.println("test xingxing");
    }
}

Boot2类:

package com.qunar.check.integration;

public class Boot2 {
    public static void main(String[] args){
        System.out.println("test liqiu");
    }
}

那么执行:

$ java -classpath target/check-jar-with-dependencies.jar com.qunar.check.integration.Boot2
test liqiu
$ java -classpath target/check-jar-with-dependencies.jar com.qunar.check.integration.Boot
test xingxing

两个main函数都可以执行

扩展:maven 使用assembly 进行打包

1. pom 中添加assembly 插件

要使用assembly 进项编译打包, 首先主要在pom 中的build中添加插件信息, 具体如图下所示:


    ${project.artifactId}
    src/main/java
    
        
            src/main/resources
            true
            
                **/*.xml
                **/*.properties
            
        
        
            ${profile.dir}
            true
        
    

    
        
        
            org.apache.maven.plugins
            maven-compiler-plugin
            3.1
            
                1.8
                1.8
                utf-8
            
        

        
        
            org.apache.maven.plugins
            maven-assembly-plugin
            
                
                src/main/assembly/assembly.xml
            
            
                
                    make-assembly
                    package
                    
                        single
                    
                
            
        
    

2. 创建assembly文件夹和assembly.xml文件

创建assembly文件夹和assembly.xml文件, 这个样子创建主要是规范。
在pom 中已经介绍assembly.xml 位置。


src/main/assembly/assembly.xml

创建assembly.xml 文件后添加如下内容:


    
        
        tar.gz
        zip
        dir
    
    false
    
        
            src/main/resources
            conf
            0644
        
        
            ${profile.dir}
            conf
            
            
                *.xml
                *.properties
                **/*.xml
                **/*.properties
            
            0644
        
        
            src/main/assembly/bin
            bin
            0755
        
    
    
        
            lib
        
    

fileMode 官方解释:

Similar to a UNIX permission, sets the file mode of the files included. THIS IS AN OCTAL VALUE. Format: (User)(Group)(Other) where each component is a sum of Read = 4, Write = 2, and Execute = 1. For example, the value 0644 translates to User read-write, Group and Other

上述的三个fileSet 分别是将resource 下的资源打包到config 目录下, 将assembly下的bin 启动相关脚本打包到bin 目录下, 将maven项目依赖的所有jar 包, 打包到lib 中。
具体结构如下图所示:

怎么使用MAVEN打JAR包

分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>