Caspar Romot
Caspar Romot

Reputation: 11

How to add a jar file that is in a git repository as a dependency in gradle?

I have 2 projects and I am publishing some class files of project 1 to bitbucket as a jar file. Now I need to somehow include that jar file as a dependency in project 2. Here is the publishing code in project 1 build.gradle (I am using a gradle-git-publish library gradle plugin):

task myJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'My Jar',
            'Implementation-Version': '1.0.0'
    }
    baseName = "my-project"
    from sourceSets.main.output
    include 'com/my/project/deep/nested/files/*.class'
    eachFile { FileCopyDetails file -> file.setPath('com/my/project/files/' + file.getName()) }
    includeEmptyDirs = false
    with jar
}

gitPublish {
    repoUri = '[email protected]:myusername/my-repo.git'

    branch = 'master'

    contents {
        from 'build/libs'
    }

    commitMessage = 'Latest commit'
}

gitPublishPush.dependsOn myJar

And here is some of what i have tried so far in project 2 to add the jar as a dependency.

repositories {
    mavenCentral()

    maven {
        url 'https://bitbucket.org/myusername/my-project/src/master/'
        artifactUrls 'https://bitbucket.org/myusername/my-project/src/master/'

    }

    ivy {
        url "https://bitbucket.org/myusername/my-project/src/master"
        layout 'pattern' , {
            artifact 'my-project-0.0.1-SNAPSHOT.jar'
        }
    }
}

dependencies {
    implementation 'com.my.project:my-project:0.0.1-SNAPSHOT'
}

Gradle build reports that it is downloading the jar file, but after the build it is still "Unable to resolve com.my.project:my-project:0.0.1-SNAPSHOT". Do you have any ideas on how to fix this issue, or am i approaching the whole problem with a faulty solution?

Upvotes: 1

Views: 1553

Answers (1)

Oren_C
Oren_C

Reputation: 761

Thanks for asking :)
Seems to me that your projects are too tightly-connected. I would advise you to build your jar file in Project 1 and then commit it to project 2 with a message that includes the commit hash of Project 1 where this jar came from. This would make things easier to manage when you need to find problems while also removing the necessity for cross git project tasks such as the one you have described above. And also, your git projects remain independent.

Consider a human mistake of trying to build project 2 while project 1 is not existent on the machine, or not available in the hard-coded URL anymore, this will break things. If you have any other questions, please let me know.

Upvotes: 2

Related Questions