Build file only to publish jars

Hi, I need to publish around 60 jars generated during build process to artifactory. These jars are not containing compiled source code. These jars are artifacts of a tool which is invoked during CI build. How can i publish them to artifactory. Tried few options like

artifacts {
archives myJar
}

Where myjar is a task which will produce those jars by calling a groovy script. But no success.

Unfortunately that will only work for archive tasks (Zip, Jar, Tar, etc). You’ll want to do something like:

artifacts {
    archives file('path/to/generated-file.jar')
}

Hi, thanks for your response.

Here is the content of my build.gradle. Maven plugin is used. Created an empty project called t. No source code is there.

artifacts {
archives file (‘PATH TO JAR’)
}

When i run the build file, getting below exception.

20:17:59.443 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
20:17:59.443 [ERROR] [org.gradle.BuildExceptionReporter] Execution failed for task ‘:install’.
20:17:59.443 [ERROR] [org.gradle.BuildExceptionReporter] > Could not publish configuration 'archives’
20:17:59.443 [ERROR] [org.gradle.BuildExceptionReporter] > A POM cannot have multiple artifacts with the same type and classifier. Already have MavenArtifact t:jar:jar:null, trying to add MavenArtifact t:jar:jar:null.

You’ll have to give these artifacts unique classifiers.

artifacts {
    archives(file('path/to/generated-file.jar')) {
        classifier = 'sources'
    }
    archives(file('path/to/generated-file.jar')) {
        classifier = 'tests'
    }
}

Hi, thanks for your response. If classifier is added then it works. But that still didn’t solve my problem. I need to publish around 60+jars generated out of build process. For each jar i really don’t have any unique classifiers. Only the jar name is different. They should have same group id and version.

Now i’m having two issues. One is i don’t have unique classifiers for each jar.

Second one is, the base archive name which is treated as artifact id, has to be changed for each jar. I don’t see any option to update the artifact id for each jar. Can you please help . thanks.

Here is my build file as you suggested.

artifacts {
archives (file (‘JAR1’)){
classifier = ‘C1’
}
archives (file (‘JAR2’)){
classifier = ‘C2’
}
}

You can supply a unique name as well.

artifacts {
  archives (file ('JAR1')) {
    name = 'foo'
  }
}

Hi, i tried giving “name” attribute. But that doesn’t impact the artifact name, in the artifactory, as well still classifier is needed to publish more than a jar.

Even with “name” attribute , project name is used as artifactid . I can’t use “archivesBaseName” to give unique artifact id to each jar.

Classifier is needed irrespective of “name” attribute usage.

Current build file.

// archivesBaseName = ‘XYZ’

artifacts {
archives (file (‘JAR1’)){
name = 'JAR1’
classifier = ‘C1’
}
archives (file (‘JAR2’)){
classifier = 'C2’
name = ‘JAR2’
}
}

Can someone point some help… thanks.

Hmm, it looks like you’re right. The project name is always used. Artifact name is purely for display purposes. I’m not sure there’s a way to do this with the old ‘maven’ plugin. Using the ‘maven-publish’ plugin you could do something like.

publishing {
    publications {
        foo(MavenPublication) {
            artifact 'build/foo.jar'
            group = 'com.company.gradle'
            artifactId = 'foo'
        }
        bar(MavenPublication) {
            artifact 'build/bar.jar'
            group = 'com.company.gradle'
            artifactId = 'bar'
        }
    }
}

I just used REST api to publish jars to artifactory…

:slight_smile: whatever works. Glad you got it sorted.

Hello Ganesh,
I have tried something like this,
plugins {
id “com.jfrog.artifactory” version “4.0.0”
}

configurations{
allJars
}

I generate my bar files and add them to the configuration,
artifacts{
allJars destFile
}

My artifactory config looks like,
artifactory {
contextUrl = “${artifactory_host_url}/artifactory” //The base Artifactory URL if not overridden by the publisher/resolver
publish {
//A closure defining publishing information
repository {
repoKey = ‘libs-snapshot-local’ //The Artifactory repository key to publish to
username = “${artifactory_username}” //The publisher user name
password = “${artifactory_password}” //The publisher password
}
defaults {
publishConfigs(‘allJars’)
}
}
}

I call artifactoryPublish task to get all my generated artifacts up on artifactory. Hope this helps

Thanks for your post… can you please look at artifactory and see what is the artifact name for the jars that you published. Was it the project name aka baseArchiveName. Or each jar has its own artifact name.

Actually … I generate a series of bars(message broker using commandline …exec task… mqsicreatebar) and upload them in this build and artifactory also has the same name as the generated artifacts.
eg.,
LOS_AccRelMnt_CBSServicesAPP-1.0.bar and so on.

I have a build file that publish jars and poms with dependency information. It is a little complex but makes publishing very easy.

The way it works is you define the group:module:version string that matches the file you want to publish like so:

artifacts {
    archives asArtifactMap("com.sencha.gxt:gxt:3.0.4")
}

Thats it. If you want to add dependencies to the POM of the artifact you add:

dependencies {
    gxt 'com.google.gwt:gwt-dev:2.5.0'
    gxt 'com.google.gwt:gwt-user:2.5.0'
}

The configuration name matches the module name of the artifact.

The rest of the code is:

apply plugin: 'base'
apply plugin: 'maven-publish' 
publishing {
    repositories.maven.url "${artifactory_url}/ext-release-local"
}
def module2conf( String module ) {
    return module.toLowerCase().replaceAll(/-/,'_')
}
def asArtifactMap( String name ) {
    def (group,module,version,ext) = name.tokenize(':')
    if (ext =~ /@.*/) {
        ext = ext.substring(1)
        return ['name':name, 'file':file("${module}-${version}.${ext}")]
    } else {
        return ['name':name, 'file':file("${module}-${version}.jar")]
    }
}
publishing {
    publications {
        // add a publication for every defined artifact
        configurations.archives.artifacts.each { pubArtifact ->
            def (group,module,ver) = pubArtifact.name.tokenize(':')
            create( module, MavenPublication ) {
                artifact    pubArtifact.file
                groupId     group
                artifactId  module
                version     ver
                pom.withXml {
                    def deps = asNode().appendNode('dependencies')
                    // create artifact dependencies in POM
                    configurations[module2conf(module)].dependencies.each { depInfo ->
                        def dep = deps.appendNode('dependency')
                        dep.appendNode('groupId',    depInfo['group'])
                        dep.appendNode('artifactId', depInfo['name'])
                        dep.appendNode('version',    depInfo['version'])
                    }
                }
            }
        }
    }
}