How to include opencv in an intellij sub module (maven) - java

I'm using IntelliJ IDE with maven. I have a project (main module) with a parent pom, that includes 2 sub modules, each with their own pom.
<!-- main pom module part -->
<packaging>pom</packaging>
<modules>
<module>ModuleA</module>
<module>ModuleB</module>
</modules>
<!-- example for sub module pom -->
<parent>
<artifactId>main-module</artifactId>
<groupId>my.main.module</groupId>
<version>0.5.0</version>
</parent>
Image ModuleA includes the OpenCV Java wrapper and ModuleB is an executable java program (having the main class) using ModuleA.
The compiling works fine, but when I run ModuleB with having set the library path in the launcher, I'll get the following error for ModuleA:
java.lang.NoClassDefFoundError: org/opencv/core/Core
Any suggestions how to fix this?

Ok, I found a solution my self. The problem was, that the opencv java wrapper was included with a system path. Now I use the maven install plugin within the validate live cycle step instead.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>org.opencv</groupId>
<artifactId>opencv</artifactId>
<version>3.3.0</version>
<packaging>jar</packaging>
<file>${project.basedir}/../lib/opencv/opencv-330.jar</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Works fine for me, but was not the way I wanted it to be... The system-path type dependency seems to be buggy in maven.

Try to add the following dependency to your ModuleA:
<dependency>
<groupId>nu.pattern</groupId>
<artifactId>opencv</artifactId>
<version>2.4.9-7</version>
</dependency>

Related

Maven-compiler-plugin throws wanted compile errors but deny the compile progress

