Maven dependency gives NoSuchMethodError - java

I am working in a Maven project (Java, using pom.xml files), where I need to use the FastMath library (part of the Apache commons math3 library).
The top level library on which I am working finds FastMath without problems. However, one of the dependencies (a local .jar that I installed so that it appears in the .m2/repository) also needs FastMath, but it gives the NoSuchMethodError message.
Installed the .jar as follows:
mvn install:install-file -Dfile=/home/me/Libraries/A.jar -DgroupId=x.y -DartifactId=y -Dversion=1.0 -Dpackaging=jar
Included it in the pom.xml file as follows:
<dependency>
<groupId>x.y</groupId>
<artifactId>y</artifactId>
<version>1.0</version>
</dependency>
It compiles without problems, the error only occurs when I attempt to run/debug.
Thanks, Michiel

This type of maven repository is not available in central repository also
.to resolve that one try to add manulaly dependencies to project http://mvnrepository.com/search?q=x.y

Related

Implement project with maven in another maven project

I have created a Java project with maven that I want to implement in other maven project.
The child project is not a spring project, is a Java project created from scratch migrated to maven to manage the dependencies.
I can generate the jar with mvn install. So far so good.
Then I try to implement this jar to other project that is built with maven and spring.
For that I added that jar to my local maven repo with the following command:
mvn install:install-file -Dfile=/Users/.../target/app-0.0.1-SNAPSHOT.jar -DgroupId=com.mycompany.childapp -DartifactId=childapp -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar
Then I am able to see that in the repo folder in .m2, the jar file is correct.
And this in the parent project pom.xml:
<dependency>
<groupId>com.mycompany.childapp</groupId>
<artifactId>childapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
And also, in the plugin section:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
At least for eclipse everything is good, as I can develop and implement the classes inside the library without any issue. I can build the parent app with mvn clean package But when I run the app and try to execute the section of the code that uses the child library I get ClassNotFoundException for the classes that are in the child project.
In the parent project, in the libraries imported by maven I see that my library has a folder icon instead of the one the other have.
How could this be fixed?
mvn clean package creates jar for parent project but it does not include child jar.
You should use SpringBoot
mvn bootRun to run your project locally
mvn bootJar to build fat jar which will include all required dependencies.
To run this fat jar call java -jar parent.jar
Finally it was that the library was like... 'corrupted'.. or something like that.. It was a Java Project (created with eclipse wizard) than then I converted to maven (with eclipse wizard).
I started a new project as maven project from the beggining and everything was working at the first attempt....
So I just have to run mvn clean install to generate the jar and installed in the .m2 repo
Then in the main app pom.xml I have to declare as any other dependency:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>mylibrary</artifactId>
<version>0.0.1</version>
</dependency>
As per the comments, you want to create a project with maven which will create a jar file. That jar file will be used in another project. If my understanding is correct. You have to do the followings.
Create a simple maven project with simple type archetype.
Run the pom.xml with the command like mvn clean install or mvn clean package.
Use the jar file from the simple project upload into your nexus/artifactory repository and use the maven definition in another maven project.
If you do not have nexus or artifactory, you can use the jar file locally in another maven project.
To add a jar file locally in another project, you have to add the following dependency structure in pom.xml.
<dependency>
<groupId>your group id</groupId>
<artifactId>name of the application</artifactId>
<version>some version</version>
<scope>system</scope>
<systemPath>your location/jar-file-name.jar</systemPath>
</dependency>
Hope it answers your question.

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 add jar dependency in maven?

When I run
mvn compile
I get package com.ibm.icu.util does not exist. So I downloaded the ICU4J jar and installed it into the local repository. I confirmed it's in .m2/repository/com/ibm/icu/icu4j/3.4.4/icu4j-3.4.4.jar. Inside that jar file is the missing class file com/ibm/icu/util/Calendar.class. Then I added the following into the dependencies section of pom.xml:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>3.4.4</version>
</dependency>
But when I run mvn compile again, I get the same error. What am I doing wrong?
You should avoid adding dependencies manually.
If you don't know a groupId and artifactId of the dependency you need, search for it at http://mvnrepository.com/. Usually, groupId matches the package names in the jar file.
For your case, the dependency is already there: http://mvnrepository.com/search?q=com.ibm.icu
So, go to http://mvnrepository.com/artifact/com.ibm.icu/icu4j and get the version of the dependency you need, e.g. 55.1: http://mvnrepository.com/artifact/com.ibm.icu/icu4j/55.1
Grab maven dependency xml and put it to your pom.xml file:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>55.1</version>
</dependency>
If you didn't find your dependency try to find it in google. Sometimes the dependency may be found in some corporate public repositories, not in a central. In this case you need to add the third-party repository to repositories section of your pom.xml.
If you're unable to find your dependency in the public repository then you have three options:
A. Install jar to internal repository server (e.g. nexus)
B. Put the JAR file in your project sources and declare project maven repository :
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://${basedir}/my-repo</url>
</repository>
</repositories>
Important: You should keep the maven repository layout in your local repository.
C. [Bad Practice] Use maven install plugin to install your custom jar to local repository on your machine. But it's a badIt's not recommended.
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=path-to-your-artifact-jar -DpomFile=path-to-pom
D. [Bad Practice] Use system dependency with absolute path to the JAR file, although it's a bad practice and should be avoided.
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>X.Y.Z</version>
<scope>system</scope>
<systemPath>${user.home}/jars/my.jar</systemPath>
</dependency>
You should not be manually installing things into the maven repository directory. That directory is maintained by maven itself.
The way dependencies work is that when you run mvn compile or some other goal, it will connect to the maven central repository and download the needed dependencies and their dependencies.
If you manually install a jar file, it may not have it's dependencies. That icu artifact will likely have other things it depends on. Maven will automatically resolve these dependencies.
I would recommend using mvn clean install this will clean the target directory and rebuild everything.
If it fails to download, then you likely need to change the maven configuration. For example, if you are behind a proxy server you need to configure maven with the proxy credentials.
Also, if you manually copied anything into the .m2/repository/ directory you should delete it and let maven put it in there correctly instead.
The beauty of maven is that you don't need to worry about things like downloading jars. It just handles that for you. Let it do it's job.
If you have an internal artifactory like JFrog maybe you should check that the jar is there. Do not download manually to .m2 because it's at least strange. At most you can upload the jar in that artifactory manually.

