This question already has answers here:
How to add local jar files to a Maven project?
(35 answers)
Closed 5 years ago.
I feel like I must be lacking some very very basic Maven knowledge here. I have a (couple of) maven project(s) and a shared library. This library should be a separate maven project with its own life cycle. I'm trying to import the library into my project using:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>nl.whatever.com</groupid>
<artifactId>my_shared_library</artifactId>
<version>1.0-SNAPSHOT</groupid>
</dependency>
...
</dependencies>
</dependencyManagement>
But maven keeps looking in my repro instead of trying to find my local build. And worst of all, it keeps looking for a jar. I get:
Could not resolve dependencies for project my.project:ejb:1.0-SNAPSHOT: Could not find artifact nl.whatever.com:my_shared_library:jar:1.0-SNAPSHOT
What's my rookie mistake? Yes, I did do a clean install of my library project.
edit:
My .m2 directory has a settings file redirecting my local repro to
/ws/repro, which contains:
/ws/repro/nl/whatever/com/my_shared_library/1.0-SNAPSHOT/
my_shared_project-1.0-SNAPSHOT.jar.lastUpdate
my_shared_project-1.0-SNAPSHOT.pom
and some property files.
edit2:
I don't think it's a duplicate. I looked at the question linked before posting my question. There is no non-maven project or external jar involved here.
Your local repository is usually in .m2/repository below the user repository. If You do clean install on your library project, it should be installed into this repository (in nl/whatever/com/my_shared_library/...). Then you can use it from all other Maven projects on the same computer.
It is furthermore important that the <packaging> is correct, i.e. the packaging needs to match the artifact you want to build. If the packaging is pom then you only create a pom (like a parent pom or a bom). Leaving out the packaging tag implictely means that you use packaging jar.
You shall try updating your pom.xml with a tag named as repositories.
Maven repositories are the places that hold build artifacts and dependencies of varying types.
A sample maven remote repository tag with its values is:
<repositories>
<repository>
<id>central</id>
<name>Maven Repository</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
</repositories>
To configure multiple repositories you can follow the guide and make use of profile in settings.xml as well.
On a side note, unless following a hierarchy or making use of specific versions of a library. You should use <dependencies> instead of <dependencyManagement>, take a look at the differences between dependencymanagement and dependencies in maven.
Related
I try to add jnetpcap as a dependency to maven. I found on the internet the following that should be added to the pom file:
<dependency>
<groupId>jnetpcap</groupId>
<artifactId>jnetpcap</artifactId>
<version>1.4.r1425-1g</version>
</dependency>
I tried this with multiple version numbers, but maven can't find the version:
Dependency 'jnetpcap:jnetpcap:1.4.r1425-1g' not found (the version
is colored red).,
Also I tried to add the library via the project structure in IntelliJ. The Maven repository can find the jnetpcap library but when I try to import it i get:
No files were downloaded for jnetpcap:jnetpcap:1.4.r1425-1g.
The library can be manually imported via the jnetpcap.jar file but I need it as a maven dependency in my pom for creating a jar file of my project. Otherwise I get a jar file which can't execute since it is missing the dependency.
Does somebody know how I can include the dependency or otherwise how I can create a jar file of my project without missing this dependency?
The artifact is correct, however you are missing one little detail which is obvious, looking at the info page at mvnrepository.com:
https://mvnrepository.com/artifact/jnetpcap/jnetpcap/1.4.r1425-1g
Especially look at the table line Repositories. There you will see that this artifact is only listed in the "Clojars" repository, a non-standard repository you most likely have not added to your project.
Therefore adding the dependency is not enough, you also have to add the following section:
<repositories>
<repository>
<id>Clojars</id>
<name>Clojars</name>
<url>https://clojars.org/repo/</url>
</repository>
</repositories>
The version of the jar you are requesting is not published to the maven repository.
This would work
<dependency>
<groupId>jnetpcap</groupId>
<artifactId>jnetpcap</artifactId>
<version>1.4.r1425-1g</version>
</dependency>
This question already has answers here:
Can I use a GitHub project directly in Maven?
(3 answers)
Closed 2 years ago.
How do I add a Java library from its GitHub repo (the library uses Maven as a build system) as a dependency to my Maven project? Can I do that without downloading and compiling the library?
Now you can import a Java library from a GitHub repo using JitPack.
In your pom.xml:
Add repository:
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
Add dependency
<dependency>
<groupId>com.github.User</groupId>
<artifactId>Repo name</artifactId>
<version>Release tag</version>
</dependency>
It works because JitPack will check out the code and build it. So you'll end up downloading the jar.
If the project doesn't have a GitHub release then its possible to use a commit id as the version.
At the moment there is no way you can do this unless the maintainer of the library provided a way to do this.
So on the title page of the library the should be an instruction containing the repository address like:
<repositories>
<repository>
<id>YOUR-PROJECT-NAME-mvn-repo</id>
<url>https://raw.github.com/YOUR-USERNAME/YOUR-PROJECT-NAME/mvn-repo/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
And a dependency name:
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</dependency>
This means that all artifact of your project including your dependency will be searched in this repo.
You could also have a glance at pom.xml to check if there was an effort made to deploy artifacts to a remote repo. Typically the keywords are oss.sonatype.org or raw.github.com like in this case.
FYI, here is a way to provide a repo for your gihub artifact: Hosting a Maven repository on github.
Github now supports packages https://help.github.com/en/github/managing-packages-with-github-packages/configuring-apache-maven-for-use-with-github-packages
You can follow the steps above to deploy Jar files to github properly.
Another very nice thing about Jitpack is, it has a lookup button on the main page. And if you type the URL of your GitHub repository, it displays different commits of the source code, and you can select which commit/tag you want. The Jitpack creates pom dependencies for you.
It became dead simple.
I have a dependency
<dependency>
<groupId>de.matthiasmann</groupId>
<artifactId>twl</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/TWL.jar</systemPath>
</dependency>
Then I execute mvn assembly:assembly. All natives files and remote maven libs are added, but there is no this jar.
UPDATE
When I am trying to run app by java -jar myjar.jar. It returns an error that there is no class from the above dependency (NoClassDefFoundError : de.matthiasmann.twl.ForExample).
I want to add classes from this jar to myjar.jar (the same what maven does with remote dependencies). How I can configure maven to do that?
See Maven 2 assembly with dependencies: jar under scope "system" not included for why system dependencies are not included and how you can work around it, specifically the mvn install:install-file code is what you want.
You cannot use systemPath, unless your Java EE server/container has that jar configured.
Remember that maven is development and compile time only. Once the war file is built, maven has no effect except for having placed all the desired jars into the WEB-INF/lib folder.
When you specify system scope, it means that it is your responsibility to ensure that the jar is present when the war is deployed. You already have a framework to do that and you do not wish to encumber your build dependency with that jar, but you have to make it available thro Maven only during development.
The other similar scope is "provided". e.g., JBoss or your corporate common deployment Tomcat framework already provides many of the jars like Spring and Hibernate that are loaded by the server startup and common to all apps in the server. Therefore, you would not want maven build to include those into the war file.
The right way, Maven gurus would tell you. is to have your own maven server and build whatever artefacts you need into that server. However, occasionally that is not possible.
Therefore, on such occasions, I create project level repository that is distributed with the project and checked into version control. I run the command mvn install to create a project level directory called, say, "project-repo".
http://maven.apache.org/plugins/maven-install-plugin/examples/specific-local-repo.html (Due to familiarity, most of the time, I build the repo by hand rather than run mvn install).
Then in the POM, I specify file://${project.basedir}/project-repo as one of the repositories. The caveat with this is that in Windows, the slashes other than the pair after "file://" has to be back-slashes when referring to Windows file system paths.
<repositories>
<repository>
<id>my-repo1</id>
<name>my custom repo</name>
<url>http://ho.ho.ho</url>
</repository>
<repository>
<id>project-repo</id>
<name>my project repo</name>
<url>file://${project.basedir}\project-repo</url>
</repository>
</repositories>
YOu can implement this in many ways refer the blog below
http://blog.valdaris.com/post/custom-jar/
If you have such an dependency the best solution is first to use a repository manager and simply put that dependency into the repository manager and afterwards use it as simple dependency.
I have a problem, I have a project which is based in a system that includes modules. This modules are other maven projects and are referenced from system POM. My problem is I'm sharing the system project with a workmate and we've got different modules.
So, is there a way to tell Maven that I want to include a module referenced in my POM only if this module exists? I mean, without compilation failure.
Thanks!
I would suggest to use profiles and activate them on file/exists option.
Use dependencyManagementis to pull all the dependency information into a common POM file, simplifying the references in the child POM file.
There are several solutions, depending on what you can do/want to achieve.
One approach is to install a Maven repository server at your company (in your local LAN or in a LAN that you and your colleague share). Build the system and then deploy the modules to the server.
In your build, you can add this new server like this (documentation):
<project>
...
<repositories>
<repository>
<id>my-internal-site</id>
<url>http://myserver/repo</url>
</repository>
</repositories>
...
</project>
Or you can both copy the base system and build it locally with mvn install. That will copy all artifacts into your local cache.
From there, you can reference them as usual. You don't need to include all the modules; just the ones you really need. That way, you and your friend don't have to use the same dependencies.
I am interested in using a project on GitHub as a dependency in my project. The GitHub project has a pom file. Can I modify my pom file to use this project? If so, how? If not, what is my best course of action?
Try jitpack, you just need to add the dependency, jitpack will build others for you.
From home page:
jitpack
Easy to use package repository for Gradle and Maven projects
JitPack builds GitHub projects on demand and provides ready-to-use packages
HOW
Add repository first
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
Add dependency
<dependency>
<groupId>com.github.User</groupId>
<artifactId>Repo name</artifactId>
<version>Release tag</version>
</dependency>
TIPS:
You can see its build log too https://jitpack.io/com/github/NanoHttpd/nanohttpd/Release-2.1.0/build.log
Not in the way I think you mean, AFAIK.
You can use github as a Maven repository--this is not the same thing as directly referencing a project, and that it has a pom file means only that it's a Maven project.
If the project is not available in the central, or other, repository, your best bet may be to clone it, build it, and install it locally. You should confirm that it's truly not available elsewhere.
#wener's answer is very helpful, but leaves some mystery.
This real example might save some time:
<project ... >
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<!-- groupId is https://github.com/fabric8io/kubernetes-client -->
<groupId>com.github.fabric8io.kubernetes-client</groupId>
<!-- module is a directory within the repo, containing pom.xml -->
<artifactId>kubernetes-model-generator-client</artifactId>
</dependency>
</dependencies>
. . .
Make sure you are signed into GitHub.
You can also find a tag index page by cutting at the tag within the URL, like https://jitpack.io/com/github/fabric8io/kubernetes-client/. In my example, I figured out if "v" from "v6.4.1" had to be removed or not, since there is a release with the v and a tag without it.
More details: jitpack.io page