Search code examples
javamavenvisual-studio-coderaspberry-pi2pi4j

Add a dependency from a maven project to a non-maven project


I'm in the process of writing a java program for a Raspberry Pi where I want to access the GPIO. For this I use the Pi4J-Library Version 2 (https://pi4j.com). As IDE I use Visual Studio Code - as they suggest.

In my VSC workspace are included:

I can compile and package the minimal example application and my own project using the appropriate maven commands.

Then I have a java library for desktop applications I wrote myself. I added this library and a project, that uses the library, to the workspace. I can run this desktop application from VSC. The library and the application where originally written using Eclipse. Maven is not involved.

Now I'm trying to use a class from the desktop-library in my Pi4J-project: new MyLibClass

VSC displays "MyLibClass cannot be resolved to a type". I have imported the class: import package.name.MyLibClass. Obviously the Pi4J-project doesn't know where to find the class. But when I CTRL-click the class name, it opens the corresponding file.

I think I need to add a dependency to the pom.xml of my Pi4J-project. But I have absolutely no idea what to specify for groupId, artifactId and version. The desktop-library is not a maven project.

Thanks a lot for your help in advance!


Solution

  • Maven supports three types of repository: local, central and remote. Normally, dependencies you add to your pom.xml file are pulled from the central repository. What you can do is compile your library to a jar, and drop that in your local repository, which can be found in one of the following locations depending on your OS:

    • Windows: C:\Users\<User_Name>\.m2
    • Linux: /home/<User_Name>/.m2
    • Mac: /Users/<user_name>/.m2

    You can install your jar in your local repository as follows:

    mvn install:install-file \
       -Dfile=<path-to-file> \
       -DgroupId=<group-id> \
       -DartifactId=<artifact-id> \
       -Dversion=<version> \
       -Dpackaging=<packaging> \
       -DgeneratePom=true
    

    After that the jar will be copied into your local repository in a folder structure that mirrors the groupId. And since you've provided a custom groupId, artifactId and version you can use those to add the dependency to your pom.xml

    Alternatively, you can add a local repository to your project and install your jar there, then add the dependency to your pom.xml as normal.