How To: Eclipse Maven install build jar with dependencies

前端 未结 1 1065
有刺的猬
有刺的猬 2020-12-07 16:50

I am using Eclipse Maven (m2e) inside Eclipse and I am running my project like this:

My pom.xml looks like this:



        
1条回答
  •  自闭症患者
    2020-12-07 17:35

    There are a couple of ways of doing this.

    1) If you want an uber-jar (repacked with all dependencies), look into using and configuring the maven-shade-plugin:

      
        
          
            org.apache.maven.plugins
            maven-shade-plugin
            1.6
            
              
                package
                
                  shade
                
                
                  
                    
                      com.group.id.Launcher1
                    
                  
                
              
            
          
        
      
    

    This will unpack all dependencies and merge them into one JAR file.


    2) If you want to deliver a bundle (zip, tar.gz, etc) with the unpacked JAR files in the bundle (perhaps under lib/) then you need to look into the maven-assembly-plugin:

      
        
          
            org.apache.maven.plugins
            maven-assembly-plugin
            2.3
            
              
                create-distro
                package
                
                  single
                
                
                  
                    src/main/assembly/dist.xml
                  
                
              
            
          
        
      
    

    Note that this requires an assembly descriptor src/main/assembly/dist.xml and example looks like this:

    
      distribution
      
        zip
      
    
      
        
          false
          false
          false
          runtime
          0755
          0755
          bin
    
          
            com.group.id:project-launch1
            com.group.id:project-launch2
          
    
        
        
          false
          true
          false
          runtime
          0644
          0755
          lib
    
          
            com.group.id:project-lib1
            com.group.id:project-lib2
            com.group.id:project-lib3
            com.group.id:project-lib4
          
    
        
      
    
    

    And since you are now assembling dependencies, you have better define the dependency in the pom.xml, like so:

      
        
          com.group.id
          project-launch1
          0.0.1-SNAPSHOT
          jar
        
        
          com.group.id
          project-launch2
          0.0.1-SNAPSHOT
          jar
        
        
          com.group.id
          project-lib1
          0.0.1-SNAPSHOT
          jar
        
        ... and so on ...
      
    


    3) If you are delivering a bundle with an executable JAR file launcher, you will likely need to add a maven-jar-plugin configuration in addition to the maven-assembly-plugin:

      
        
          com.group.id
          project-lib1
          0.0.1-SNAPSHOT
          jar
        
        
          com.group.id
          project-lib2
          0.0.1-SNAPSHOT
          jar
        
        
          com.group.id
          project-lib3
          0.0.1-SNAPSHOT
          jar
        
        ... and so on ...
      
    
      
        
          
            maven-jar-plugin
            
              
                false
                true
                
                  com.group.id.Launcher1
                  true
                  ../lib/
                
              
            
          
        
      
    

    Note that the "addClasspath" directive adds the project dependencies to the class path. This is needed for JAR launchers, as they explicitly ignore all CLASSPATH environmental variables.

    0 讨论(0)
提交回复
热议问题