1

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!

Joe
  • 53
  • 7

1 Answers1

2

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.

JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24
  • 1
    Thanks for your answer. The disadvantage with the jar file is, that with every change in the library I have to generate the jar and install it, correct? I see that the best way would be, that the library is also a maven project. I'm currently trying this way. Since the library doesn't use the maven structure I think I have to do pretty much configuration in the pom.xml. – Joe Nov 17 '21 at 21:28
  • 1
    @Joe Correct. Another option is, if you can and want, to just publish your library to the central maven repository. That way not just you, but anyone else, could use it. I limited my answer to installing to the local repository because it was what required less effort from you, although it comes with the disadvantage you mention, and not everyone can or wants to convert their project to a maven project and/or publish it to maven central. – JustAnotherDeveloper Nov 17 '21 at 21:48
  • The library is already available on SourceForge. I don't want to publish it to the central maven repository because the library doesn't use the correct maven structure. I created a pom.xml and with that I can generate the jar file which I installed to the local repository. I added it as dependency to the other project but I still can't access the classes. What am I missing? – Joe Nov 17 '21 at 22:13
  • I assume the relevant import statements are there in the class where you want to use stuff from your library, and that you have refreshed and updated your maven project and the dependency has been pulled from your local repo? If that's the case, since this question was about adding a non-maven dependency to a maven project and you seem to have achieved that, perhaps it would be best if you post a new question detailing what you did, showing the relevant bits of code and stating the error you're getting. – JustAnotherDeveloper Nov 17 '21 at 22:18