Managing DLL dependencies with Maven - java

I have a Java program with Maven managing its dependencies. One of those dependency is a JNI wrapper for another program. Maven takes care of the reference to the relevant JAR file, but I'm left messing around with the DLL file myself.
Is there a good way of having Maven handle the DLL as well? Ideally I would like to have the DLL loaded into our local repository like the JAR file.

Did you try something like this:
<dependency>
<groupId>com.foo</groupId>
<artifactId>footron</artifactId>
<version>4.2</version>
<scope>runtime</scope>
<type>dll</type>
</dependency>
You can add them to maven's repository with something like this:
mvn install:install-file -Dfile=footron.dll -DgroupId=com.foo -DartifactId=footron -Dversion=4.2 -Dpackaging=dll -DgeneratePom=true
Haven't done this for DLLs but something like this should work.

I found another solution, which is described in the answer to this question: Using maven with DLL from third party and managing the name of the DLL
Basically, if you put the DLL into a ZIP file, and manage it as a separate dependency, and use the nativedependencies-maven-plugin, then the DLL will get unpacked with the right name.

Related

How to import Processing libraries into Java application

I am calling Processing functions from Java code.
This works fine for the standard Processing classes, but how to you import other Processing libraries; e.g. gicentre?
I've actually got it working by extracting the jar file from the processing library and then manually installing the artifact into the maven project.
Is there a proper way to do it?
Add this dependancy in your maven pom.xml file.
<!-- mvnrepository.com/artifact/org.processing/core -->
<dependency>
<groupId>org.processing</groupId>
<artifactId>core</artifactId>
<version>2.2.1</version>
</dependency>
Sandip's answer will work for the core Processing library (with the caveat that you should use the latest version, not version 2.2.1), but like you've discovered, gicentre doesn't have a maven repository.
You can download the various gicentre libraries from this page. Each of those libraries comes as a .zip file that contains a .jar file.
Now that you have the .jar file, it's just a matter of adding that .jar to your classpath. How you do that depends on how you've set up your project. The simplest way to do it is to use the command line to compile your project, and then you'd use the -cp argument. You've said you're using Maven, so Googling "maven local jar" will lead to a ton of results, including this one: How to add local jar files to a Maven project?
But note that you don't have to use Maven. You could just set the classpath yourself, either via the command line or via your IDE settings. For simple projects, this can be a better option, especially if Maven is giving you trouble.

How to properly use any Java API

