How do I publish file created by zip task in gradle - gradle

I'd like to publish the output of my custom zip task to a maven repo. My problem is that when I set the artifact of the publication as the zip task, the file that gets zipped is not what gets published. Instead what gets published is the value given to the "from" argument of my custom zip task. How would I make it so that "jar.zip" is the file that is published?
tasks.register('zipJars', Zip) {
archiveFileName = "jar.zip"
destinationDirectory = layout.buildDirectory.dir("${projectDir.parentFile}/DesktopAndroid/jars/zipped")
from fatJarDev
}
publishing {
publications {
apkBuilding(MavenPublication){
artifact zipJars
}
}
repositories {
maven {
name = 'Local'
url = "file://${rootDir}/Repository"
}
}
}```

Ah - are you referring to the name of the file in the maven repo ? you will need to customise it in publication ;
see https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html
publishing {
publications {
apkBuilding(MavenPublication){
artifact zipJars
artifactId 'zipjars'
}
}
repositories {
maven {
name = 'Local'
url = "file://${rootDir}/Repository"
}
}
}

Related

publish pre-built jars to nexus repo using gradle

I am trying to publish obfuscated jars to nexus repo.
I created a task to obfuscate the code using proguard, then a task that copy the obfuscated jars into build folder.
task proguard (type: proguard.gradle.ProGuardTask) {
println("Performing Obfuscation ..")
configuration 'proguard.conf'
subprojects { porject ->
injars "${projectDir}/build/libs/${porject.name}-${rootProject.version}.jar"
outjars "${projectDir}/build/libs/obfuscated/${porject.name}-${rootProject.version}.jar"
}
libraryjars "/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar"
}
task postProguard (){
doFirst{
println("Deleting Non Obfuscated jars")
subprojects { project ->
delete "${projectDir}/build/libs/${project.name}-${rootProject.version}.jar"
}
println("Copying Obfuscated Jars")
subprojects { project ->
copy {
from "${projectDir}/build/libs/obfuscated/"
into "${projectDir}/build/libs/"
include '*.jar'
}
}
}
}
proguard.finalizedBy postProguard
the issue is when I run ./gradlew publish the project gets re-built and the jars gets changed to non obfuscated again.
I tried to change the publishing task but without results.
publishing {
if(new File("${projectDir}/build/libs/obfuscated").exists()){
publications {
maven(MavenPublication) {
artifact "${projectDir}/build/libs/${project.name}-${rootProject.version}.jar"
pom.withXml {
def dependency = asNode().appendNode('dependencies').appendNode('dependency')
dependency.appendNode("groupId", "${project.name}")
dependency.appendNode("artifactId", "${project.name}")
dependency.appendNode("version", "${rootProject.version}")
}
}
}
}
repositories {
maven {
name = 'maven-snapshots'
url = ***
}
}
}
I added a builtBy attribute to the publication here is a working code
publications {
if(new File("${projectDir}/build/libs/obfuscated").exists()){
maven(MavenPublication) {
artifact ("${projectDir}/build/libs/${project.name}-${rootProject.version}.jar"){
builtBy postProguard
}
}
}
}

gradle publish custom pom

I have written a gradle task which writes a custom pom to "build/libs/pom.xml"
I want to publish above custom pom.xml , so I defined :
def pomXml = artifacts.add('archives', file("$buildDir/libs/pom.xml")) {
builtBy('writePoms') // writePoms is my custom task to create custom pom.xml
}
publishing {
publications {
maven(MavenPublication) {
artifact pomXml
artifactId "myartifact"
groupId 'com.xyz'
version project.version
}
}
repositories {
// Task for manually publishing the maven image. When CI works, setup CI_TOKEN auth.
maven {
url <url>
name "GitLab"
credentials(HttpHeaderCredentials) {
name = System.getenv("CI_JOB_TOKEN") ? "Job-Token" : "Private-Token"
value = System.getenv("CI_JOB_TOKEN") ? System.getenv("CI_JOB_TOKEN") : gitLabPrivateToken
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
When I run ./gradlew publish it publishes pom from "build/publications/maven/pom-default.xml" instead of "build/libs/pom.xml"
Can someone help how can I achieve it?
I had a similar issue (aggregate build of multiple external repositories with their own build systems and then publishing the results of those builds to a local artifactory).
I was able to effectively replace the generated POM for the "artifacts" publication with the content of an external POM via withXml:
pluginManager.withPlugin("maven-publish") {
extensions.configure(PublishingExtension::class.java) {
publications.register<MavenPublication>("artifacts") {
groupId = "com.example"
artifactId = "external"
version = "1.0.0"
artifact("external.jar")
pom.withXml {
asNode().setValue(XmlParser().parse("some/external/pom.xml").value())
}
}
}
}
This leaves the <xml ...> and <project ...> nodes intact (acceptable for my case).

Build and publish test classes with gradle.kts

We have a multiproject gradle repository with several subprojects. One of the subprojects is supposed to generate separate testJar and publish it to the local maven repository:
// build a repositories-testJar.jar with test classes for services tests
tasks.register<Jar>("testJar") {
archiveClassifier.set("testJar")
from(sourceSets["test"].output)
}
// the testJar must be built within the build phase
tasks.build {
dependsOn(tasks.withType<Jar>())
}
// needed for publishing plugin to be aware of the testJar
configurations.create("testJar") {
extendsFrom(configurations["testCompile"])
}
artifacts {
add("testJar", tasks.named<Jar>("testJar"))
}
This works and I can see -testJar.jar is generated within the /build/libs.
Then, within the rootProject gradle.kts we set up a publishing extension:
allprojects {
configure<PublishingExtension> {
publications {
create<MavenPublication>(project.name) {
groupId = "foo.bar"
artifactId = project.name.toLowerCase().replace(":", "-")
version = "ci"
from(components["kotlin"])
}
}
repositories {
maven {
url = uri("file://${rootProject.rootDir}/../m2")
}
}
}
}
This also normally works and I can see modules being published in the m2 directory. However the testJar is not published. I couldn't find a way how to attach the testJar artifact to the publication defined in root project.
Solved with addition of:
publishing {
publications {
create<MavenPublication>(project.name + "-" + testArtifact.name) {
groupId = "my.group"
artifactId = project.name.toLowerCase().replace(":", "-")
version = "version"
artifact(testArtifact)
}
}
}

Gradle MavenPublication: "Task has not declared any outputs despite executing actions"

I want to publish a zip archive to a remote maven repository. The task zipSources packs a sample file into a zip archive. The publication myPubliction publishes to mavenLocal and to the remote maven repository.
The publication works - I can see the packages uploaded to the remote repository. But the build still fails with
> Task :publishMyPublicationPublicationToMavenRepository FAILED
Task ':publishMyPublicationPublicationToMavenRepository' is not up-to-date because:
Task has not declared any outputs despite executing actions.
Publishing to repository 'maven' (null)
FAILURE: Build failed with an exception.
What am I missing? How do I declare outputs for the publishing action? Or is there another cause?
Here is my build.gradle:
plugins {
id "maven-publish"
}
group = 'com.example.test'
version = '0.0.1-SNAPSHOT'
task zipSources(type: Zip, group: "Archive", description: "Archives source in a zip file") {
from ("src") {
include "myfile.txt"
}
into "dest"
baseName = "helloworld-demo"
destinationDir = file("zips")
}
publishing {
publications {
myPublication(MavenPublication) {
artifactId = 'my-library'
artifact zipSources
pom {
name = 'My Library'
description = 'A concise description of my library'
}
}
}
repositories {
maven {
mavenLocal()
}
maven {
url "http://nexus.local/content/repositories/snapshots"
credentials {
username = 'admin'
password = 'admin'
}
}
}
}
It appears there must not be a mavenLocal() declaration inside repositories, as per https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:install

Publishing non-jar files in gradle

I am having a set of files (specifically a set of json documents) that I need to publish to my Maven repository.
How do I use publishing and specify the directory in the artifact.
publications {
myPublication(MavenPublication) {
artifact(/path/to/dir>) // directory that contains files to publish
}
}
You can create a task to zip all your documents and then publish the zip.
task jsonZip(type: Zip) {
source file(/path/to/dir)
}
publications {
myPublication(MavenPublication) {
artifact jsonZip.archivePath
}
}

Resources