Upload sources to nexus repository with gradle - java

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:
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:
uploadArchives {
repositories{
mavenDeployer {
repository(url: "http://...") {
authentication(userName: "user", password: "myPassword")
}
uploadSources = true
}
}
}

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.
The situation also doesn't change when using the new maven-publish plugin. Here, you also need to explicitly define additional artifacts:
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.

You can use 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
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
nexusUsername = deployment
nexusPassword = deployment123

Nexus for save artifact, not source code.
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

Related

How to publish gradle subproject to Maven local repository?

My build.gradle looks like this
configurations {
bukkitPlugin
...
}
dependencies {
bukkitPlugin(project(':IslandCraft-Bukkit'))
bukkitPlugin(project(':IslandCraft-NMS-v1_12_R1'))
bukkitPlugin(project(':IslandCraft-NMS-v1_13_R1'))
}
task bukkit(type: ShadowJar) {
description = 'Create the IslandCraft Bukkit plugin JAR'
group = project.group
baseName = project.name
appendix = 'Bukkit'
version = project.version
configurations = [ project.configurations.bukkitPlugin ]
from {
project.configurations.bukkitPlugin
}
relocate 'org.mcstats', 'com.github.hoqhuuep.islandcraft.mcstats'
}
publishing {
publications {
bukkitPlugin(MavenPublication) { publication ->
shadow.component(publication)
}
}
}
gradle bukkit works fine and builds all that I need but gradle publishToMavenLocal saves jar with META-INF only on my local repository. How can I install the build to local repository?

Intellij and Eclipse unable to find library artifact

I've a library project in Android studio that will be used in both Android and Java projects. I'm publishing this library to my own remote artifact repository in both .aar and .jar formats using the maven-publish plugin that's shipped with Android Studio. I can fetch and use the library properly in an Android app however when I try to use it in a Java project, intellij and eclipse are unable to detect even if the library is there even though gradle/maven is fetching the library from the remote repo. I've tried pushing only aar to the remote repo and fetching that in Java projects but that didn't workout either. Please note that I've no Android dependency/resources in this library. Following is the script being used to generate and push the artifacts to remote server.
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
android.libraryVariants.all { variant ->
variant.outputs.all {
outputFileName = "${_groupId}-${_artifactId}-${android.defaultConfig.versionName}.aar"
}
}
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "sources"
}
task deleteOldJar(type:Delete){
delete 'release/MyLibrary.jar'
}
task exportJar(type:Copy){
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
rename('classes.jar','MyLibrary.jar')
}
publishing {
repositories {
maven {
url _remoteRepoUrl
}
}
publications {
library(MavenPublication) {
groupId _groupId
artifactId _artifactId
version android.defaultConfig.versionName
artifact(sourceJar)
artifact(exportJar)
artifact ("$buildDir/outputs/aar/${_groupId}-${_artifactId}-${android.defaultConfig.versionName}.aar") { //aar artifact you want to publish
builtBy assembleDebug
}
//generate pom nodes for dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.compile.allDependencies.each { dependency ->
println "dependency name is: "+dependency.name
if(!dependency.name.equals('unspecified')) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dependency.group)
dependencyNode.appendNode('artifactId', dependency.name)
dependencyNode.appendNode('version', dependency.version)
dependencyNode.appendNode('type', "aar")
}
else
println "dependency name is unspecified"
}
}
}
}
}
What am I doing wrong?
I found the solution. Turns out the com.android.library plugin in gradle was somehow messing with the generated artifact such that only Android projects could use the library artifact and not the plain java projects. Since my library doesn't have any layout resources, I can skip making .aar and just generate a jar file. For generating the jar file and publishing it to the remote repo following snippet is being utilized:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven-publish'
sourceCompatibility = 1.8
def _remoteRepoUrl = "http://myserver.com/repo"
def _groupId = group
def _versionName = version
repositories {
mavenCentral()
jcenter()
maven {
url _remoteRepoUrl
}
flatDir {
dirs 'libs'
}
}
jar {
from sourceSets.main.output
}
task jarWithSources(type: Jar) {
from sourceSets.main.output
if (gradle.startParameter.taskNames.any{it == "publishToMavenLocal"}) {
from sourceSets.main.allJava
}
}
// Define how maven-publish publishes to the nexus repository
publishing {
repositories {
maven {
url _remoteRepoUrl
}
}
publications {
mavenJava(MavenPublication) {
from components.java
}
// publish the <project> jar as a standalone artifact
mavenJar(MavenPublication) {
artifact jarWithSources
//from components.java -- can't have both the above line and this
//artifactId "${jar.baseName}_jar"
version project.version
}
}
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
}
}

How to uploadArchives with multiple repositories using parameters in Gradle?

