How to change the jar filename when upload to artifactory via gradle - gradle

I am using gradle in order to upload jar to artifactory.
I managed to do it however I am trying to change the jar filename but it doesnt really let me.
I am using shadowJar to package. this is how I do it:
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'maven-publish'
apply plugin: 'com.github.johnrengelman.shadow'
shadowJar {
classifier = ''
baseName = 'com.mycompany.app-all'
manifest {
attributes 'Main-Class': 'com.mycompany.app.main.starter'
}
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.shadow
groupId 'com.mycompany'
artifactId "app"
version "${build_version}"
}
}
}
Now if build_version=2.1 than the dirs on artifactory will look like this:
http://repo.address:8081/artifactory/libs-release-local/com/mycompany/app/2.1/app-2.1.jar
I would like to keep the folder structure but change the jar filename(as defined in shadowJar)
and to have it this way:
http://repo.address:8081/artifactory/libs-release-local/com/mycompany/app/2.1/com.mycompany.app-all.jar
any idea?

The Jar task default naming convention is
[baseName]-[appendix]-[version]-[classifier].[extension]
If you set just the basename, the remaining values get appended to it. To override, set archiveName instead of baseName
shadowJar {
...
archiveName = 'com.mycompany.app-all'
...
}
To change naming on what artifactory is publishing:
publishing {
publications {
mavenJava(MavenPublication) {
from components.shadow
groupId 'com.mycompany'
artifactId 'com.mycompany.app-all' //<-- changed here
version '' // and here
}
}
}

As mentioned in the above answer, to modify the jar name to be published to artifactory , just adding the below to publishing worked for me.
artifactId = 'ChangedJarName'

Related

How do I resolve the "Cannot assign 'String' to 'Publication'" Gradle errors in IntelliJ?

I'm using Gradle 6.7 for a project and I'm using IntelliJ. When writing Gradle files or when running inspections, I keep getting this error.
"Cannot assign 'String' to 'Publication'".
Based on the Gradle docs and all the examples, it seems as though my configuration is correct but I'm unable to resolve these warnings.
import org.gradle.api.tasks.testing.logging.TestLogEvent
buildscript {
buildscript {
repositories {
google()
mavenCentral()
gradlePluginPortal()
jcenter()
}
dependencies {
classpath "org.github.ngbinh.scalastyle:gradle-scalastyle-plugin_2.11:1.0.1"
classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.8"
classpath "net.ltgt.gradle:gradle-nullaway-plugin:0.2"
classpath "gradle.plugin.com.dorongold.plugins:task-tree:1.5"
}
}
}
apply plugin: 'java'
apply plugin: 'scalaStyle'
apply plugin: 'scala'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'java-library'
apply plugin: 'net.ltgt.errorprone'
apply plugin: 'net.ltgt.nullaway'
apply plugin: "com.dorongold.task-tree"
apply plugin: "jacoco"
group 'com.mridang'
version '0.0.2'
...
...
publishing {
repositories {
maven {
name = "github"
url = uri("https://maven.pkg.github.com/mridang/myrepo")
credentials(PasswordCredentials)
}
}
publications {
maven(MavenPublication) {
from components.java
artifact sourcesJar
artifact scaladocJar
pom {
name = 'myproject'
description = 'Foo'
url = 'https://github.com/mridang/myrepo'
issueManagement {
system = "Github"
url = "https://github.com/mridang/myrepo/issues"
}
}
}
}
}
Here's a screenshot that better illustrates the issue:
It is false positive inspection warning. You can vote for this issue in tracker: IDEA-162281.

SoftwareComponentInternal with name 'java' not found

App module build.gradle
apply plugin: 'com.android.library'
apply from: rootProject.file('deploy-bintray.gradle.kts')
android {...}
deploy-bintray.gradle.kts it's my bintray/maven publications script.
I'm having problems generating .jar files:
val sourcesJar by tasks.registering(Jar::class) {
archiveClassifier.set("sources")
from(project.the<SourceSetContainer>()["main"].allSource)
}
publications {
create<MavenPublication>(bintrayRepo) {
groupId = publishedGroupId
artifactId = artifact
version = libraryVersion
from(components["java"])
artifact(sourcesJar.get())
artifact(dokkaJar.get())
...
}
}
}
it fails with:
SoftwareComponentInternal with name 'java' not found.
or, if I comment from(components["java"]) it fails with:
SourceSet with name 'main' not found.
If I add java plugin:
The 'java' plugin has been applied, but it is not compatible with the
Android plugins.
So I'm stuck here. How can I solve this?
I finally found a solution!
I was doing a few things wrong, first, both dokkaJar and sourceJar tasks have to be in the main build.gradle and not inside deploy-bintray.gradle.kts. Moving them made it work and fixes:
SourceSet with name 'main' not found.
Secondly we cannot use from(components["java"]) because this is an Android lib so I've replaced that line with artifact("$buildDir/outputs/aar/${artifactId}-release.aar").
Last but not least, as stated here (step 7):
"Also, the POM file generated does not include the dependency chain so
it must be explicitly added..."
I had to add this:
pom {
...
withXml {
val dependenciesNode = asNode().appendNode("dependencies")
configurations.getByName("implementation") {
dependencies.forEach {
val dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", it.group)
dependencyNode.appendNode("artifactId", it.name)
dependencyNode.appendNode("version", it.version)
}
}
}
}

