Artifact is not published using gradle and artifactory plugin - gradle

I'm trying to upload a simple zip to artifactory using gradle. This is my script:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:latest.release"
}
}
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
def user = "$System.env.ARTIFACTORY_USER"
def pass = "$System.env.ARTIFACTORY_PASSWORD"
task createArchive(type: Zip){
baseName 'myzip'
destinationDir(new File('target'))
from '.'
exclude 'target'
exclude '.gradle'
exclude 'build.gradle'
exclude 'README.md'
doFirst{
new File('target').mkdirs()
}
}
task clean(type: Delete){
delete 'target'
}
publishing {
publications {
customArtifact(MavenPublication) {
artifact createArchive
}
}
}
artifactory {
publish {
contextUrl = 'http://myrepo/artifactory'
repository {
repoKey = "myrepo-tools"
username = user
password = pass
}
publications ('customArtifact')
}
}
The code seems to work fine but actually only the build info are published. The zip is not:
Deploying build descriptor to: http://myrepo/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under http://myrepo/artifactory/webapp/builds/myproject/1503338121156
My code does not seem different from the one published here: How to build Groovy JAR w/ Gradle and publish it to in-house repo (which was pointed as reference in question nothing published to artifactory using gradle artifactory plugin) but I'm not sure that is still valid since is quite old.
The main difference is that almost all the examples are using java plugin so they have the from components.java line in the publications. In my case I cannot use it as I have a custom zip.. but I can't find what else can be changed.
Thanks,
Michele.

Related

How do I suppress POM and IVY related warnings in Gradle 7?

After upgrading to Gradle 7 I have many warnings like:
Cannot publish Ivy descriptor if ivyDescriptor not set in task ':myProject:artifactoryPublish' and task 'uploadArchives' does not exist.
Cannot publish pom for project ':myProject' since it does not contain the Maven plugin install task and task ':myProject:artifactoryPublish' does not specify a custom pom path.
The artifactoryPublish task works fine.
My Gradle script:
buildscript {
repositories{
maven {
url = '...'
credentials {
username '...'
password '...'
}
metadataSources {
mavenPom()
artifact()
}
}
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.24.12"
}
}
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
suppressAllPomMetadataWarnings()
}
}
}
group = '...'
artifactory {
contextUrl = '...'
publish {
repository {
repoKey = '...'
username = '...'
password = '...'
}
defaults {
publishConfigs('archives')
publishIvy = false
publications("mavenJava")
}
}
}
How do I disable those warnings?
It looks like you mixed between the old Gradle publish-configuration method and the new Gradle publication method.
You applied the maven-publish plugin which allows creating publications. In artifactory.default, you added the "mavenJava" publication as expected.
However, the archives publish-configuration doesn't exist in your build.gradle file. Basically, publish-configurations are created by the legacy maven plugin. The configured mavenJava publication does the same as the archives publish-configuration and therefore all of the JARs are published as expected.
To remove the warning messages you see, remove the publishConfigs('archives') from artifactory.default clause:
artifactory {
publish {
defaults {
publishConfigs('archives') // <-- Remove this line
publishIvy = false
publications("mavenJava")
}
}
}
Read more:
Gradle Artifactory plugin documentation
Example

Gradle how to publish a gradle-plugin to maven central

Hi I have a java project, which contains a submodule which is a gradle plugin. I want to publish this module to maven central.
I used this two plugins in my build.gradle:
plugins {
id 'java-gradle-plugin'
id 'maven-publish'
}
and my publishing block looks something like:
publishing {
publications {
javaLibrary(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
artifactId = project.archivesBaseName
pom {
name = artifactId
description = "..."
url =
licenses {
license {
}
}
developers {
...
}
scm {
...
}
issueManagement {
...
}
ciManagement {
...
}
}
}
}
repositories { maven { url = "some local repo" } }
}
I noticed that when I build this module, the generated pom-default.xml is what I expected, but when I run gradle publishToMavenLocal and manually checked the pom.xml file in the .m2 folder, all the metadata like name description licenses are gone!
I also noticed in the .m2 folder there are 2 artifacts that are related to this single plugin, I think it's somewhat related with https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_markers but I don't fully understand the meaning. Both these 2 artifacts' pom are missing the pom metadata as I described above.
Could some gradle expert help me here: how to keep the metadata in the published pom?
You should not need to manually define a MavenPublication for your plugin submodule. The java-gradle-plugin reacts to the application of the maven-publish plugin and automatically configures/creates publications for the plugin artifacts. See this line.
You are correct for the (2) artifacts produced. One is the plugin marker (single pom.xml) and the other is the actual plugin JAR artifact.
As for POM customization, Gradle seemingly provides its own the POM irrespective of any POM customization(s) you have defined: https://github.com/gradle/gradle/issues/17022
Sorry for late answer, but you can do something like this:
afterEvaluate {
tasks.withType(GenerateMavenPom) { task ->
doFirst {
// Update POM here
def pom = task.pom
pom.name = ...
pom.url = ...
pom.description = ...
pom.scm {
...
}
}
}
}
This will catch the pom of the plugin marker artifact as well.

