Maven发布公共jar包到私服仓库

一个简单的springboot微服务项目,所有子模块依赖api-root这个POM


Maven发布公共jar包到私服仓库_第1张图片
模块说明
Maven发布公共jar包到私服仓库_第2张图片
项目结构

公共的jar中因为没有main方法,当我们install整个项目的时候,会抛异常:

Maven发布公共jar包到私服仓库_第3张图片
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.2.RELEASE:repackage (default) on project commons: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:2.0.2.RELEASE:repackage failed: Unable to find main class -> [Help 1]

解决办法:在公共jar包的POM中加入

   
      
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
            
        
    

而服务jar我们通常不需要deploy到私服仓库中去,只需要在服务模块的POM中加入

   
        ${project.artifactId}
        
            
                org.apache.maven.plugins
                maven-deploy-plugin
                
                    true
                
            
        
    

deploy我们需要告诉它发布到哪去,只需要在父POM中加入下面的代码:

    
        
        
            nexus-releases
            Nexus Release Repository
            http://192.168.1.2:8081/repository/maven-releases/
        
        
            nexus-snapshots
            Nexus Snapshot Repository
            http://192.168.1.2:8081/repository/maven-snapshots/
        
    

上面的和maven的settings.xml中配置的id要对应

  
    
        nexus-releases
        admin
        admin123
    
    
        nexus-snapshots
        admin
        admin123
    
  

这样当我们运行mvn deploy -Dmanve.test.skip=true的时候,就可以将api-root.pom,commons.jar,api-feign.jar发布到私服仓库中供别人使用了。


有时候我们自己的公共的snapshots包deploy到私服后,maven并没有去拉取最新的jar包,这是因为maven默认配置每天只拉取一次。

方案一:

可以在setting.xml文件中修改默认行为:

always

updatePolicy可用的值包括:

  • daily 每天检查一次(默认)
  • never 从不检查更新;
  • always 每次构建都检查更新;
  • interval:X 每隔X分钟检查一次更新(X为任意整数)

方案二:

或者打包的时候添加-U参数来强制更新。

mvn clean package -U

方案三:

idea中还可以勾选Always update snapshots

Maven发布公共jar包到私服仓库_第4张图片
image.png


附:

  • maven的setting.xml


  
  
  
  
  
    
        nexus-releases
        admin
        admin123
    
    
        nexus-snapshots
        admin
        admin123
    
  

  
    maven-public
    maven-public
    http://192.168.1.2:8081/repository/maven-public/
    central
  
  
  
     
      maven-public 
       
           
              nexus-releases 
              
              Nexus Release Snapshot Repository 
              
              http://192.168.1.2:8081/repository/maven-releases/
              
                  true
               
              
                  false
               
          
          
              nexus-snapshots
              http://192.168.1.2:8081/repository/maven-snapshots/
              
                false
              
              
                true
              
              always
          
      
       
         
          nexus-releases 
          Nexus Release Snapshot Repository 
          http://192.168.1.2:8081/repository/maven-releases/
          
              true
           
          
              true
          
         
         
          nexus-snapshots
          http://192.168.1.2:8081/repository/maven-snapshots/
          
            true
          
          
            true
          
          always
         
        
     
  
  
  
      maven-public 
  

  • 完整的api-root的pom.xml


    4.0.0

    com.wd.cloud
    api-root
    0.0.1-SNAPSHOT
    pom

    api-root
    微服务根POM

    
        api-feign
        api-gateway
        config-server
        discovery
        auth-server
        doc-delivery
        commons
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Finchley.M9
        1.4.4.RELEASE
        1.1.9
        1.4.7
        1.7.0.RELEASE
        [4.0.12,)
        2.9.5
        2.9.5
        1.9.12
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
                com.alibaba
                druid
                ${com.alibaba.druid.version}
            
            
                org.springframework.cloud
                spring-cloud-starter-feign
                ${openfeign.version}
            
            
                com.spring4all
                swagger-spring-boot-starter
                ${swagger.version}
            
            
                javax.mail
                mail
                ${javax.mail.version}
            

            
                com.wd.cloud
                commons
                ${version}
            
            
                com.wd.cloud
                api-feign
                ${version}
            
            
                cn.hutool
                hutool-all
                ${cn.hutool.all.version}
            
            
                com.fasterxml.jackson.core
                jackson-databind
                ${jackson.databind.version}
            
            
                com.fasterxml.jackson.core
                jackson-core
                ${jackson.core.version}
            
            
                org.codehaus.jackson
                jackson-mapper-asl
                ${jackson.asl.version}
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
        
        
            nexus-releases
            Nexus Release Repository
            http://192.168.1.2:8081/repository/maven-releases/
        
        
            nexus-snapshots
            Nexus Snapshot Repository
            http://192.168.1.2:8081/repository/maven-snapshots/
        
    

    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/libs-milestone
            
                false
            
        
    


  • 公共模块commons的pom.xml


    4.0.0
    ${parent.version}
    commons
    jar

    commons
    公共包

    
        com.wd.cloud
        api-root
        0.0.1-SNAPSHOT
        ../pom.xml
    
    
        UTF-8
        UTF-8
        1.8
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
            
        
    

  • 服务模块config-server的pom.xml


    4.0.0

    config-server
    ${parent.version}
    jar

    config-server
    配置服务中心

    
        com.wd.cloud
        api-root
        0.0.1-SNAPSHOT
        ../pom.xml
    

    
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.wd.cloud
            commons
        
        
            com.spring4all
            swagger-spring-boot-starter
        
    

    
        ${project.artifactId}
        
            
                org.apache.maven.plugins
                maven-deploy-plugin
                
                    true
                
            
        
    


你可能感兴趣的:(Maven发布公共jar包到私服仓库)