Pull dependencies as a jar in maven? - java

Is there a way I can pull some specific jar directly as a dependency defined in maven?
I have a maven project which has some internal and external dependencies. I am hosting this project on a maven registry so I can consume it in other projects. I am creating a jar and as well as creating "jar-with-dependencies" using the maven-assembly plugin in my project. Then I am hosting this on the maven repository where it has the pom, jar, and jar-with-dependencies all hosted.
In the other maven project, I want to pull the above maven project as a dependency. When I am trying to directly define it as a dependency as mentioned below, it is trying to pull the dependencies of the original project too in which some of the internal ones it could not find. Is there a way so that it can pull the hosted jar-with-dependencies directly or some other solution to this problem.
P.S. I do not want to host all those internal dependencies directly on the maven registry.
Update: I tried adding dependency with the help of the maven classifier to point to jar-with-dependencies. But it still does not resolve the issue
<dependencies>
<dependency>
<groupId>com.myproject.project</groupId>
<artifactId>project-java-sdk</artifactId>
<version>1.0.0-SNAPSHOT</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
</dependencies>

Related

Pom.XML Maven Build Dependency of POM Only Looking for Jar

GIVEN:
I have an in house tool built with gradle that includes a dependency that is only a POM file which in turn then includes a bunch of other dependencies. No jar for this particular dependency by itself. The tool builds.
I have a maven project with a pom.xml file that I want to include this tool in because of all the company specific methods needed for some processes. I added the dependency with the type of pom and when I build it fails.
ERROR:
[ERROR] Failed to execute goal on project <MYPROJECT>: Could not resolve dependencies for project <MYPROJECT>:jar:0.0.326: <com.pom.only.dependency>:jar:7.0 was not found in <Company Repo where this POM file exists> during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of <company repo> has elapsed or updates are forced
REQUEST:
I have tried making the tool a fat jar in the hopes it would not need this. But it does. The my project builds without this tool jar so I know it is this jar that brings in the dependency. I just have no idea how to resolve this.
ALMOST CODE EXAMPLE
Because of company specific, I can not put the exact POM code but I can put what it looks like removing company specific stuff.
<dependency>
<groupId>com.group</groupId>
<artifactId>tools-app</artifactId>
<version>1.0.11</version>
</dependency>
<dependency>
<groupId>com.group</groupId>
<artifactId>pom only dependency</artifactId>
<version>7.0</version>
<type>pom</type>
</dependency>
So tools app is the one that I am pulling it. It is the gradle build and uses the pom only dependency without any issue. If I pull this into a gradle app it works fine and builds. However, in the app that has this in the pom, if fails for above. So I add the dependency for the pom only dependency and mark it as type pom but still get the error.
So for my situation (and not the best solution), I went into the dependency that has only a pom and pulled the dependencies out of there and built. It worked. But feel there should be a way to make it work without having to do this.

How to refer to a project from innersource?

I have a java-maven project working on my STS. The thing is that I want to create my own spring-boot starter and include it as a maven dependency in the pom.xml file of my project. The thing is that this starter will be located in an innersource repository and I don't know what information I need to put in the pom.xml file to refer to that one in innersource and not to other one. Maybe is it possible to specify the URL of the repository or what?
Thanks you so much for your help!
The thing is that I want to create my own spring-boot starter and
include it as a maven dependency in the pom.xml file of my project.
By reading this I think that what you are looking for is how to create a dependency, that you can easily include it at any of your projects.
By this you can have a local maven repository or push it to a remote repository (like most of all dependencies), what is important is wherever you push it is available to be downloaded.
If you go by the local version you can use a maven/gradle plugin to package your jar and publish it. You will need to specify a couple of properties like groupId, artifactId and version
https://docs.gradle.org/current/userguide/publishing_maven.html
with the groupId, artifactId and version you will be able to include it in your project like
<dependency>
<groupId>org.myorg</groupId>
<artifactId>my-artifact</artifactId>
<version>0.0.1</version>
</dependency>
if this is from a maven local dependency don't forget to include maven local as part of your repositories.
https://www.baeldung.com/maven-local-repository
Check these articles, it will help you.
https://maven.apache.org/guides/introduction/introduction-to-repositories.html
Create local maven repository

Add jnetpcap to maven fails

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>

Add third party jars from the file system to final executable jar without adding the third party jars in local maven repo

I want dependencies that are having system scope to be part of my project final executable jar. I tried maven-assembly, maven-shade and maven-dependency plugin. But using these plugins, only those dependency of my project which were present in my local maven repository were getting added. Dependency with system scope (not present in my local maven repo) are not getting added in the final executable jar.
I tried searching over google, but most of the links are suggesting to add it local maven repo first. I have some limitations so I cannot add those dependency on local repo. I want it to picked from file system directly, and wanted it to be part of final executable jar.
<!-- Teradta jdbc dependency -->
<dependency>
<groupId>org.teradata</groupId>
<artifactId>teradata</artifactId>
<version>4.0</version>
<scope>system</scope>
<systemPath>${basedir}/../../../lib/terajdbc4.jar</systemPath>
</dependency>
Above dependency is not getting added in the final jar that maven is building.
Please suggest me the right plugin with its usage for this use case.
Any help on this would be really appreciated.

add external jar to our dependency

There is a jar file lets say "abc.jar" which maven dependency does not exist(ie created a jar by using java command of own classes). I want to add this jar as maven dependency so that at build time it will automatically copy that jar in lib folder as like other maven dependency. how i will do. please help .
Add it as a dependency with a system scope. See the docs here.
However, rather than adding it as a system dependency it might be better to mavenize the jar itself, then you can build and install it into your dependency management system.
Also, see this question: Can I add jars to maven 2 build classpath without installing them?
You can use the systemPath attribute in the dependency tag in the POM file of your project.
In your pom.xml, use the following snippet corresponding to abc.jar:
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>abc</groupId>
<artifactId>x</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>{path_to_abc.jar}</systemPath>
</dependency>
</dependencies>
The scope parameter corresponding to this artifact must be set to system, for the artifact to be picked up from the specified systemPath.
Hope this helps!
A normal maven dependency is always resolved by looking into a repository. So you must put your JAR file into a repository.
You could install your JAR into your local repository. Have a look at the install plugin. The install-file goal is your friend.
If other developers also need this JAR (because they are working with the same project), they either need to install it locally too, or - better - you deploy the JAR to a remote repository. Have a look at the deploy plugin. Here the deploy-file goal is your friend. For deploying artifacts, you need a repository manager like Nexus or Artifactory.
However, a dependency could also have the system scope (look at the other answers).

Categories