Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle is not publishing custom jar artifact

Tags:

gradle

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?

like image 349
eprst Avatar asked Nov 29 '25 20:11

eprst


1 Answers

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.

like image 172
Jk1 Avatar answered Dec 01 '25 20:12

Jk1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!