Java 使用 gradle 将源上传到 nexus 存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28630470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload sources to nexus repository with gradle
提问by gllambi
I successfully uploaded my jars to a nexus repository using the maven plugin for gradle but it didn't upload the sources. This is my configuration:
我使用 gradle 的 maven 插件成功地将我的 jars 上传到了 nexus 存储库,但它没有上传源代码。这是我的配置:
uploadArchives {
repositories{
mavenDeployer {
repository(url: "http://...") {
authentication(userName: "user", password: "myPassword")
}
}
}
}
I searched and found that I can add the sources by adding a new task.
我搜索并发现可以通过添加新任务来添加源。
task sourcesJar(type: Jar, dependsOn:classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
This works fine but I think there must be a better solution by configuring the maven plugin, something like uploadSource = true like this:
这工作正常,但我认为通过配置 Maven 插件必须有更好的解决方案,例如 uploadSource = true 像这样:
uploadArchives {
repositories{
mavenDeployer {
repository(url: "http://...") {
authentication(userName: "user", password: "myPassword")
}
uploadSources = true
}
}
}
回答by Eddú Meléndez
You can use gradle-nexus-plugin
您可以使用gradle-nexus-plugin
In order to use the plugin, add the lines below and import plugin
为了使用插件,添加下面的行并导入插件
buildscript {
repositories {
mavenLocal()
jcenter {
url "http://jcenter.bintray.com/"
}
}
dependencies {
classpath 'com.bmuschko:gradle-nexus-plugin:2.3'
}
}
apply plugin: 'com.bmuschko.nexus'
Add this section, where you will configure url to deploy
添加此部分,您将在其中配置要部署的 url
nexus {
sign = false
repositoryUrl = 'http://localhost:8081/nexus/content/repositories/releases/'
snapshotRepositoryUrl = 'http://localhost:8081/nexus/content/repositories/internal-snapshots/'
}
Note:You must have in ~/.gradle/gradle.properties
注意:你必须在 ~/.gradle/gradle.properties
nexusUsername = deployment
nexusPassword = deployment123
回答by Konstantyn
Nexus for save artifact, not source code.
Nexus 用于保存工件,而不是源代码。
for upload compiled artifact I do:
对于上传编译的工件,我这样做:
apply plugin: 'java'
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "http://nexus-server:8081/nexus/content/repositories/snapshots") {
authentication(userName: "admin", password: "secret")
}
pom.version = "1.0.0-SNAPSHOT"
pom.artifactId = "${project.name}"
pom.groupId = "path.to.artifact"
}
}
}
and call upload from console
并从控制台调用上传
$ gradle upload
for source code use maven or git repository
对于源代码使用 maven 或 git 存储库
回答by Sebi
There is no better solution than what you described yourself. The gradle maven plugin is uploading all artifacts generated in the current project. That's why you have to explicitly create a "sources" artifact.
没有比您自己描述的更好的解决方案。gradle maven 插件正在上传当前项目中生成的所有工件。这就是为什么您必须明确创建“源”工件的原因。
The situation also doesn't change when using the new maven-publishplugin. Here, you also need to explicitly define additional artifacts:
使用新的maven-publish插件时,情况也不会改变。在这里,您还需要明确定义其他工件:
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}
The reason is that gradle is more about being a general build tool and not bound to pure Java projects.
原因是 gradle 更像是一个通用的构建工具,而不是绑定到纯 Java 项目。