Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle task for deploying vendored libraries into local maven repository

I'm trying to figure out how to get gradle to deploy some jar files into the local maven repository, to support the rest of the build system. A dependency that I have against something has its own dependency on jndi:jndi:1.2.1 which isn't available in jcenter or maven central.

What I've done (as suggested in the docs of my dependency - Jira for what it's worth) is downloaded the jndi.jar file, and have run the following:

mvn install:install-file -Dfile=lib/jndi.jar -DgroupId=jndi \
-DartifactId=jndi -Dversion=1.2.1 -Dpackaging=jar

That works fine. But I'd like gradle to be able to execute a task that will install this file to the local maven repository, to make CI easier and to make on-boarding other developers easier.

I've tried to follow the recommendations here (with code), but I'm not having much luck. Here's an excerpt from my build.gradle:

apply plugin: 'java'
apply plugin: 'maven'

artifacts {
    archives(file('lib/jndi.jar')) {
        name 'jndi'
        group 'jndi'
        version '1.2.1'
    }
    archives(file('lib/jta-1_0_1B.jar')) {
        name 'jta'
        group 'jta'
        version '1.0.1'
    }
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath)
        }
    }
}
install.dependsOn(uploadArchives)

And when I run the install task:

$ gradle --version

------------------------------------------------------------
Gradle 2.4
------------------------------------------------------------

Build time:   2015-05-05 08:09:24 UTC
Build number: none
Revision:     5c9c3bc20ca1c281ac7972643f1e2d190f2c943c

Groovy:       2.3.10
Ant:          Apache Ant(TM) version 1.9.4 compiled on April 29 2014
JVM:          1.8.0_11 (Oracle Corporation 25.11-b03)
OS:           Mac OS X 10.10.3 x86_64

$ gradle install
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:uploadArchives FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':uploadArchives'.
> Could not publish configuration 'archives'
   > A POM cannot have multiple artifacts with the same type and classifier. Already have MavenArtifact engage-jira:jar:jar:null, trying to add MavenArtifact engage-jira:jar:jar:null.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Now, I don't really want to install my artifacts to the local maven repository - just the transitive dependencies. I don't mind if my artifact is installed though.

Update - Solution:

So, this seems to do the trick:

repositories {
    jcenter()

    maven { 
        url './lib' 
    }
}

dependencies {
    runtime files(
       'lib/jndi/jndi/1.2.1/jndi-1.2.1.jar', 
       'lib/jta/jta/1.0.1/jta-1_0_1.jar'
    )
    runtime fileTree(dir: 'lib', include: '*.jar')
}

I moved the libs into the folder structure expected by maven, added the local folder structure into the repositories stanza, and added the runtime files into dependencies.

No stuffing about with the localhost global repository, executing command lines, or anything like that. It'd nice to have better support for local transitive dependencies, but how often is that required in practise?

like image 889
Josh Smeaton Avatar asked Sep 28 '22 19:09

Josh Smeaton


1 Answers

Gradle allows adding dependencies directly to your build, without first installing them to the local repository.

dependencies {
    runtime files('lib/jndi.jar', 'lib/jta-1_0_1B.jar')
    runtime fileTree(dir: 'lib', include: '*.jar')
}

That should work right away. Eventually, you might want to set up a Maven repository manager, e.g. Artifactory, install your missing libraries there, and refer to them in your build like this:

repositories {
    maven { url "http://192.168.x.x/artifactory/local-repository" }
    mavenCentral()
}

dependencies {
    compile "jndi:jndi:1.2.1"
    compile "jta:jta:1.0.1"
}
like image 52
approxiblue Avatar answered Oct 06 '22 18:10

approxiblue