Can't Publish SubProjects with Gradle

I have a gradle project, it has jvm (Scala) subprojects, and I want to publish all of their jars to a m2 repository.
I applied maven-publish like this ...
allprojects {
apply plugin: 'maven-publish'
publishing {
repositories {
// path to somewhere in my repo
maven { url new File(project.rootProject.getProjectDir().absoluteFile.parentFile, ".m2-repo") }
}
}
}
... but it only publishes the root project. I've tried running publish from the subprojects; I get an up-to-date message but nothing is added to the repo on disk.
I'm using the 4.0.1 GradeWrapper
It needs to be;
apply plugin: 'maven-publish'
publishing {
repositories {
// publish to a "local" repo that I can also consume or upload as I wish
maven { url new File(project.rootProject.getProjectDir().absoluteFile.parentFile, ".m2-repo") }
}
// really, really publish things
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}

Publish wrapped OSGi bundles to artifactory with Gradle

As not all jars are automatically usable OSGi bundles I use wrapping to generate them. After having being wrapped I'd like to publish them to my Artifactory repository. However, my lack of understanding of Gradle inhibits success, and after reading several suggested Stackoverflow answers I am still stuck.
This is my build.gradle file:
buildscript {
repositories {
maven { url 'http://localhost:8081/artifactory/gradle-dev' }
}
dependencies {
classpath 'org.standardout:bnd-platform:1.2.0'
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4+"
}
}
apply plugin: 'org.standardout.bnd-platform'
apply plugin: "com.jfrog.artifactory"
apply plugin: 'maven-publish'
group = 'com.google.code.gson'
version = '2.8.0'
publishing {
publications {
osgiBundles(MavenPublication) {
artifacts {
files("build/plugins")
}
}
}
}
artifactory {
contextUrl = "${artifactory_contextUrl}" //The base Artifactory URL if not overridden by the publisher/resolver
publish {
repository {
repoKey = 'gradle-dev-local'
username = "${artifactory_user}"
password = "${artifactory_password}"
maven = true
}
defaults {
publications ('osgiBundles')
}
}
resolve {
repository {
repoKey = 'gradle-dev'
username = "${artifactory_user}"
password = "${artifactory_password}"
maven = true
}
}
}
platform {
useBndHashQualifiers = false
defaultQualifier = ''
bundle(group: 'com.google.code.gson', name:'gson', version:'2.8.0') {
bnd {
instruction 'Export-Package', 'com.google.gson,com.google.gson.stream,com.google.gson.annotations,com.google.gson.reflect'
}
}
}
The output of the script is as follows:
gradle artifactoryPublish
:generatePomFileForOsgiBundlesPublication
:artifactoryPublish
Deploying artifact: http://localhost:8081/artifactory/gradle-dev-local/com/google/code/gson/bundle-jars/2.8.0/bundle-jars-2.8.0.pom
Deploying build descriptor to: http://localhost:8081/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under http://localhost:8081/artifactory/webapp/builds/bundle-jars/1489323863518
When I look in the artifactory repository the structure is not what I expected:
+- com
+--- google/code/gson/bundle-jars
|+-- 2.8.0
| +- bundle-jars.pom
+--- maven-metadata.xml
The wrong directory structure (google/code/gson/bundle-jars), where I expected several sub directorties (google, code, gson) with a 2.8.0 and a jar file.
I think I have to change the publications block, but I don't know what it should be.
I use unpuzzle (or rather this fork) to create Maven artifacts from OSGi bundles (and publish them to Artifactory).
This is probably not the most efficient solution for your use case, but at least something that works and I can come up with fast.
Here is an example of where I use unpuzzle for this purpose. Maybe that can serve as a starting point (together with the unpuzzle docs). There is a lot of bloat in my example because there I try to actually determine the original Maven artifacts for OSGi bundles created from them - as you always want the OSGi bundle, that's probably not relevant for you.
Note that by default the published artifacts will have different names (based on the bundle symbolic name) and a different group (which is configurable). But I think that is to be preferred over having the original group and name, otherwise it may get confused w/ the original. Adapting the group and name individually is possible as you can see in the example.
The publication should be as follows:
publishing {
publications {
osgiBundles(MavenPublication) {
groupId 'my.group'
artifactId 'com.google.code.gson'
version '2.8.0'
artifact file("build/plugins/com.google.code.gson_2.8.0.jar")
}
}
}

Generate local maven repository with all dependencies using Gradle

What I currently have
dependencies {
compile ...
}
task copyDependencies(type:Copy) {
from configurations.compile
into 'build/dependencies/'
}
This task copies all the required dependencies to the build/dependencies/ directory. Inside the directory, it looks as follows:
/dep1-1.0.jar
/dep2-1.0.aar
...
So essentially what is known as flatDir in gradle terms.
What I'd like to have
It's a local maven repository with all these dependencies, instead of a flatDir.
If I understand your question correctly, you just want to publish to a local directory as if it were mavenLocal() but in a location you specify.
In that case, I believe you just need:
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
url "/path/to/wherever"
}
}
}
See the Maven Publish plugin docs

Resources