用maven打包java项目并发布jar包到maven仓库nexus上比较常见,这里就介绍用gradle来发布。
首先在build.gradle的plugins上加“ id ‘maven-publish’”就可以在右边Gradle菜单栏该项目下面的Tasks看到“publishing”了,如果没有就点build.gradle文件内右上角的小象图标“Load Gradle Changes”,接着配置publishing。
plugins {
id 'java'
id 'maven-publish'//用于发布jar包到maven仓库nexus
}
publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
repositories {
maven {
// allowInsecureProtocol = true //Failed to publish publication ‘maven‘ to repository ‘maven3‘ > Using insecure protocols with repo
url "nexus仓库地址"
credentials {
username 'nexus账号'
password 'nexus密码'
}
}
}
}
然后右边Gradle菜单栏该项目下面的“publish”或“publishAllPublicationsToMavenRepository”即可将项目打包成jar包并发布到nexus上。
细心的话你会发现没有打包源码jar包,这个时候只需要在build.gradle配置文件中加上withSourcesJar()然后重新点“publishAllPublicationsToMavenRepository”发布即可。
java {
// withJavadocJar()
withSourcesJar()
}
你还可以在“maven(MavenPublication)”中加上“pom”来添加项目名称、描述、url、许可(licenses)、开发者信息(developers)、scm等等,更多配置参考Gradle官网的Publishing Libraries文档。
plugins {
id 'org.springframework.boot' version '2.7.10'//'2.3.5.RELEASE' 2.6.6
id 'io.spring.dependency-management' version '1.0.15.RELEASE'//'1.0.10.RELEASE' 1.0.11.RELEASE
id 'java'
id 'maven-publish'//用于发布jar包到maven仓库nexus
}
group = 'cn.xubingtao.dev'
version = '0.0.1-SNAPSHOT'//ALPHA
sourceCompatibility = '1.8'
//task sourceJar(type: Jar) {
// from sourceSets.main.allSource,sourceSets.main.allJava
// archiveClassifier = "sources"
//}
java {
// withJavadocJar()
withSourcesJar()
}
publishing {
publications {
maven(MavenPublication) {
// groupId = 'cn.xubingtao.dev'
// artifactId = 'dev'
// version = '0.0.1-SNAPSHOT'
// artifact sourceJar
//// artifact "dev-0.0.1-SNAPSHOT.jar"
from components.java
pom {
name = "dev"
description = "This is the project of dev."
url = "https://www.xubingtao.cn/"
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
developers {
developer {
id = "xbt"
name = "xubingtao"
email = "kefu@xubingtao.cn"
}
}
// scm {
// connection = "scm:svn:https://svn.xubingtao.cn/svn/project/trunk/"
// developerConnection = "scm:svn:https://svn.xubingtao.cn/svn/project/trunk/"
// url = "https://svn.xubingtao.cn/svn/project/trunk/"
// }
}
}
}
repositories {
maven {
// allowInsecureProtocol = true //Failed to publish publication ‘maven‘ to repository ‘maven3‘ > Using insecure protocols with repo
url "nexus仓库地址"
credentials {
username 'nexus账号'
password 'nexus密码'
}
}
}
}
}
...
其中,“withJavadocJar()”可以打包文档但是要先处理好各种规范不然会报错;maven(MavenPublication)下的groupId、artifactId、version直接跟nexus上面的maven-snapshots等components的目录或路径对应,不配就用项目默认的;“sourceJar”的作用等同于“withSourcesJar()”。
另外用“sourceJar”以及build.gradle中加上“sourceSets.main.resources.srcDirs = [“src/main/java”,”src/main/resources”]”会导致报下面的策略问题duplicatesStrategy,所以还是直接使用“withSourcesJar()”比较好。
Execution failed for task ':sourceJar'.
> Entry xxx/xxx.java is a duplicate but no duplicate handling strategy has been set. Please refer to https://docs.gradle.org/8.1.1/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy for details.
历史上的今天:
- 2020: 小程序开发[代码加入主动更新](0)
展开阅读全文