jitpack打包

1.添加依赖

在gradle.build文件中,应用插件:maven或者maven-publish

1
2
3
apply plugin: `maven`
或者
apply plugin: `maven-publish`

如果是新版gradle,应用插件可以使用plugins{}取代apply plugin写法。例如:

1
2
3
4
plugins{
id 'java'
id 'maven-publish'
}

如果应用的maven插件,可以使用下面命令在本地测试打包情况

1
./gradlew install

如果使用的maven-publish插件,可以使用下面命令在本地测试打包情况

1
./gradlew build publishToMavenLocal --stacktrace //本地打包发布,--stacktrace参数查看更详细内容

2.更改build.gradle

示例代码:来自jitpack官方

Simple - https://github.com/jitpack/gradle-simple

Example Gradle project producing a single jar. Uses the maven plugin to publish the jar to the local repository.

https://jitpack.io/#jitpack/gradle-simple

To install the library add:

1
2
3
4
5
6
7
repositories { 
jcenter()
maven { url "https://jitpack.io" }
}
dependencies {
implementation 'com.github.jitpack:gradle-simple:1.1'
}

build.gradle文件示例内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
apply plugin: 'java'
apply plugin: 'maven-publish'

group = 'com.github.jitpack'
version = '2.0'

sourceCompatibility = 1.8 // java 8
targetCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
implementation 'com.google.guava:guava:29.0-jre'
}

java {
withSourcesJar()
withJavadocJar()
}

publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
}

wrapper {
gradleVersion = "7.3.3"
distributionType = Wrapper.DistributionType.ALL
}

Multiple modules - https://github.com/jitpack/gradle-modular

Example gradle project with two modules:

  • client
  • server

To install each module individually use com.github.User.Repo for groupId and module name as artifact id:

1
2
compile 'com.github.jitpack.gradle-modular:client:1.1'
compile 'com.github.jitpack.gradle-modular:server:1.1'

build.gradle文件示例内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
allprojects {
version = "1.4"
group = 'io.jitpack.gradle-modular'
}

subprojects {
apply plugin: 'java'
apply plugin: 'maven-publish'

sourceCompatibility = 1.8 // java 8
targetCompatibility = 1.8

java {
withSourcesJar()
withJavadocJar()
}

publishing {
publications {
maven(MavenPublication) {
groupId project.group
artifactId project.name
version project.version
from components.java
}
}
}
}

Project with multiple artifacts - https://github.com/jitpack/gradle-multiple-jars

Example Gradle project producing two jars from single source directory:

  • API from src/api
  • Impl from src/impl

Both jars are installed to local maven reactor with:

1
gradle install

To add them to build.gradle use the following syntax:

1
2
compile 'com.github.jitpack.gradle-multiple-jars:api:1.0.1'
compile 'com.github.jitpack.gradle-multiple-jars:impl:1.0.1'

Or to add them both together:

1
compile 'com.github.jitpack:gradle-multiple-jars:1.0.1'

build.gradle文件示例内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
apply plugin: "java"
apply plugin: "maven"

group = "com.github.jitpack"
version = 1.0
sourceCompatibility = 1.8 // java 8
targetCompatibility = 1.8

repositories {
mavenCentral()
}

sourceSets {
api
impl
}

sourceSets.all { set ->
def jarTask = task("${set.name}Jar", type: Jar) {
baseName = "$set.name"
from set.output
}

artifacts {
archives jarTask
}
}

dependencies {
apiCompile 'commons-codec:commons-codec:1.5'

implCompile sourceSets.api.output
implCompile 'commons-lang:commons-lang:2.6'

testCompile 'junit:junit:4.9'
testCompile sourceSets.api.output
testCompile sourceSets.impl.output

runtime configurations.apiRuntime
runtime configurations.implRuntime
}

jar {
from sourceSets.api.output
from sourceSets.impl.output
}

// this step is necessary when installing both jars in the local maven repository
install {
repositories.mavenInstaller {
addFilter('api') { artifact, file -> artifact.name.endsWith('api') }
addFilter('impl') { artifact, file -> artifact.name.endsWith('impl') }
}
}

3.打包和发布

3.1公开库

3.2私有库

3.2.1 总览-以私有库为例

  1. 使用github账号登录Jitpack,点击settings

  2. 输入自己的私有库地址,点击Generate生成SSH keys

  3. 把jitpack.io生成的ssh-key粘贴到自己的私有工程中,配置访问权限。

    1
    2
    3
    私有库的设置页面,在"deploy keys"下面点击 “add deploy keys
    点击 “add deploy keys”之后,添加key,配置访问权限。
    不用勾选页面下方的“Allow write access”
  4. 在github上生成relase,之后在jitpack.io中,查找自己的库,点击Look up出现我们的release版本

3.2.2 setup

要开始使用私有存储库,您首先需要授予 JitPack 访问权限。打开https://jitpack.io/private并按照以下步骤操作:

  1. 单击授权以获取您的个人身份验证令牌。您团队的每个成员都应该获得自己的令牌。

  2. 将令牌添加到 $HOME/.gradle/gradle.properties:

    authToken=AUTHENTICATION_TOKEN

  3. 然后在 build.gradle 中使用 authToken 作为用户名:

1
2
3
4
5
6
repositories {
maven {
url "https://jitpack.io"
credentials { username authToken }
}
}

做完这一步,自己就可以不用公开存储库,而是可以直接使用了。

如果要让其他人可以使用,参考下面的两种分享方法

问题:按照官网的写法但是使用的时候jitpack -从服务器收到状态代码521/401/403

因为您在 gradle.properties 中定义了 jitpack 令牌,但您没有将其指向那里,只需复制并粘贴下面在 gradle 属性中使用 jitpack 令牌的代码和应该能正常工作

1
2
3
4
5
6
7
8
9
10
11
12
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven {
url "https://jitpack.io"
credentials { username = project.properties['jitpackToken'] }
}
maven { url 'https://maven.microblink.com' }
}
}

3.2.3 公开分享

在某些情况下,您希望公开发布您的项目,同时保持源代码的私密性。在这种情况下:

  1. 使用具有推送权限的用户登录
  2. 查找您的私人存储库
  3. 点击设置图标
  4. 单击 Lock 图标以公开此库

3.2.4 私有分享

任何对您的 Git 存储库具有读取权限的人都可以下载构建工件。您还可以在 JitPack.io 上添加对构建工件具有读取权限的其他协作者。他们将无权访问源代码。

添加协作者:

  1. 使用具有推送权限的用户登录
  2. 查找您的私人存储库
  3. 点击设置
  4. 添加合作者 GitHub 用户名

合作者需要登录 JitPack.io 以获得他们自己的身份验证令牌。

问题

说没有gradle-wrapper

这个文件是gradle的包装,在不同的环境下不同的gradle版本中确保编译顺利,所以,版本库里不能把他忽略。

如果git的忽略文件中的规则把他忽略了(比如忽略文件中的规则 : *.jar),可以使用git add -f gradle-wrapper.jar 强制添加

Entry META-INF/DownloadEngine.kotlin_module is a duplicate but no duplicate handling strategy has been set.

  • Try adding this to your build.gradle . in packagingOptions:
1
2
3
4
5
packagingOptions {
...
exclude 'META-INF/rxkotlin.properties'
exclude 'META-INF/rxkotlin_main.kotlin_module'
}
  • Looks like a cache error (with kotlin) to me. Running gradle clean or gradle clear (not sure which is the right spelling, sorry, one of these should exist as a gradle task) and rebuilding should fix the error, because all files in cache will be deleted by the task and regenerated by the project build.

    1
    ./gradlew clean

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!