A few days ago i started with Maven. I have to put only a few of my dependencies in my generated jar file. This is needed because my code is only a plugin (Minecraft Plugin) executed by an api (Minecraft Server Software Spigot). Now the Problem is, that my Plugin depends on an other api (json-simple-1.1).
The last days i tried to edit the maven shade plugin to get the wished result. I failed, and now i did it in this way:
maven include the json-simple-1.1 api, i needing for my plugin
eclipse include the spigot api (Minecraft server software), which will executing my plugin
pom.xml:
<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>de.falco.essentialsXXX</groupId>
<artifactId>EssentialsXXX-bungeecord</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Basic class for every Plugin
</description>
<build>
<sourceDirectory>src</sourceDirectory>
<!-- COMPILE -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- BUILD -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-json</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
When i now execute 'mvn clean install' (in the right directory) i get many many errors. That make completely sense. Maven can not find types or classes and everything else comeing from the spigot-api.
My Problem is, that this isnt a real error because when the spigot-api execute my plugin i have the classes and types i need. Maven dont know that and dont compile my Programm :(
At this point a have no idea what to do. I read so many articles but i couldnt find a solution. Every article say ohhh an error here try to use tags and the right api values. That isnt what i need.
I need something like a "bypass" attribute for the compiler so the compiler know "yes this is an error but the coder knows what he does"
If you need something for compilation, it needs to be a Maven dependency.
So take that artifact, install it in your local repository and add it as dependency.
Then your compilation process will probably work.
Note that using a dependency does not mean that you have to include the dependency into the resulting jar.

How to add a local JNI jar in a pom file properly?

I have a SpringbootApplication that provides a REST API, let's call it fetchPrediction.
This fetchPrediction API uses some classes defined inside a JAR file, which is a JNI.
My application compiles and starts, however, if I call the fetchPrediction API it fails.
When I do jar -xvf on the created jar after mvn clean install, I do not see the classes that should be picked up by including the jar dependency.
Also, when I try to run the Jar file I see ClassNotDefinedException for the classes inside the JAR.
How can I do this properly?
Currently I am importing the JAR dependency as follows:
<dependency>
<groupId>jarcom.jarlib</groupId>
<artifactId>jarname</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/jarname.jar</systemPath>
</dependency>
In an spring boot application you usually do not have JNI parts. Furthermore the spring-boot-maven-plugin by default does not include dependencies with <scope>system</scope> that's the reason why you do not see the jar file in the resulting jar.
You have to have to configure the spring-boot-maven-plugin and set the includeSystemScope to true as described in the docs for the plugin.
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.4.RELEASE</version>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</execution>
</executions>
...
</plugin>
...
</plugins>
...
</build>
...
</project>

Specify both jar and test-jar types in Maven dependencies

I have a project called "commons" that contains common includes for both runtime and test.
In the main project I added a dependency for commons:
<dependency>
<groupId>com.alexb</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
However the test common files are not included. So I added :
<dependency>
<groupId>com.alexb</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
<type>test-jar</type>
</dependency>
However when type is test-jar, the runtime is not included.
Unfortunatelly, it seems I cannot include both:
<type>jar,test-jar</type>
What can I do to include both?
As #khmarbaise mentioned in the comments you should separate your test-jar part project.
I presume you have in the commons pom.xml something like this which generates common test-jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
The problem with this approach is that you don't get the transitive test-scoped dependencies automatically.
Check this link for more details:
https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html

Add InstallerListener to IzPack installer project with Maven

I have a working IzPack installer project set up with maven and added following to my install script install.xml to [installation][listeners]:
<listener classname="(company-name).listener.InstallerListener" stage="install"/>
Sadly, the line seems to be ignored and the debugger does not halt on set breakpoints in the InstallListener class. I have read the documentation for InstallListeners, but it is not useful as I have the build process integrated with maven; here are the relevant parts of the Project Object Model pom.xml:
<properties>
<izpack-standalone.version>4.3.1</izpack-standalone.version>
</properties>
<dependencies>
<!-- izpack -->
<dependency>
<groupId>org.codehaus.izpack</groupId>
<artifactId>izpack-standalone-compiler</artifactId>
<version>${izpack-standalone.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
<plugins>
<!-- IzPack compiler -->
<plugin>
<groupId>org.codehaus.izpack</groupId>
<artifactId>izpack-maven-plugin</artifactId>
<version>${org.codehaus.izpack.izpack-maven-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.codehaus.izpack</groupId>
<artifactId>izpack-standalone-compiler</artifactId>
<version>${izpack-standalone.version}</version>
</dependency>
</dependencies>
<configuration>
<izpackBasedir>${staging.dir}</izpackBasedir>
<customPanelDirectory>${staging.dir}</customPanelDirectory>
</configuration>
<executions>
<execution>
<id>standard-installer</id>
<phase>package</phase>
<goals>
<goal>izpack</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
What am I missing here?
Note: The compiled installer does contain the specified InstallerListener class file, so it is available at runtime.
You must place the jar file containing your panel classes into the {customPanelDirectory}/bin/panels folder where it will be picked up automatically by the izpack-maven-plugin.
In the case above this folder would resolve to {staging.dir}/bin/panels since you configured <customPanelDirectory>${staging.dir}</customPanelDirectory>.
Adding it to install.xml file will not work, since this would be resolved at install time, but not at installer build time.

Does Enunicate maven plug-in could generate the data model which in other java project?

Would you please help me to fix a problem I met that to use Enunciate maven plug-in? The problem is my domain is in other project, not in API project (not package, but java project), so when generate the documents, there is no data model, but I create a data model (#XmlRootElement) in the same project of API, it generated. So, does the plug-in could generate the data model which in other project?
Check out the FAQ. The first question links to this document which teaches you how to "import" classes into the project.
1.Export sources from your external API project
You can add this to the API project or to the parent project in case this API project it's a module
<project ...>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.add a reference to the package in your enunciate.xml file
<enunciate ...>
<api-import pattern="com.mycompany.pck1.dao.*" />
</enunciate ...>
3.create the dependency to the sources of the external project.
<project ...>
...
<dependencies>
...
<dependency>
<groupId>...</groupId>
<artifactId>domain</artifactId>
<version>...</version>
<classifier>sources</classifier>
<scope>compile</scope>
<optional>true</optional>
</dependency>
...
</dependencies>
** enunciate will try to compile your code so you need to add all the dependencies to external libraries
More Help: Multi-module projects

Categories

Resources