In my gradle project, I want to upload my jar to multiple maven repository, so I do the following:
uploadArchives {
repositories {
mavenLocal()
mavenDeployer {
repository(url: maven_repo_1) {
authentication(userName: maven_user_1, password: maven_password_1)
}
repository(url: maven_repo_2) {
authentication(userName: maven_user_2, password: maven_password_2)
}
}
}
}
My question is:
Is this the correct way to upload to multiple maven repository at once?
Is there a way to use a parameters to upload to specific maven repository? I.e uploadArchives -r mavenLocal
Note:
Previously I had to remove the maven repository and run uploadArchives, I don't think this is the best practice. However I didn't find documentation for uploading it with parameters.
What you could do, is using a project property to pick the right configuration.
For example, if you want to deploy to a remote repository by default, but with a parameter that switches to a local repo:
uploadArchives {
repositories {
if (project.hasProperty('localRepoPath')) {
mavenDeployer {
repository(url: uri(project.getProperty('localRepoPath')))
...
}
} else {
mavenDeployer {
repository(url: maven_repo_1)
...
}
}
}
}
Then, to invoke:
# Local upload
./gradlew uploadArchives -PlocalRepoPath=/tmp/maven
# Public upload
./gradlew uploadArchives
You can adapt this approach to your own needs.

Publishing locally with Gradle not working

I have the following directory structure:
myapp/
src/main/resources/
<lots of code>
build.gradle
With the following build.gradle:
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
group = 'net.myuser'
repositories {
jcenter()
}
dependencies {
compile(
<dependencies here>
)
}
jar {
baseName = 'myapp'
}
task writePom << {
pom {
project {
groupId group
artifactId 'myapp'
version version
inceptionYear '2015'
licenses {
license {
name 'myapp'
distribution 'Blah blah blah'
}
}
}
}.writeTo("build/libs/pom.xml")
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
artifacts {
archives(file("${buildDir}/libs/myapp-${version}.jar")) {
name "myapp"
classifier ""
}
}
When I do:
./gradlew clean build writePom install -Pversion=0.1.0
I get two problems:
Inside the generated build/libs/pom.xml the groupId is showing as null; and
I cannot find a net.myuser directory under ~/.gradle/caches/modules-2, which tells me install is not working
So I ask: What do I need to change so that groupId isn't null, and how do I get install publishing all of the following:
JAR
Sources JAR
Javadoc JAR
pom.xml
As per comment, use groupId project.group instead of groupId group in order to set the <groupId> properly.
Regarding the install task, please have a look at Gradle Maven Plugin documentation:
Installs the associated artifacts to the local Maven cache, including Maven metadata generation.
By default, the local Maven cache is located in ~/.m2/repository, thus you are looking at the wrong location. The install task does not tamper with ~/.gradle/caches/modules-2 which is (as the name already implies) only a cache for resolved dependencies.
By using the Maven plugin, Gradle already creates a POM file for you. So please check if you really need a custom writePom task.
Also, the main artifact is installed automatically, so this might be redundant:
artifacts {
archives(file("${buildDir}/libs/myapp-${version}.jar")) {
name "myapp"
classifier ""
}
}

Ubuntu: Gradle maven-publish plugin - Failing to connect by sftp

I'm trying to get my gradle script to upload to an sftp account, but it keeps failing with error,
Failed to deploy artifacts: Could not transfer artifact com.himself12794:Heroes-Mod:jar:0.9-rev1 from/to remote (sftp://himself1#ftp.himself12794-develops.com:18765/public_html/maven): Could not write to resource 'com/himself12794/Heroes-Mod/0.9-rev1/Heroes-Mod-0.9-rev1.jar'
I'm able to connect via sftp command:
sftp -P 18765 himself1#ftp.himself12794-develops.com
However, the publishMavenJavaPublicationToMavenRepository task fails, with the above error.
This is a Minecraft Forge mod, if that sheds any light on things.
My build.gradle is like so:
buildscript {
repositories {
jcenter()
maven {
name = "forge"
url = "http://files.minecraftforge.net/maven"
}
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.0- SNAPSHOT'
}
}
apply plugin: 'net.minecraftforge.gradle.forge'
apply plugin: "maven-publish"
// for people who want stable
/*plugins {
id "net.minecraftforge.gradle.forge" version "2.0.1"
id "maven-publish"
}*/
repositories {
maven {
name = "himself12794-develops"
url = "http://maven.himself12794-develops.com"
}
}
version = "0.9-rev1"
group = "com.himself12794"
archivesBaseName = "Heroes-Mod"
minecraft {
version = "1.8-11.14.3.1514"
runDir = "run"
mappings = "snapshot_20141130"
// makeObfSourceJar = false
}
dependencies {
compile "com.himself12794:powersAPI:1.1-rev2"
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
publishing {
repositories {
maven {
url "sftp://himself1#ftp.himself12794-develops.com:18765"
}
}
}
processResources
{
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
from(sourceSets.main.resources.srcDirs) {
include 'mcmod.info'
expand 'version':project.version, 'mcversion':project.minecraft.version
}
from(sourceSets.main.resources.srcDirs) {
exclude 'mcmod.info'
}
}
Any assistance anyone can give is greatly appreciated.
I have not solved this specific problem yet, but I have discovered that the problem with the original maven plugin that caused me to try the maven-publish plugin was actually an issue with the current stable version of Gradle (2.7). Using the current nightly wrapper build has allowed me to circumvent this.

Categories