Alex
Alex

Reputation: 7491

How to use in gradle a jar from the Local Maven repository

For some reason cannot find an example that shows publishing a jar in one project and then using this jar in another project.

So, my first library project has the following build.gradle:

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'

version = '1.0.0'
repositories {   mavenLocal() }
group = 'com-hlibrary'
publishing { 
    publications { 
         mavenJava(MavenPublication) { 
             from components.java
          }
       }
   }

This script publishes a jar with the following path:

~/.m2/repository/com-hlibrary/gradle-library/1.0.0/gradle-library-1.0.0.jar

The pom file gradle-library-1.0.0.pom in the same directory is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com-hlibrary</groupId>
<artifactId>gradle-library</artifactId>
<version>1.0.0</version>
</project>

The Java file I want to use is as follows:

package com.hlibrary;
import java.time.LocalTime;
public class HourClass {
    public static int getHour() { 
        LocalTime localTime = LocalTime.now(); 
        return localTime.getHour();
    }
}

So, in my another project I have a gradle.build with the dependency to the built jar:

apply plugin: 'java-library'
apply plugin: 'maven'
apply plugin: 'maven-publish'
repositories { 
    jcenter()
    mavenLocal() 
}

dependencies { 
    compile ("com.hlibrary:gradle-library:1.0.0")
}

However after I type

 gradle dependencies 

I see numerous errors like this:

compile - Dependencies for source set 'main' (deprecated, use 'implementation ' instead).
\--- com.hlibrary:gradle-library:1.0.0 FAILED

Upvotes: 4

Views: 8332

Answers (2)

Mike Patrick
Mike Patrick

Reputation: 11007

I think the problem is that your groups don't match.

When you publish, you're using group com-hlibrary. When you consume the dependency, you're using group com.hlibrary.


This means that (as you noted) your .jar is being published to

~/.m2/repository/com-hlibrary/<project-name>

But then your client application is looking for it in

~/.m2/repository/com/hlibrary/<project-name>

If you're consistent with dot vs. dash, this code works as expected for me using Gradle 4.0.

Edit:

Also, if you run gradle clean jar, you'll get a much more useful error message than gradle dependencies gives you:

* What went wrong:
Could not resolve all files for configuration ':compileClasspath'.
> Could not find com.hlibrary:LibraryTest:1.0.0.
  Searched in the following locations:
      https://jcenter.bintray.com/com/hlibrary/LibraryTest/1.0.0/LibraryTest-1.0.0.pom
      https://jcenter.bintray.com/com/hlibrary/LibraryTest/1.0.0/LibraryTest-1.0.0.jar
      file:/C:/path/to/home/.m2/repository/com/hlibrary/LibraryTest/1.0.0/LibraryTest-1.0.0.pom
      file:/C:/path/to/home/.m2/repository/com/hlibrary/LibraryTest/1.0.0/LibraryTest-1.0.0.jar
  Required by:
      project :

Upvotes: 4

acesar
acesar

Reputation: 185

Try instead

compile ("com.hlibrary:gradle-library:1.0.0 )

The format is "group:artifactId:version"

you can check the .pom file in

~/.m2/repository/com-hlibrary/gradle-library/1.0.0/gradle-library-1.0.0.pom

Upvotes: 2

Related Questions