How to import Maven external jar (name without version) to local repo?

28 Views Asked by At

I am migrating a simple Eclipse (ant based project) to Maven. I have created the pom.xml file with the generic dependencies, but now I need to add to the project the external jars (this are custom jars but we don't have the source code of most of them)

I have a list of jars (dependant among them) that I want to import to my local .m2 repository. The naming convention of this is something like this

  • foobar.jar
  • foobarOne.jar (dependant of foobar.jar)
  • etc

The problem is that when a local jar file is added, Maven rename them to this format foobar-1.0.jar, adding the version number and the dependencies between local jar files no longer work.

Is there a way of avoid this behaviour on file import or anybody have any idea?

Thanks in advance!!

I developed a simple Python script to add all dependencies to the local repo (this script must be executed before starting with the development)

import os
import subprocess

def install_jars(directory):
    # Traverse the directory to find JAR files
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith(".jar"):
                file_path = os.path.join(root, file)
                # Extract filename without extension
                filename, extension = os.path.splitext(file)
                # Install the JAR file into the local Maven repository
                install_command = f"mvn install:install-file -Dfile={file_path} -DgroupId=local -DartifactId={filename} -Dversion= -Dpackaging=jar"
                subprocess.run(install_command, shell=True)

if __name__ == "__main__":
    # Specify the directory containing your JAR files
    # TODO: Pending define correct path
    jar_directory = os.path.dirname(os.path.realpath(__file__)) + "\\dependencies";
    install_jars(jar_directory)

And then I have added the references to the pom.xml.

<dependency>
      <groupId>local</groupId>
      <artifactId>foobar</artifactId>
      <version>1.0</version>
</dependency>
0

There are 0 best solutions below