Install custom jars files with artifact id, group id and version into local maven repo

Hi Folks,

I have been trying to find a way I can install custom jars into the local maven repo with gradle. Basic I have a list of jar files I want to install with an artifact id, group id and version.

Most of the examples I found assumes gradle is building the artifacts, but in my case I download the files and now want to add them to the local maven repo

Is that possible with the current API?

Thanks

/Flemming

Take a look at Publishing Multiple Modules to Maven with the maven-publish plugin. You’ll often see the from components.java, which requires a SoftwareComponent. However, you can also use an artifact. The example directly references the output of a task, but a file(...) will work just fine too. You could list out each artifact like this:

apply plugin: 'maven-publish'

publishing {
    publications {
        jarFileOne(MavenPublication) {
            groupId 'com.group.one'
            artifactId 'jar-one'
            version '1.0'
            artifact file('jar-one-1.0.jar')
        }
        jarFileTwo(MavenPublication) {
            groupId 'com.group.two'
            artifactId 'jar-two'
            version '2.0'
            artifact file('jar-two-2.0.jar')
        }
    }
}

However, if you have your list in a parseable format with the group, artifact, version, and file name, you could easily loop through that to create each MavenPublication. Alternatively, if all necessary, unique information is embedded in the file names, just looping through all the files in the folder would be fine too. You can then handle publishing of them at once with the publishToMavenLocal task.

1 Like

Thanks for the answer.

I got so far I was able to create the Maven Publication list from a list of files with the code below and it works fine with the publishToMavenLocal as long the files exist in the configuration phase.

But in my case I download a zip file and unpack it and then scan the files, how can I lazy initializing the Maven Publication list and make it work with publishToMavenLocal?

task buildMavenPublications {
    doLast {
        FileTree ioTree = fileTree(dir: "$buildDir/", includes: ["${IDEA_UNPACK_VERSION}/lib/**/*.jar"])
        ioTree.each { f ->
            String artifactName = f.name.substring(0, f.name.indexOf(".jar"))
            publishing.publications.create("jar$artifactName", MavenPublication) {
                artifactId artifactName
                groupId 'com.intellij'
                version IDEA_DOWNLOAD_VERSION
                artifact file(f.path)
            }
        }
    }
}

task bootstrap {
    unpackIdea.mustRunAfter downloadIdea
    buildMaven.mustRunAfter unpackIdea
    publishToMavenLocal.mustRunAfter buildMavenPublications
    dependsOn("downloadIdea")
    dependsOn("unpackIdea")
    dependsOn("buildMavenPublications")
    dependsOn("publishToMavenLocal")
}

Thanks

With your current code, I would expect that the publishing extension isn’t being configured soon enough. The plugin can handle creation of publications in the executing phase, just not if it already is getting ready to publish and there isn’t anything to publish yet. The bootstrap task depending on all of the other tasks with mustRunAfter ordering doesn’t model the dependencies accurately enough. This should work correctly if you model the true dependencies of each task.

unpackIdea.dependsOn downloadIdea
buildMavenPublications.dependsOn unpackIdea
publishToMavenLocal.dependsOn buildMavenPublications

task bootstrap {
    dependsOn publishToMavenLocal
}

I might even call buildMavenPublications a finalizer task of unpackIdea if your sole purpose of unpacking is to create the publications.