Managing dependencies in a Maven Project

I have 2 JARs that are supposed to be imported into a maven project. I followed this tutorial (click here) and imported those JARs into my maven project. Basically, I executed this code in the terminal:
mvn install:install-file -Dfile=myfile.jar -DgroupId=mygroup -DartifactId=com.mygroup.project -Dversion=1.0 -Dpackaging=jar -DlocalRepositoryPath=lib -DcreateChecksum=true
and then I imported the library into my Maven project.
All this works fine. However, the JARs I am importing are supposed to have few dependencies themselves. As I understand, Maven handles the internal dependencies automatically. Now I have a list of dependencies (with group ID, artefact ID and version) but I don't understand where do I write those. In the folder 1.0 of the library, there is a file called myjar-1.0.pom. I tried writing the dependencies there but it was of no use.
Could you tell me a way of manually telling Maven to load up a few dependencies?
I also tried specifying these dependencies in the main pom.xml but it results in errors - saying the repo-url/dependency/file.pom was not found. So I guess it needs to be mentioned in internal dependency only - but I can't figure out a way of manually defining them. Will I need to create a pom.xml within those libraries, or is there something that I am missing out?
You can do this:
write the pom for you jar, and declare the dependencies in the pom.
user mvn install:install-file -Dfile=path-to-your-artifact-jar -DpomFile=path-to-pom to install you jar.
link: http://maven.apache.org/plugins/maven-install-plugin/examples/custom-pom-installation.html
Let's say you have the following JARs and dependencies:
myMavenProject
--> library1.jar
--> library2.jar
--> library3.jar
(I'm assuming that your library1, library2 and library3 are not Maven projects)
For each library1, library2 and library3, perform mvn install:install-file like what you've did before:
mvn install:install-file -Dfile=library1.jar -DgroupId=com.mygroup.project -DartifactId=library1 -Dversion=1.0 -Dpackaging=jar
(Note that I swap your groupId and artifactId here)
Then in your Maven project, update your pom.xml to have following:
<dependency>
<groupId>com.mygroup.project</groupId>
<artifactId>library1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.mygroup.project</groupId>
<artifactId>library2</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.mygroup.project</groupId>
<artifactId>library3</artifactId>
<version>1.0</version>
</dependency>
Unless you have a very specific requirement, I'm not sure why you want to have -DlocalRepositoryPath=lib extra options for Maven and not using your local M2 Repository.
The tutorial you are following might be working, but it is not The Maven Way! If you will follow this path, you will have troubles all along the way!
You'd better read some tutorial from the maven user center or the maven books and resources page. Maven: the definitve guide for example is available online for free.
Why the approach you are currently following is not good and how you'd do this better is mentioned by Stephen Connolly on a very good blog post of him (Stephen is an active maven developer).

How to install Google API's json-simple jar to a maven project?

I've just made a java project with maven:
mvn archetype:create -DgroupId=com.company -DartifactId=\
myproject -DarchetypeArtifactId=maven- archetype-quickstart
Then need to import a 3rd party jar, path is ~/Downloads/json-simple-1.1.1.jar, I tried the following commands:
mvn -e install:install-file -Dfile=~/Downloads/json_simple-1.1.1.jar\
-DgroupiId=org.json.simple - DartifactId=json_simple -Dversion=1.1.1 -Dpackaging=jar
but an error occoured:
[ERROR] BUILD ERROR
[INFO] Missing group, artifact, version, or packaging information
I'm using maven 2.2.1, java 1.6.0_35 and Ubuntu 11.10.
The dependency is already in maven central repository. Just the following dependency to the pom of your project and you are done:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
You don't need to install json_simple locally. It is in Maven Central. Go to http://search.maven.org and search.
groupid: com.googlecode.json-simple
artifactid: json-simple
version 1.1.1
It is better to satisfy a dependency from Central rather than relying on one you installed locally. For a start, it will make it easier for other people to build your code.
As for the build problem, I suspect that you have made a mistake in the dependencies section of your project's "POM.xml" file.
Don't remember the exact syntax but you can di the following:
Add thus as a dependency in the pom file of the project you want to build
The build will fail as this jar is not part of the repo yet
But maven will also generate the command to install the jar in repo copy paste that, change the path the jar and execute.
This should solve the purpose.
But looks like this jar is already part of some repo.gGoogle it out might just need to add a new repo

Categories

Resources