how to use cplex.jar in a maven eclipse project? - java

I want to use cplex through a maven project. when I use cplex in a non-maven eclipse project, it runs correctly. in non-maven project I had to add the path to native library location of cplex in java build path.
but in the maven project, I get errors like "java.lang.NoClassDefFoundError: ilog/concert/IloNumExpr" at runtime.
I add dependencies to my cplex.jar in pom :
<dependency>
<groupId>cplex</groupId>
<artifactId>cplex</artifactId>
<version>2.0.1</version>
<scope>system</scope>
<systemPath>${basedir}\IBM.ILOG.CPLEX.Optimizer\cplex\lib\cplex.jar</systemPath>
</dependency>
and I test to add native libraries to src/main/resources, and adding resource tab in pom: it doesn't work. I add the native library to class path environment variable: it doesn't work. I copy them in c:/windows/system32: it doesn't work!
can any body help me? what is the solution? :-(

Just go through
How do I add a jar file to my local Maven repository using Eclipse m2e on Luna Service (4.4.1)?
basically if its a jar not available in maven repository, you can still add it to your local repository and with the required group id, artifact id, and other parameters and use the scope as system while referring to it from you pom file.

Related

Maven project architecture

I am trying to add the locally created project which contains JARs added using java build path to my maven project as dependency and the JARs used in the dependency project are also be useful in the main project. how to deal in this scenario if I don't want to convert my dependency project to maven?
You have different possibilities:
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>2.53.1</version>
<scope>system</scope>
<systemPath>
C:/.../your-jar.jar
</systemPath>
</dependency>
or you can install it in your local .m2 repo
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
This doesn't force you in any way to convert your project into a maven project, but let you reuse the jar you already have.
This is similar like Maven multimodule project. In that all the submodules gets added to the root project as a jar after successful compile. In your case try covert the project into jar and copy the same jar file to the lib folder of parent project.

What is the equivalent of adding a jar to deployment assembly in maven

I am working with a maven project in eclipse which I have been testing using the built-in "run on server" tomcat option. When I try to do this, I get errors stating certain dependencies are unmet from an external jar I reference in the POM. However I have found if I add the jar via the DeploymentAssembly Tab I can run in eclipse without issues:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.company.user.server.UserDetailsAuthoritiesMapper#0'
The issue comes when I try to deploy on an actual tomcat instance. The Maven build runs fine, but when I start the servlet I get the same unmet dependency errors. This to me is indicating that the external jar is not being properly packaged into the war. What is the maven equivalent of adding the package via the DeploymentAssembly tab in eclipse? The entry in the POM.xml:
<dependency>
<groupId>com.company.webapp</groupId>
<artifactId>webapp-user</artifactId>
<version>106</version>
</dependency>
Thanks
You need to locate the maven info for the external jar. If you google the name of the jar and maven you often find a direct link to the block you need. For example if I want version 1.58 of the Bouncy castle jar Google "Maven BouncyCastle" you can find the artifact info. Add that info to your pom.xml as a new dependency in your block.
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.58</version>
</dependency>
After you rebuild, refresh maven in your IDE 1st after doing a clean, this will tell maven to pull down the jar and added it to your build.
You can also go to the repo directly and search:
Maven Repo: https://mvnrepository.com/

How to organize local dependencies with Maven?

in my company, I have a Java project:
common: This contains classes that are used for several projects
GUI: This project contains a GUI for common
Without Maven, I add common to the class path, I can build the project. But how to setup the POM.xml and the Eclipse workspace when I want to build the GUI with Maven?
I also want to package the app into a JAR later, so I tried to setup the gui as .jar package
But then how to assign a Maven type to the common project?
Ideally, I could import "common" to the Maven project?
UPDATE:
Ok, it seems the mvn package command is able to resolve the "common" project when I add as local dependency see this under GUI. Still a bit confused about whether to use "pom", "maven-plugin" or "module" - anyone can add some links/clarifications, when to use what approach?
Declare common as usual maven dependency in GUI.
If it isn't maven project, add it to local repository as shown there How to add local jar files in maven project?
I would follow this steps
Create a local maven repository to store your custom jars. Which nothing but a proper directory structure with pom files in it.
Create your sub projects(common, gui) and package them as jar.
Install the local jars to local mvn repository.
Use the jars as dependency from your project.
Example:
Create a master project on ${master_project} location and your subprojects on ${master_project}/${common} and ${master_project}/${gui} location.
Create mvn repository in: ${master_project}/local-maven-repo
Specify your local repository location in your subprojects pom.xml located in ${master_project}/${gui}/pom.xml
<repository>
<id>local-maven-repo</id>
<url>file:///${project.parent.basedir}/local-maven-repo</url>
</repository>
Install your jars in local mvn repository
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where: <path-to-file> the path to the file to load
<group-id> the group that the file should be registered under
<artifact-id> the artifact name for the file
<version> the version of the file
<packaging> the packaging of the file e.g. jar
Finally use them as regular dependency
<dependency>
<groupId>org.company</groupId>
<artifactId>common</artifactId>
<version>0.9.0-SNAPSHOT</version>
</dependency>
Reference for this answer.
Here's further resources for Maven multiple module projects
install the maven plugin in your eclipse using the following link.
This will enable maven in your eclipse. Then right click on your project and click Configure and you will see Convert to Maven Project option. This will convert your existing project to a maven project. You can configure your project to be build as a .jar, .war etc. while converting from the wizard. The following is the tutorial on how to do it... link

How to add dependencies for maxmind geoip2 eclipse

I'm trying to use MaxMind's GeoIP2 database, I've added the jar to my Java build path and configured it with Javadoc and source but when I run the program I get a NoClassDefFound error, which according to this stack overflow answer is because I need to add the dependencies, but I have no clue how to add them in eclipse, I tried extracting the jar files and adding them to the build path but that didn't work, how do I do this in eclipse?
Thanks for any help.
In eclipse you have to create a new maven java project. If you dont know maven you can get informations here:
apache maven startsite
how to install maven
Afer that you are able to configure your dependencies in your pom.xml file. Which is the configuration file for maven.
You can find some dependencies for your usage here:
maxmind documentation
The include of the dependency would look like this:
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>v2.3.0</version>
</dependency>
But first, you should look at maven installation and using. It will make your life easier by handling all the necessary dependencies you will need in this project and in all of your other projects in future.
You don't need to use Maven
For Eclipse, you may use Rightclick on Project > Properties > Java Build Path > Add External JARs. Select the JAR file you downloaded.

Intellij not importing updated jar

I have a maven project and I have created a new external jar called myjar. I subsequently updated my pom to myjar.17.1.snaphot from myjar.17.snaphot. In my external libraries, I do not see this jar - I still see myjar.17.snaphot. Therefore, Intellij cannot compile my code.
Whe I do a maven install at the command line, all works fine. Why doesn't intelliJ update my jar?
<dependency>
<groupId>com.me</groupId>
<artifactId>myjar</artifactId>
<version>17.1-SNAPSHOT</version>
</dependency
IntelliJ can not be aware of a change to an external dependency. When you change your version in an external dependency, there is no JAR installed into your local repo ($HOME/.m2). Once you do mvn install there is a file in this dir, IntelliJ notices it and adds it as a dependency.
A possible workaround is to add this external dependency to a parent pom as a module of your project.
EDIT
After installing you also need to update all references in your project poms to the new snapshot version, then right click on the pom.xml in Idea -> Maven -> Reimport.

Categories

Resources