How to include native library on maven's java.library.path variable - maven

I am trying to use JNotify for my application , which has the following requirements
JNotify can be tested by simply running the jar file with the followng commend:
java -Djava.library.path=. -jar jnotify-VER.jar [dir]
JNotify will then monitor the specified dir (or the current directory if dir is not specified) and print detected events. Note that java.library.path should point to the location of the native libraries that comes with jnotify (dlls, so dylibs etc).
But trying to get the same thing working with maven is not working out. I am trying to run a simple test but I get the following error
java.lang.UnsatisfiedLinkError: no jnotify in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at net.contentobjects.jnotify.linux.JNotify_linux.<clinit>(Unknown Source)
at net.contentobjects.jnotify.linux.JNotifyAdapterLinux.<init>(Unknown Source)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
which means the native files are not found on the library path.
My pom.xml looks like this -
I have added the jar and .so to our internal repository
<dependency>
<groupId>net.contentobjects</groupId>
<artifactId>jnotify</artifactId>
<version>0.93</version>
</dependency>
<dependency>
<groupId>net.contentobjects</groupId>
<artifactId>jnotify</artifactId>
<version>0.93</version>
<type>so</type>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Djava.library.path=target/lib/</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>net.contentobjects</groupId>
<artifactId>jnotify</artifactId>
<version>0.93</version>
<type>so</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
This doesn't work though, any ideas whats missing?
thanks

Does the dependency .so file get copied into the target directory in the compile phase? Is it named correctly? Likely it will be named something like jnotify-0.93.so.
If you need it named just jnotify.so, you might want to tryt to turn on the stripVersion option of the maven-dependency-plugin's copy goal.

Related

Create a independent jar with maven and without assembly plugin

I have a jar project that uses some .jar libs to run. In old days we use Eclipse to build the jar, but now my company is migrating to Maven. I didn't know how hard is to create an independent jar file that contains its dependencies (or some of them) inside itself like before xD.
I've read that there's an "assembly" maven plugin that "unpack" the dependencies and put them inside the independent jar, but my company has its own maven repository and they don't have that plugin. So, I'm using the "dependency-plugin" to achieve that.
The problem is that I can't get my independent jar to run.
I have this in my pom:
...
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>.</classpathPrefix>
<mainClass>jar.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.example.jar2</groupId>
<artifactId>jar2</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</plugin>
<plugin>
Actually, the content of the pom.xml is not important because I get what I wanted: my jar with a structure like this:
jar1.jar
|
- com/something/my-classes.class
|
- META-INF
| |
| - MANIFEST
| - maven
| |
| - A lot of maven stuffs
|
- jar2-1.0-SNAPSHOT.jar
And my MANIFEST file has these entries:
Class-Path: ./jar2-1.0-SNAPSHOT.jar
Main-Class: jar.App
I thought it was enough to have my jar dependencies copied inside my independent jar and indicate the Class-Path in my MANIFEST to tell to java "hey! here are my dependencies!", but, when I try to run the jar with:
java -jar my-jar.jar
I get:
Exception in thread "main" java.lang.NoClassDefFoundError: jar2/Operations
In the line where I'm trying to use the classes of my dependencies.
Why is that? What I need to get an independent jar with Maven (without using the assambly plugin)? I supposed I'm missing something to indicate to Java where my dependencies are, but I'm not sure... Or maybe I need an extra class that "loads" every class that I'm using?
Thanks a lot!

Simple Java Mail dependencies not in the jar file

I used simplejavamail in my maven project. The program can send out the email if I run it from Intellij IDE. But when I create a jar file, and run it from the jar file, then I got class not found for all the simplejavamail classes. And I open the jar, I find out that they are not included in the jar. But all the other dependency classes are there. Any one have meet this issue before?
parts of my pom.xml
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>6.4.3</version>
</dependency>
<build>
<finalName>my-project-name</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I'm having the same problem. It appears that the dependencies (Ex. Email, Mailer, EmailBuilder, etc) appear in the org.simplejavamail.api repository. I'll update you if I find a working solution with v6.4.3 but I have a feeling we may need to include additional dependencies.
Edit: To at least patch your problem,
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>5.5.1</version>
</dependency>
The 5.5.1 version still has the classes in the jar. You can reference this for yourself here:
https://www.javadoc.io/doc/org.simplejavamail/simple-java-mail/5.5.1/index.html
Then click on the different versions to see what classes exist.
I think something went wrong in their builds since v6.
Let me know if this helps!

Create the configuration file at maven install and without or before feature install for Karaf

