使用github packages发布依赖

之前github没有免费teams服务的时候,并没有发现这个功能,但实际上只要有github账号,任何一个仓库都具备这个功能,汗颜啊,之前没认真看过github提供的每个按钮。

还是进入正题了,由于基本上都是在写后端代码,所以这里先记上maven项目和gradle项目的github packages使用方法(其实官方也有说明)

示例项目在https://github.com/beeant0512/packagesdemo

  • gradle项目
    官方的文档https://help.github.com/en/packages/using-github-packages-with-your-projects-ecosystem/configuring-gradle-for-use-with-github-packages
plugins {
	
    id 'java'
    // 引入这个依赖,把gradle发布到maven仓库/nexus仓库
    id 'maven-publish'
}


group 'com.xstudio.demo'
version '1.0.0-SNAPSHOT'

apply plugin: 'idea'
apply plugin: 'jacoco'


sourceCompatibility = 1.8

repositories {
    maven {
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
    maven {
        url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
    }
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile "org.projectlombok:lombok:1.18.10"
    annotationProcessor "org.projectlombok:lombok:1.18.10"

    testCompile group: 'junit', name: 'junit', version: '4.12'
}


[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

// 打包源码
task sourceJar(type: Jar, dependsOn: classes) {
    classifier 'sources'
    from sourceSets.main.allSource
}
// 打包javadoc
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives javadocJar
    archives sourceJar
}
// 这边就是maven-publish的配置了,需要的自行百度查看maven-publish的配置说明
publishing {
    repositories {
        maven {
            // 官方这样说的,我也还不清楚为啥要配置成GitHubPackages
            name = "GitHubPackages"
            url = uri("github仓库地址")
            credentials {
                username = "github账号"
                password = "githubtoken"
            }
        }
    }
    publications {
        mavenJava(MavenPublication) {
            artifactId = '包ID'
            from components.java
            artifact sourceJar
            artifact javadocJar
            pom {
                name = "包ID"
                description = "gradle github packages demo"
                url = "github仓库地址"
                licenses {
                    license {
                        name = "The Apache License, Version 2.0"
                        url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
                    }
                }
            }
        }
    }
}

javadoc {
    if(JavaVersion.current().isJava9Compatible()) {
        options.addBooleanOption('html5', true)
    }
    options {
        encoding "UTF-8"
        charSet 'UTF-8'
        author true
        version true
    }
}
  • maven项目
    官方的帮助文档https://help.github.com/en/packages/using-github-packages-with-your-projects-ecosystem/configuring-apache-maven-for-use-with-github-packages
... 省略n多其他配置

 
        
            github
            ithub账号
            github仓库地址
            
                true
            
            
                false
            
        
 


        
            github
            github账号
            github仓库地址
        
 

... 省略 build的配置

这些账号信息就配置在了maven的settings.xml文件里面



  
    github
  

  
    
      github
      
        
          central
          https://repo1.maven.org/maven2
          true
          true
        
        
          github
          GitHub OWNER Apache Maven Packages
          https://maven.pkg.github.com/OWNER/REPOSITORY
        
      
    
  

  
    
      github
      USERNAME
      TOKEN
    
  


执行mvn deploy发布包就可以了

你可能感兴趣的:(Java)