Add a non-classpath dependency in Maven - java

I would like to add a dependency to my maven project so that Maven can resolve the dependency, but not add it to the classpath. E.g.
<dependency>
<groupId>com.example</groupId>
<artifactId>example-artifact</artifactId>
<type>bin</type>
<classifier>jar-with-dependencies</classifier>
<version>1.0</version>
</dependency>
In this particular case, the dependency is a FAT executable jar file, that I want to run from my Mojo. I don't want the contents of that jar file to pollute my mojo's classpath. I just want it treated as a binary file, so that I can look it up inside my mojo (via project.getArtifacts(), and then execute it as CLI.
I've tried both "zip" and "bin" packaging types, but it always seems to end up on the classpath.
I could bundle the zip inside another jar dependency and then extract/run it at runtime, but I would prefer to just figure out how to resolve it but exclude it from the classpath.
Any ideas?

Related

External jar finalname

Is it possible to change the finalname of external jar in the pom file in Maven?
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>iText</artifactId>
<version>2.1.7</version>
</dependency>
I would like to change it so only this specific jar name would be in the camel case. Instead of itext-2.1.7.jar I would like to have iText-2.1.7.jar. How Can I achieve it?
There's no direct way. So as an ugly workaround;
Add a sub module to your project and add this dependency there and package using assembly plugin. Change the sub module's artifact ID as you wish. (Basically this new module will be the wrapper for this dependency). In the runtime, the classes will be available.
You should define this module as the top element of the <modules> tag. So that it will be built first. and updated in the .m2. That also makes sure that your code will compile just fine any where without referring to the artifact from the maven central.
Again, there's no OOTB way. Hence this.

How to import a .jar in a Java's file without IDE and with Maven?

I can't compile the files directly. I use mvn package.
I can't run the files directly. I use storm (Apache).
I don't know much about Maven.
I tried to just put the .jar in the same folder as the code and use import com.path.of.jar. It did compile, but when I tried to run, gave a NoClassDefFoundError.
Try this way to add dependencies directly, like this:
<dependency>
<groupId>sample</groupId>
<artifactId>com.sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>
When you work on a maven based project, you manage dependencies through the pom.xml file at the root of the project. POM stands for Project Object Model and contains information about the project and configuration details used by Maven to build the project (Introduction to the POM).
A maven project produces an artifact uniquely identified by its coordinates: The <groupId>, <artifactId> and <version> that you normally find at the top of your pom.xml file. Once an artifact is published to a repository other maven projects can depend on it.
If you look at the content of your POM file you should see a <dependencies> element containing all dependencies that your project needs. If you want to import classes from a jar in your code you will need to find the maven coordinates of this jar (for example on search.maven.org or mvnrepository.com).
Once you have the coordinates add a corresponding dependency section. It should look like this:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
Next time you run mvn package, the jar will be downloaded, used during compilation and packaged with your artifact.
And if you would like to get a good understanding of maven the following free book is excellent: Maven: The Complete Reference

Apache Nifi failure due to java.lang.NoClassDefFoundError in a local maven dependency jar.

This is the first time I am using maven, I want to implement a processor for apache-nifi. Now for this I am using a proprietary jar file which is an SDK. It is not on the repositories. Therefore i have put it in the pom.xml as follows.
<dependency>
<groupId>KS</groupId>
<artifactId>En-SDK</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/En-SDK-Java.jar</systemPath>
</dependency>
I am using intelliJ which seem to have no problem importing this dependency. So is the maven build procedure.
I use mvn clean install as explained in this tutorial. Which gives me with a nar package as the output. (no build errors)
This nar , which is supposed to be put in the $NIFI_HOME/lib directory does not bundle the said local jar.
If i place this nar file in the required directory and start apache-nifi,
INFO [main] org.apache.nifi.nar.NarClassLoaders Loaded NAR file: /..././work/nar/extensions/myNar.nar
Seems to be loaded by the NarClassLoaders , but following that I get an exception and nifi doesn't start.
java.lang.NoClassDefFoundError: com/kls/../../SubscriptionInterface
The SubscriptionInterface is a class from the said local jar.
If I look at the nar file's folder structure, inside the META-INF/bundled-dependencies/ i see every other dependant jar files defined in the pom but not this local jar i used.
How to overcome this?
The following Stackoverflow question seems to be similar to what you have asked.
Maven 2 assembly with dependencies: jar under scope "system" not included
Try to put the dependency in a local repository and declare it in the pom.

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