Search code examples
mavengradlemaven-3github-package-registryjava-package

How do I Import a user created package in Java?


I recently hosted a Maven package on Github's package registry and now I am trying to use it. The package is intended to provide the data models that are used by various applications, allowing my team to make changes to the data model package instead of updating the models in all of the Spring applications that share them.

I put the dependency in my pom.xml file of the application that needs the data model package and then I ran mvn install (I also had to modify my .m2/settings.xml file to access the repository). I am under the impression that the package was installed because it logs the success and it showed up in my IDE's maven plugin dependency list.

And now I am stuck. Up until now, I assumed that once the package was installed I would be able to import the classes I needed just like I always have with Maven packages. But when I try to do this my IDE can't find the package.

I have started to look at the data model package again, wondering if there is some additional metadata I need to provide. Specifically, I am thinking I might need to provide some module descriptors, but I can't say for sure if this will actually get me any closer to being able to use the package.

Here is the pom.xml for the data model package.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.companyname</groupId>
    <artifactId>data-model</artifactId>
    <version>0.0.2-SNAPSHOT</version>

    <name>Data-Model</name>

    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.1.5.RELEASE</version>
        </dependency>
    </dependencies>
</project>

When I publish the package with Gradle I get this log output:

11:05:23 AM: Executing task 'publishGprPublicationToGitHubPackagesRepository'...

Starting Gradle Daemon...
Gradle Daemon started in 1 s 63 ms
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :bootJar UP-TO-DATE
> Task :generateMetadataFileForGprPublication
> Task :generatePomFileForGprPublication
> Task :publishGprPublicationToGitHubPackagesRepository

BUILD SUCCESSFUL in 16s
6 actionable tasks: 3 executed, 3 up-to-date
11:05:39 AM: Task execution finished 'publishGprPublicationToGitHubPackagesRepository'.

The log that prints when I run mvn install is very long so I won't post it, but it doesn't generate any errors, and I can see that it downloads the datamodel jar and build successfully. It also shows up in the list of external libraries in the project directory.

external libraries

The group id for the data model is the same as the group id for the application that I want to use it in (my company name). When I type import com.companyname. it suggests some local packages but doesn't recognize data model. When I hover over the red text the tooltip says Cannot resolve symbol 'datamodel'

What do I need to do now so that my application can import the classes from the maven package I created?


Solution

  • Finally figured this out.

    After tweaking the gradle.properties of the package slightly and adding

    <distributionManagement>
       <repository>
         <id>github</id>
         <name>GitHub OWNER Apache Maven Packages</name>
         <url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
       </repository>
    </distributionManagement>
    

    to the pom.xml, I still wasn't able to successfully add the dependency to Maven, despite the fact that I could see it in the list. What I finally did to fix it seems like a possible bug related to the IDE but I can't say for sure. I had already tried various combinations of invalidating cache and restarting the IDE, Maven clean and then running install, but finally was able to get the package working after I deleted the dependency from my pom.xml, ran a clean install, and then added it again and ran install one more time.

    UPDATE: for some reason, it broke again and in order to get access to the package classes for import again, I had to (from the package) run mvn clean then mvn deploy and then, from the dependent application, do the same steps listed above and delete the dependency from the pom.xml and then re-run mvn install.

    This seems like a big pain and it seems likely that (if this isn't a bug in IntelliJ) I'm doing something wrong.

    More recently I have been able to see updates to the package in the dependent application just by doing the following:

    1. In the datamodel package, run mvn clean then mvn deploy
    2. In the dependent application, run mvn install