I am using shadowJar in my java project. I would like to push the outcome into artifactory.
My gradle script look like this, I am not sure how to connect the dots:
shadowJar {
baseName = 'com.mycompany.myapp'
manifest {
attributes 'Main-Class': 'myapp.starter'
}
}
]
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'com.github.johnrengelman.shadow'
// rep for the project
repositories {
mavenCentral()
maven {
url 'http://repo.company:8081/artifactory/libs-release'
credentials {
username = "${repo_user}"
password = "${repo_password}"
}
}
maven {
url 'http://repo.company:8081/artifactory/libs-snapshot'
credentials {
username = "${repo_user}"
password = "${repo_password}"
}
}
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}
How do I code gradle to take the shadowjar file?
thanks, ray.
It's a bit late, but I'd like to show you how I got it to work. Maybe it helps someone stumbling across this question as I did:
build.gradle.kts:
plugins {
java
`maven-publish`
id("com.github.johnrengelman.shadow") version "5.1.0"
}
publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
artifact(tasks["shadowJar"])
}
}
repositories {
maven {
/* ... */
}
}
}
The example above is written in Kotlin, if you prefer Groovy, you would need to write this instead:
build.gradle:
publishing {
publications {
shadow(MavenPublication) {
from components.java
artifact shadowJar
}
}
}
(found it here: https://libraries.io/github/johnrengelman/shadow )
The publication
section determines what you're publishing using the maven-publish
plugin.
In your current config, from components.java
is going to publish the default jar artifact of your project and artifact sourceJar
publishes the sourceJar. In order to publish a different jar, you need to modify (or add a new) publication.
shadowJar {
baseName = 'myproject-shadow'
classifier = ''
}
publishing {
publications {
shadow(MavenPublication) {
from components.shadow
artifactId = 'myproject-shadow'
}
}
}
The version used in the name of the jar comes from project.version.
The API has changed in the shadow
plugin, this works for me with com.github.jengelman.gradle.plugins:shadow:2.0.1
:http://imperceptiblethoughts.com/shadow/#publishing_shadow_jars
5. Publishing Shadow JARs
5.1. Publishing with Maven-Publish Plugin
The Shadow plugin will automatically configure the necessary tasks
in the presence of Gradle’s maven-publish plugin. The plugin provides
the component method from the shadow extension to configure the
publication with the necessary artifact and dependencies in the
POM file.
Publishing a Shadow JAR with the Maven-Publish Plugin
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'com.github.johnrengelman.shadow'
publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
}
}
repositories {
maven {
url "http://repo.myorg.com"
}
}
}
Here is what I did to get Gradle to publish to our Nexus repo using the shadow plugin and the maven-publish plugin: (run publish task: "gradle publish")
plugins {
id 'com.github.johnrengelman.shadow' version '1.2.4'
}
apply plugin: 'maven-publish'
publishing {
publications {
shadow(MavenPublication) {
from components.shadow
groupId 'myGroupId'
artifactId 'myArtifactId'
}
}
repositories {
maven {
url "http://nexus.mycompany.com/repository/stuff"
credentials {
username = "${System.env.NEXUS_USERNAME}"
password = "${System.env.NEXUS_PASSWORD}"
}
}
}
}
Have you looked at the Artifactory Gradle Plugin ? This can help you with what you need, resolving and deploying artifacts from and to Artifactory.
You can publish using the plugin by omitting the 'artifactory' plugin from a project that does not publish anything. Note that this does not apply to the root project that contains the convention object, and so, requires the plugin to be applied. Activate the corresponding artifactoryPublish Gradle task manually for each project to which you wish to apply the plugin. For example in our Gradle project example you can run: ./gradlew clean api:artifactoryPublish shared:artifactoryPublish
You can just use this:
plugins {
kotlin("jvm")
`maven-publish`
id("com.github.johnrengelman.shadow") version "6.0.0"
}
publishing {
publications {
val publication = create<MavenPublication>("shadow")
project.shadow.component(publication)
}
}
None of the other answers take multimodule projects into consideration. For a multimodule project, no one configures publication for every single module; instead, it's done in the root build file.
Also, in a multimodule project, usually only one module needs to be shadowed as an executable JAR. It makes no sense to apply the shadow plugin to every module.
Here's how to achieve all of the above using Kotlin DSL:
In the subproject:
plugins {
id("com.github.johnrengelman.shadow")
}
...
project.tasks.findByName("jar")?.enabled = false
tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
mergeServiceFiles()
archiveClassifier.set("")
manifest {
attributes(mapOf("Main-Class" to "com.mycompany.MainClass"))
}
}
In the root project:
publications {
create<MavenPublication>("maven") {
afterEvaluate {
val shadowJar = tasks.findByName("shadowJar")
if (shadowJar == null) from(components["java"])
else artifact(shadowJar)
}
artifact(kdocJar)
artifact(sourcesJar)
}
...
}
The key is the afterEvaluate
configuration. See this for details.
The project.shadow.component(publication)
everyone keeps talking about doesn't exist in the root project and will not compile in a Kotlin DSL. It's the ShadowExtension
that only exists in the project where the plugin is applied.
Once you deploy the Shadow plugin, you can then run the publishShadowPublicationToMavenLocal
and publishShadowPublicationToMavenRepository
gradle tasks.
These is the same as the corresponding tasks from theMaven publishing gradle plugin such as publishToMavenLocal
, but it publishes the shadow jar instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.