我有一个用作 API 的 gradle 项目。该 API 发布在 github 包上,但没有 javadoc,即使注释很好地存在于 github 的存储库中。
主要
build.gradle
是这样的:
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
plugins {
id 'java-library'
id "maven-publish"
id 'io.github.goooler.shadow' version '8.1.7'
}
java.toolchain.languageVersion.set(JavaLanguageVersion.of(21))
publishing {
repositories {
maven {
name = "github"
url = uri("https://maven.pkg.github.com/MyProject/MyProject")
credentials(HttpHeaderCredentials);
authentication {
header(HttpHeaderAuthentication)
}
}
}
publications {
gpr(MavenPublication) {
artifact("build/libs/MyProject-1.0-SNAPSHOT.jar") {
builtBy shadowJar
}
groupId 'com.example'
artifactId 'myproject'
}
}
}
archivesBaseName = 'MyProject'
sourceSets.main {
java.srcDirs = ['src']
resources.srcDirs = ['resources']
}
compileJava.options.encoding = 'UTF-8'
shadowJar {
archiveBaseName.set('MyProject')
archiveClassifier.set(null)
}
build.dependsOn shadowJar
使用
./gradlew shadowJar publish
时效果很好,而且我在 github 上看到了可用的包。我可以访问它,但只有 jar 文件,没有源代码,也没有 javadoc。所以我的 IDE 在使用方法时不会显示注释,即使有。
需要明确的是,使用此代码时:
public class Test {
/**
* Run all tests.
*/
public static void runTests() {
}
}
我在使用代码
Run all tests
时没有看到Test.runTests()
注释。 (即使我对其他一些依赖项这样做,所以问题不是来自我的 IDE)
我尝试添加这个:
java {
withSourcesJar()
withJavadocJar()
}
但是我得到了错误:
* What went wrong:
Execution failed for task ':publishGprPublicationToGithubRepository'.
> Failed to publish publication 'gpr' to repository 'github'
> Could not PUT 'https://maven.pkg.github.com/MyProject/MyProject/com/example/myproject/1.0-SNAPSHOT/myproject-1.0-20240906.132919-68.jar.sha1'. Received status code 409 from server: Conflict
name: Build with gradle
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21'
cache: gradle
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build
run: ./gradlew shadowJar publish -PgithubAuthHeaderName='Authorization' -PgithubAuthHeaderValue='Bearer ${{ secrets.TOKEN }}'
- name: Upload Artifact to GitHub Action
uses: actions/upload-artifact@v4
with:
name: MyProject
path: build/libs/MyProject-*.jar
我做了一个MCVE:
com.elikill58.test.GradleIssue
类,以及带有注释的方法 checkIfIssue
。注意:两个 github 操作都使用
TOKEN
作为秘密,这是一个基本的身份验证令牌。否则,您可以根据您的身份验证方式进行更改。
在build.gradle中添加生成Javadocs的任务:
task generateJavadoc(type: Javadoc) {
source = sourceSets.main.allJava
classpath = configurations.compileClasspath
destinationDir = file("$buildDir/docs/javadoc")}
修改发布区块:
publishing {
publications {
gpr(MavenPublication) {
artifact("build/libs/MyProject-1.0-SNAPSHOT.jar") {
builtBy shadowJar
}
artifact tasks.generateJavadoc {
classifier "javadoc"
}
groupId 'com.example'
artifactId 'myproject'
}
}}
还要确保文档正在生成:
publish.dependsOn generateJavadoc
添加所有这 3 个部分后,您应该能够发布文档。