Yoctopuce is now available in Maven

Yoctopuce is now available in Maven

Maven is the most popular build engine for Java. We have therefore modified our Java library to make it compatible with Maven and it is now also available from the Maven Central Repository. Thanks to this, it's now possible to use the Yoctopuce library in a Maven project by adding only three fields in an xml file :-)





The greatest strength of the Java programming language is without doubt the quantity of libraries already available. Be it to parse a file, to access a database, or to draw a graph, there is probably already a library which does it. But manually managing the compilation of a project using external libraries which themselves use external libraries becomes very complex very soon. The solution is to use a build engine to automatize compilation and to manage dependencies. Maven is perfect for this because it takes care of compiling the project and it is also able to directly download the needed libraries from the web.

If you already use Maven, the remainder of this post will most likely seem trivial. You will find everything needed to use our library on Maven Central Repository. But take heart, next week we'll talk about something else.

If, on the opposite, you have never used Maven, we are going to show you how easy it is to add the Yoctopuce library to a Maven project from the three main IDE: InteliJ, Netbeans, Eclipse, as well as to a project created by hand with your pet editor.

For this post, we are going to write a short program which is simply going to display the serial numbers of the Yoctopuce modules locally connected. If you have never used the Yoctopuce library, a very similar example is detailed in la the documentation of each module. We assume that you know at least a little how to use the Yoctopuce API and that you have already installed the VirutalHub on your machine.

package com.mycompany.example;
import com.yoctopuce.YoctoAPI.*;

public class Demo {
  public static void main(String[] args)
  {
    try {
      // set up the API to use local VirtualHub
      YAPI.RegisterHub("127.0.0.1");
      System.out.println("Device list");
      YModule module = YModule.FirstModule();
      while (module != null) {
          System.out.println(module.get_serialNumber() + " ("
                           + module.get_productName() + ")");
          module = module.nextModule();
      }
    } catch (YAPI_Exception ex) {
      System.out.println(ex.getLocalizedMessage());
    }
    YAPI.FreeAPI();
  }
}




Maven without IDE

A Maven project is made of a pom.xml file (for Project Object Model) and several directories which will contain the source files.

Maven forces you to use a very specific directory structure:

  • /pom.xml: the project pom file
  • /src/main/java: contains the Java source files
  • /src/main/resources: contains the resource files (images, files, ...)
  • /src/main/webapp : is the webapp directory of the project
  • /src/test : contains the test files
  • /target : contains binaries and packages generated by Maven

The source file of our small project (Demo.java) must be saved in the /src/main/java/com/mycompany/example sub-directory. Without this, Maven won't compile it.

The pom.xml file must contain at least three fields:groupId, artifactId, and version. These three fields are needed to uniquely identify a Maven artifact.

  • groupId identifies the group that created the project in "reverse domain name"
    (for example: com.mycompany.example)
  • artifactId identifies the artefact name (for example demo)
  • version defines the version number of the artefact

For the compilation of our example to succeed, we must still add a dependency on the Yoctopuce library. We only need to add a dependency entry in the dependencies section referencing the groupId, the arterfactId and the version number of the Yoctopuce library that we want to use.

Here is the pom file that can compile our application with version 1.10.15882 of the Yoctopuce Java library.

<?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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany.example</groupId>
 <artifactId>demo</artifactId>
 <version>1.0-SNAPSHOT</version>
 <dependencies>
  <dependency>
    <groupId>com.yoctopuce.java</groupId>
    <artifactId>yoctolib</artifactId>
    <version>1.10.15882</version>
  </dependency>
 </dependencies>
</project>


To compile our application, we have to run "mvn package". If Maven doesn't already have this version of the Yoctopuce library locally, it automatically downloads it from Maven Central.

Netbeans

It's possible to create a Maven project directly from the Netbeans wizard. In the project creation wizard, you must select a project in the Maven directory. Maven creates the minimal arborescence and the pom.xml file. To add the dependency on the Yoctopuce library, you must right click on the "Dependencies" branch of the "Project" panel. You must then provide the groupId, artifactId, and version of the Yoctopuce library you want to use. Here you are, the project is ready.

The following video shows the whole process.

  
Here is how to add the Yoctopuce library to a Maven project with Netbeans.



Intellij

IntelliJ supports Maven projects natively as well. As with Netbeans, the project creation wizard creates the arborescence and the pom.xml file. However, to add the Yoctopuce library, you must edit the pom.xml file by hand and manually add the following section:

<dependencies>
  <dependency>
    <groupId>com.yoctopuce.java</groupId>
    <artifactId>yoctolib</artifactId>
    <version>1.10.15882</version>
  </dependency>
 </dependencies>


The following video shows the whole process.

  
Here is how to add the Yoctopuce library to a Maven project with IntelliJ.



Eclipse

Eclipse "for Java and Dsl Developper" already contains a plugin which adds the Maven support. However, this plugin is not included in the Eclipse standard version. In this case, you must add it by hand. The Maven project creation wizard generates an arborescence compatible with Maven and the pom.xml file, as well as the project files used by Eclipse. To add the Yoctopuce library, you must edit the pom.xml file, but Eclipse makes this task easier with a wizard which automatically generates a valid pom.xml file.

The following video shows the whole process.

  
Here is how to add the Yoctopuce library to a Maven project with Eclipse



Without Maven

Obviously, the Java library remains available on our web site and on GitHub.
These two latter versions also include the API documentation as well as numerous examples.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.