How to publish gradle subproject to Maven local repository? - java

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?

Related

gradle publishing project-version-PLAIN.war file instead of project-version.war

When I run "gradle build", it generates a build/libs/project-version.war file which is able to run by itself with "java -jar" command. But when I run "gradle artifactoryPublish", it saves on the artifactory repository a "project-version-plain-war" file which does not have tomcat embedded.
I am following this instruccions https://www.jfrog.com/confluence/display/JFROG/Gradle+Artifactory+Plugin
The lines added to the gradle.build are something like this:
plugin "maven-publish"
plugin "com.jfrog.artifactory"
artifactory {
...
}
subprojects {
plugin "war"
group = group
version = version
publishing {
publications {
MavenPublication(MavenPublication) {
from components.web
}
}
repositories{
maven { url "https://artifactory-server" }
}
}
}
help is appreciated
Using from components.web means that you are publishing the artifact produced by the war task. You need to publish the artifact produced by the bootWar task. You can do so by changing the publication to use artifact bootWar instead:
publishing {
publications {
MavenPublication(MavenPublication) {
artifact bootWar
}
}
repositories{
maven { url "https://artifactory-server" }
}
}
This is described in the reference documentation for Spring Boot's Gradle plugin.

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'
}
}

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.

Gradle: Adding sources.jar file within /lib folder of published dist.zip along with all my other dependencies

I have a java project that I am building using gradle. I am releasing a dist.zip folder for my project and I want to add the sources.jar for my project to the /lib subfolder within dist.zip. I am able to create a sources.jar file along with my dist.zip file but I am not sure how to add the sources.jar within the dist.zip. How can I do this? I've tried to copy the .jar into the /lib folder after creating it but I am unable to get it to work. Below is my publishing and sourcesJar task within my gradle.build script.
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
publishing {
repositories {
maven {
url 'localfolder'
}
}
publications {
maven(MavenPublication) {
groupId 'ca.project'
artifactId applicationName
version version
from components.java
artifact sourcesJar {
classifier "sources"
}
artifact distZip {
classifier "dist"
}
}
}
}
You need to tell the application plugin to include the output of your sourcesJar task in the distribution:
distributions {
main {
contents {
from sourcesJar
}
}
}
That will place it in the root, if you want it somewhere else you can do this:
distributions {
main {
contents {
from(sourcesJar) {
into "lib"
}
}
}
}

Upload sources to nexus repository with gradle

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

Categories