Gradle will not publish artifact?

Gradle v4.10.2
I’m building a Gradle Java plugin, and it builds. However when I run ./gradlew publish it does nothing, i.e., the artifact doesn’t get published. Here’s my build.gradle file (I have all the variables defined in my gradle.properties file). Also, if I just run ./gradlew publish w/o running ./gradlew build first, it doesn’t run the build phase. What am I missing in my build.gradle file? Thanks.
plugins {
id 'java'
id 'maven'
id 'maven-publish'
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'
group=project.groupId
version = '0.0.1'
jar {
manifest {
attributes 'artifactId': project.artifactId,
'groupId': project.groupId,
'version': project.version
}
baseName artifactId
doLast {
println "artifactId: $project.artifactId\ngroupId: $project.groupId\nversion: $version"
}
}
dependencies {
compile gradleApi()
}
// For publishigh to S3 maven repo
publishing {
repositories {
maven {
url "s3://" + s3_bucket
credentials(AwsCredentials) {
accessKey AWS_ACCESS_KEY
secretKey AWS_SECRET_KEY
}
}
}
}
RTFM. I was missing the publications block inside the publishing block. Here’s the whole block
publishing {
publications {
myLibrary(MavenPublication) {
from components.java
}
}
repositories {
maven {
url "s3://" + s3_bucket
credentials(AwsCredentials) {
accessKey AWS_ACCESS_KEY
secretKey AWS_SECRET_KEY
}
}
}
}

gradle maven-publish plugin only publishing first sub-modules artifacts

I have a multi-module project as follow:
top-level-module
sub-module-1
sub-module-2
sub-module-3
Top level module gradle config looks like this:
...
def javaProjects() {
subprojects.findAll { new File(it.projectDir, 'src/main/java').directory }
}
configure(javaProjects()) {
apply plugin: "io.franzbecker.gradle-lombok"
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: "jacoco"
apply plugin: 'maven'
apply plugin: 'maven-publish'
...
publishing {
repositories {
maven {
url = ...
credentials {
username = ...
password = ...
}
}
}
}
}
sub-module-1 and sub-module-2 have a gradle config like this:
plugins {
id 'java'
id 'groovy'
id 'application'
id 'maven'
id 'maven-publish'
}
mainClassName = 'com.mycompany.MyClass'
applicationName = 'xxx-cli' // xxx-cli is different for both modules
...
publishing {
publications {
XXXCliTar(MavenPublication) { // XXXCliTar is different in two modules
artifact(distTar)
artifactId "${applicationName}"
}
}
}
When I use the publish task as follows:
gradle -i build publish
I find that only the artifacts from sub-module1 is published.
What is really odd about this is that this only happens when run in Jenkis job (on a linux slave). It dos not happen when run on my windows dev machine!
I am wondering why artifacts from sub-module2 is not published.
I think there is a bug in the maven-publish plugin. The workaround was to not define the publishing.repositories.maven config in root module and instead duplicating it in sub-module1 and sub-modules2 like this:
publishing {
publishing {
repositories {
maven {
url = ...
credentials {
username = ...
password = ...
}
}
}
}
publications {
XXXCliTar(MavenPublication) { // XXXCliTar is different in two modules
artifact(distTar)
artifactId "${applicationName}"
}
}
}
Make sure to not apply the maven-publish plugin in rootProject's common config for sub-modules.

Gradle is not publishing custom jar artifact

I have a gradle build that must publish a pre-built jar file as an artifact. By some reason it is not being picked up. Here's a simplified version to reproduce it:
folder contents:
settings.gradle
build.gradle
some.jar
settings.gradle:
rootProject.name = 'some'
build.gradle:
apply plugin: 'base'
apply plugin: 'maven-publish'
group = 'foo.bar'
version = '1.0'
task copyPrebuilt(type: Copy) {
from(projectDir) {
include 'some.jar'
rename 'some', "some-$version"
}
into distsDir
}
artifacts {
// should be 'archives'?
// http://stackoverflow.com/questions/39068090/gradle-archives-artifacts-not-populated-to-default-configuration
'default' (file("$distsDir/some-${version}.jar")) {
name 'some'
type 'jar'
builtBy copyPrebuilt
}
}
some-1.0.jar is not copied to ~/.m2/repository/foo/bar/some/1.0 by gradle publishToMavenLocal
any clues?
The easiest way to get something published is to define a publication object:
apply plugin: 'base'
apply plugin: 'maven-publish'
group = 'foo.bar'
version = '1.0'
task copyPrebuilt(type: Copy) {
from(projectDir) {
include 'some.jar'
rename 'some', "some-$version"
}
into buildDir
}
publishToMavenLocal.dependsOn copyPrebuilt
publishing {
publications {
prebuilt(MavenPublication) {
artifact file("$buildDir/some-${version}.jar")
}
}
}
This code makes publishToMavenLocal task install some-1.0.jar to the local maven repository.

Resources