Maven/Spring: How to add external jar to classpath without installing it as maven dependency? - maven

The common ways of including external non-maven jar in the classpath of your Maven project is to either use "addjar-maven-plugin" (which I have not been able to get to compile maven with) or "mvn install:install-file" and then adding a dependency section for the external JAR. This approach essentially installs client JAR in your repo and makes it available in classpath for maven. But is there a better way to do this (or) are the options mentioned above the only ones available? I just want the external JAR to be added to classpath for component scanning by Spring but do not want the JAR itself to be added to my repo as it is client's JAR? I hope this is a valid case (if not, kindly explain)
Thanks,
Paddy

You can create lib folder under your project's src folder and reference this folder as maven repository.
<repository>
<id>local</id>
<url>file://${basedir}/src/lib</url>
</repository>
Then you should add dependency to your jar in your pom.xml
<dependency>
<groupId>com.company</groupId>
<artifactId>dependency</artifactId>
<version>1.0</version>
</dependency>
After that your should rename jar file and place it on following path src/lib/com/company/dependency/1.0/dependency-1.0.jar . This path depends on how you want to reference your jar.

Related

In a Springboot Maven project, how to reference from another Jar?

I have a SpringBoot Maven project. I am dependent on another set of libraries. Currently am pointing to their repository path , downloading it to .m2 repository and using.
But the repository website is not reliable. SO I wanted to package the dependent libraries as part of JAR in the resources folder.
After putting the jars in resource folder. How can I get references of the Types/libraries ?
Currently:
<dependencies>
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-core</artifactId>
<version>5.23.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>www.dcm4che.org</id>
<name>dcm4che Repository</name>
<url>https://www.dcm4che.org/maven2</url>
</repository>
</repositories>
You can put all your libraries in a single folder either in your project or in some folder in your local. You can then add them to your maven POM.
Lets say you put all your jars in a folder called libs in your base project directory. You can then add something similar to the below in your maven POM.
<groupId>com.abc.xyz</groupId>
<artifactId>my-artifact-id</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/libs/my-jar-name.jar</systemPath>
You need to use the scope system. Excerpt from Maven website,
Scope : system - This scope is similar to provided except that you
have to provide the JAR which contains it explicitly. The artifact is
always available and is not looked up in a repository.
Maven Documentation
If you read further, it also mentions that this has been deprecated. So, its a nice quickfix or a hack but then the best thing would be to set up a repository manager as suggested by others.

Refer a jar which is inside a zip folder in Maven

I am working on maven multi module project and the maven version is 3.0.3.
The parent pom needs a particular jar which is inside a zip folder in Nexus.
So, I have included the dependency as below in the parent pom file.
<properties>
<standalonejar_version>5.5.0</standalonejar_version>
</properties>
<dependency>
<groupId>org.abc</groupId>
<artifactId>standalonejar</artifactId>
<version>${standalonejar_version}</version>
<type>zip</type>
</dependency>
Looks like it is not working and also, there are other modules in the same project which require that particular jar.
So, can you please help how to refer that particular jar which is inside the zip folder. Thanks a lot for your help.

Dependency packaged as rar but need jar

The XADisk library deployed on Maven Central packaged as 'rar' instead of 'jar'. But i just need the jar (and possibly source) for the project i'm working on. I was wondering what the best (maven style) way is to deal with this dependency.
The jar files are available on Central but not specified in the pom thus type="jar" doesnt work
the pom is here: https://repo1.maven.org/maven2/net/java/xadisk/xadisk/1.2.2/xadisk-1.2.2.pom
and the jars can be found here: https://repo1.maven.org/maven2/net/java/xadisk/xadisk/1.2.2/xadisk-1.2.2.pom
I can't reproduce your issue, maybe they changed something in the repo?
If I add the following dependency to my project:
<dependency>
<groupId>net.java.xadisk</groupId>
<artifactId>xadisk</artifactId>
<version>1.2.2</version>
</dependency>
then the JAR file gets packaged into my project.
By the way, if no "type" is specified (for the dependency), maven uses JAR as default.

Copy war dependency mentioned in maven jar module

The war dependency mentioned in jar project just to get copied from copy-dependency plugin/assembly plugin to use for some custom packaging. But plugin skip the war dependency , Is this plugin behaviour?
or need something else?
<dependency>
<groupId>com.external.modules</groupId>
<artifactId>war-jboss</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>war</type>
</dependency>
Maven assembly plugin works on include and exclude. You would not be including your war file correctly. Check if you are using the correct regular expressions to include the war file in assembly plugin.

How to add resources to the classpath of Maven plugins?

I've got a Maven plugin that depends on slf4j for logging. The default behavior is too chatty for my liking but I can't figure out how to add my logback.xml to the plugin's classpath.
<plugin>
<dependencies>
</dependencies>
</plugin>
allows you to add dependencies to the plugin's classpath, but how do you add local (resource) directories?
You have to wrap your logback.xml into a proper Maven artifact (i.e. a jar) and install it to local repository or deploy to your shared repository, or use systemPath in your dependency declaration to point to a jar placed somewhere inside of your project, which is highly not recommended.
The reason for this is reusability of your build. Think how others would be able to reproduce it.
You don't. You must package them up as an artifact and add it as a dependency.

Resources