I have been trying to use the vget library/api to make my own youtube video downloader. The vget library can be found here: https://github.com/axet/vget
I have downloaded the zip on github and I imported the project into eclipse. However, I am confused to how I am supposed to properly use this API. Should I make a completely new project, and import the classes that I need or do I put my own source files in the project of the api?
I have read other threads concerning this problem. However, they all mention how a api is typically packaged in a JAR file, but in my case it is just files and classes. So I am confused to how I should properly use this api.
The vget project is a maven project. You can see that because it has a pom.xml file in the root folder of the project.
To use it, you don't even need to download the source, because the compiled jar files are already stored in the central maven repository. You can find more information about this here:
http://mvnrepository.com/artifact/com.github.axet/vget/1.1.23
(in general, you can use the http://mvnrepository.com/ site to search whether your library is available on the maven central repository. If it's even a mildly popular library, then chances are that it is)
What you need to do is to make your own project a maven project.
Here's a "5 minutes" starter guide that describes how to do that.
When you've done that, you just add the dependency on vget to your pom.xml file in the <dependencies> section:
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>vget</artifactId>
<version>1.1.23</version>
</dependency>
Since you are making use of a 3rd party software, and not extending it with your own logic, the way to go is to create a new project, which references the 3rd party software.
You then construct your application and make it do whatever you need it to do. When it comes to using logic which is available within the 3rd party logic, you would then simply delegate that call to the 3rd party library.
I have seen on the link you have provided, that this is a maven project. You have to execute a maven package command, or maven install, so that the jar file will be generated.
With this jar follow the Bill's instructions, and add it as external library to your claspath.
When you do this, you will be able to invoke methods of that api.
Let us know if you need some help doing this in eclipse.
If your project is a maven project, you can solve dependencies problems just adding the dependency written on Readme file to your pom file.
The easiest and most automatic way is to use something like maven, ant, or gradle, that will automatically download and put the jars in to your classpath if they are in the central repositories. For example, in the maven configuration file(pom.xml) you can add this to the dependency list:
VGet Maven Repository
These build tools also allow you to add external jars if needed.
If
I would suggest you get familiar with Maven. At the bottom there is a Maven dependency you just have to include into your pom.xml, and then you can use the extension immediately.
Maven is a build platform which organizes your project in a technical way (convention over configuration, e.g. code is in /src/main/java, tests are in /src/test/java). The proper way is it to create a Maven project in Eclipse (you have to install the plugin and download Maven as well) and put the dependency
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>vget</artifactId>
<version>1.1.23</version>
</dependency>
into your <dependencies> inside your pom.xml. After adding it, you project recognizes the additional package automatically.
Nobody tinkers by adding libraries manually. It's actually not professional to work without a build platform like Maven or Gradle.

Best way to add local dependency to Maven project

There are a lot of questions about this, but the answers seem to contradict each other. So I wanted to ask it for my version of Maven (3.0.4).
I have a JAR file that's not part of any maven repository. It's a local dependency. I understand there are two ways to add it to my Maven project.
Add it as a dependency and specify the file path in the <systemPath> property. (No other work needed.)
Create a local repository <my-project>/repo and install the JAR in this repository. Then, add the new repository in my pom.xml file and add the new dependency.
I'm curious which way is better practice? I've heard some negative things about the <systemPath> property, although that way looks faster.
The answer is, it depends...
If you add it as a system dependency it is likely to become path dependent which makes it more difficult to share among other developers. Sure you can also distribute the 3rd party jar relative to your POM via your SCM but the intention of systemPath is more for dependencies that are provided by the JDK or the VM. From the docs about systemPath:
They are usually used to tell Maven about dependencies which are provided by the JDK or the VM. Thus, system dependencies are especially useful for resolving dependencies on artifacts which are now provided by the JDK, but where available as separate downloads earlier.
To install the jar in the local repo is also not friendly for sharing. It requires all developers to "upload" the jar to their local repo before building. You can of course add a script that handles this for you but it is always easy to forget to upload and IMO a better solution is point 3 below. The local install process is described here.
If you are working in an organization where the developers share the POM you should upload the 3rd party jar to a local repository manager. That way you can easily work with the 3rd party jar as if using any other maven enabled jar. Check this link for a comprehensive list on repository managers.
To summarize, if you are sharing the POM with others I would suggest that you upload it to a local repository manager. Otherwise the second option can work for you. As a last option, I would fall back to option number 1.
Also note that this has nothing to do with which version of Maven you are running.
You can add jar to your local maven repository. Usually it located at:
$home_directory/.m2/repository
For example you have expample.jar and you want to add it to your pom as:
<dependency>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0</version>
</dependency>
Then you must add example.jar to:
$home_directory/.m2/repository/com/example/1.0/example.jar
In my case NetBeans do it for me.
The best way I see so far is to use install:install-file goal with maven. You can use the mvn command line to execute it directly with appropriate parameters, or if you are using eclipse EE, you can do so by leveraging the embedded maven and creating a Run Configuration as follows:
Then, you include the jar as follows:
<dependency>
<groupId>mylocal.weka</groupId>
<artifactId>weka</artifactId>
<version>1.0</version>
</dependency>
Of course adjust the parameters as per your needs.
Best,
Haytham

Building a maven project with external JNI libraries

I'm developing a program on MacOSx that uses third party jar files that all use JNI to call C functions.
How can I include those on my build path and set the java.library.path to use the external dependencies properly ?
I would like to avoid having to install anything in my maven repository. This is important since I'll have to deploy my program to other linux platforms as well, which already have those third-party jars and C libraries installed somewhere...
For now what I've done is adding my jar dependencies with scope=system and systemPath pointing to the full path of my jar files, but I don't know how to set the os-specific dependencies...
Regards,
Philippe
Could you use:
<dependency>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
<systemPath>/my/path/to/c_libs_folder/myexecutable.jar</systemPath>
</dependency>

Maven: How to include jars, which are not available in reps into a J2EE project?

in my J2EE project I've a couple of dependencies, which are not available in any Maven repository, because they're proprietary libraries. These libraries need to be available at runtime, so that have to be copied to target/.../WEB-INF/lib ...
Right now, I'm listing them as system dependency in my POM, but with this method the problem is, that aren't being copied to the target build during compilation. Also this method is not very elegant.
So which is the best way to integrate them in Maven?
Note: I don't want to create my own Maven repository.
For people wanting a quick solution to this problem:
<dependency>
<groupId>LIB_NAME</groupId>
<artifactId>LIB_NAME</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/WebContent/WEB-INF/lib/YOUR_LIB.jar</systemPath>
</dependency>
just give your library a unique groupID and artifact name and point to where it is in the file system. You are good to go.
Of course this is a dirty quick fix that will ONLY work on your machine and if you don't change the path to the libs. But some times, that's all you want, to run and do a few tests.
EDIT: just re-red the question and realised the user was already using my solution as a temporary fix. I'll leave my answer as a quick help for others that come to this question. If anyone disagrees with this please leave me a comment. :)
As you've said you don't want to set up your own repository, perhaps this will help.
You can use the install-file goal of the maven-install-plugin to install a file to the local repository. If you create a script with a Maven invocation for each file and keep it alongside the jars, you (and anyone else with access) can easily install the jars (and associated pom files) to their local repository.
For example:
mvn install:install-file -Dfile=/usr/jars/foo.jar -DpomFile=/usr/jars/foo.pom
mvn install:install-file -Dfile=/usr/jars/bar.jar -DpomFile=/usr/jars/bar.pom
or just
mvn install:install-file -Dfile=ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0 -Dpackaging=jar
You can then reference the dependencies as normal in your project.
However your best bet is still to set up an internal remote repository and I'd recommend using Nexus myself. It can run on your development box if needed, and the overhead is minimal.
Create a repository folder under your project. Let's take
${project.basedir}/src/main/resources/repo
Then, install your custom jar to this repo:
mvn install:install-file -Dfile=[FILE_PATH] \
-DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] \
-Dpackaging=jar -DlocalRepositoryPath=[REPO_DIR]
Lastly, add the following repo and dependency definitions to the projects pom.xml:
<repositories>
<repository>
<id>project-repo</id>
<url>file://${project.basedir}/src/main/resources/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>[GROUP]</groupId>
<artifactId>[ARTIFACT]</artifactId>
<version>[VERS]</version>
</dependency>
</dependencies>
You need to set up a local repository that will host such libraries. There are a number of projects that do exactly that. For example Artifactory.
None of the solutions work if you are using Jenkins build!! When pom is run inside Jenkins build server.. these solutions will fail, as Jenkins run pom will try to download these files from enterprise repository.
Copy jars under src/main/resources/lib (create lib folder). These will be part of your project and go all the way to deployment server. In deployment server, make sure your startup scripts contain src/main/resources/lib/* in classpath. Viola.
you can install them in a private, local repository (e.g. .m2/repository under your home directory): more details here
If I am understanding well, if what you want to do is to export dependencies during the compilation phase so there will be no need to retrieve manually each needed libraries, you can use the mojo copy-dependencies.
Hope it can be useful in your case (examples)
#Ric Jafe's solution is what worked for me.
This is exactly what I was looking for. A way to push it through for research test code. Nothing fancy. Yeah I know that that's what they all say :) The various maven plugin solutions seem to be overkill for my purposes. I have some jars that were given to me as 3rd party libs with a pom file. I want it to compile/run quickly. This solution which I trivially adapted to python worked wonders for me. Cut and pasted into my pom. Python/Perl code for this task is in this Q&A: Can I add jars to maven 2 build classpath without installing them?
def AddJars(jarList):
s1 = ''
for elem in jarList:
s1+= """
<dependency>
<groupId>local.dummy</groupId>
<artifactId>%s</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/manual_jars/%s</systemPath>
</dependency>\n"""%(elem, elem)
return s1
Continue to use them as a system dependency and copy them over to target/.../WEB-INF/lib ... using the Maven dependency plugin:
http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html
Install alone didn't work for me.
mvn deploy:deploy-file -Durl=file:///home/me/project/lib/ \
-Dfile=target/jzmq-2.1.3-SNAPSHOT.jar -DgroupId=org.zeromq \
-DartifactId=zeromq -Dpackaging=jar -Dversion=2.1.3

Categories

Resources