In my project, in the bundle module, at resource directory, I have my sample startup.cfg file. This file contains properties I want to use at the startup. In the bundle pom.xml I have the following.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/classes/startup.cfg</file>
<type>cfg</type>
<classifier>config</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And in my feature xml, I have,
<feature name="my-sample-feature" description="Sample service" version='${project.version}'>
<bundle>mvn:org.sample.app/service.implementation/{{VERSION}}</bundle>
<configfile finalname="/etc/org.sample.app.startup.cfg">mvn:mvn:org.sample.app/service.implementation/{{VERSION}}/cfg/config</configfile>
</feature>
In feature xml, I have added,
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>service.implementation</artifactId>
<version>${project.version}</version>
<classifier>config</classifier>
<type>cfg</type>
</dependency>
This feature is a startup feature too. Now, when I install the feature in a karaf container or in this case when I spin up the karaf, only then the etc/org.sample.app.startup.cfg is created. My target is simple, to have this conf file always at etc/ independent of the feature is installed or not.
The cfg file are deployed into etc folder when installing the feature . If you want it to be present independent of the feature is installed or not, this means that you have to deploy it manually to etc and this is more simple after attaching the cfg to your artifact as you did in your pom.
Remember that if you want to follow best pratices (especially in a production environnement), don't separate cfg deployment from it's origin feature.

Wrapping Ant in Maven - JAVA_HOME points to the JRE but works with just Ant

I have an Ant project that builds just fine on its own. I'm now trying to wrap it in a Maven build that will kick off the Ant build using maven-antrun-plugin. When I do this the build fails and I get this error,
[ERROR] C:\Users\bobby\workspace\libraries\build-targets\common-targets.xml:170: Unable to find a javac compiler;
[ERROR] com.sun.tools.javac.Main is not on the classpath.
[ERROR] Perhaps JAVA_HOME does not point to the JDK.
[ERROR] It is currently set to "C:\Java\jdk1.8.0_65\jre"
There are a lot of SOF posts on this error but I feel like mine is unique since it only happens when I'm wrapping the Ant build in Maven i.e., I do not get this error on the same project when I just say $ ant build.
This is part of my pom.xml file
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<tasks>
<ant antfile="build.xml" target="build" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-jar</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>build/bin/myWarFile.war</file>
<type>war</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
My JAVA_HOME Environment Variable is set to C:\Java\jdk1.8.0_65.
The file that is mentioned in the error is from a library my work maintains where we keep all of our Jars. In that file here is what's on line 170
<target name="compile-src">
<!-- Compile source -->
<javac srcdir="${java.src.dir}"
destdir="${class.dir}"
debug="${debug.flag}"
deprecation="${deprecation.flag}"
nowarn="${warnings.flag}"
optimize="off"
source="${source.value}">
<classpath refid="compile.classpath"/>
</javac>
</target>
The line with source= is line 170.
It's a common issue. Try with this configuration:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
...
<!-- Add this dependency to your ant-run configuration -->
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
...
</plugin>
Maven uses Java's system property java.home, which is not the same as the environment variable JAVA_HOME, but it is using it to compute its java.home by tacking on the jre sub-directory, as witnessed. Consequently, stuff needed by Ant is simply not available in the jre directory.
However, this configuration ensures that Ant's plugin dependencies are correctly satisfied.
You need to point to JDK not JRE. Just remove ire and try.
It is currently set to "C:\Java\jdk1.8.0_65\jre"
And if your JDK is set - another workaround - Can you copy tools.jar from jdk lib to jre lib and see if it works.

XML maven artifact not on classpath

I have some external configuration (XML files) that are installed in Maven. I need to have them on my test classpath but they aren't appearing.
They must stay as XML, I cannot package them inside a Jar - but I am willing to try anything else for this, custom plugin etc.
(Please don't inform me that Maven is only for Jars - that's simply not true (and if you provide a reference refuting that I can assure you it's out-of-date/misinformation).
The dependencies are specified thus:
<dependency>
<groupId>some.group</groupId>
<artifactId>some.artifact</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>xml</type>
<classifier>some.classifier</classifier>
</dependency>
These XML artifacts have been created by the build-helper plugin (so there's no 1-2-1 with their project's POM).
My only current hacky solution is to, check for the M2_HOME property and load the files from there (as they're defined as dependencies Maven does pull them down) - but I'm not happy with this.
EDIT: The next best hack is probably to use the maven-dependency-plugin to copy these to the output directory (target/classes). If my config is fine for Jars then this smells like a Maven bug.
EDIT 2: #khmarbaise asked for the build-helper-plugin config:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>target/classes/ddl-seed.xml</file>
<type>xml</type>
<classifier>ddl-seed</classifier>
</artifact>
<!-- ... more definitions -->
This generates the correct maven-metadata-local.xml data for all the XML artifacts.
Unfortunately I can find no way of forcing maven to add the test dependency specified to the test classpath, other than this stinky hack of copying it to a directory on the test classpath.
This seems the quickest way (it's for a test dependency), avoiding any JAR creation.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-test-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.acme.gid</groupId>
<artifactId>com.acme.aid</artifactId>
<version>${project.version}</version>
<classifier>ddl</classifier>
<type>xml</type>
<outputDirectory>${project.build.directory}/test-classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

Resources