746

Maven 2 is driving me crazy during the experimentation / quick and dirty mock-up phase of development.

I have a pom.xml file that defines the dependencies for the web application framework I want to use, and I can quickly generate starter projects from that file.

However, sometimes I want to link to a third-party library that doesn't already have a pom.xml file defined, so rather than create the pom.xml file for the third-party library by hand and install it, and add the dependency to my pom.xml file, I would just like to tell Maven: "In addition to my defined dependencies, include any JAR files that are in /lib too."

It seems like this ought to be simple, but if it is, I am missing something.

Any pointers on how to do this are greatly appreciated. Short of that, if there is a simple way to point Maven to a /lib directory and easily create a pom.xml with all the enclosed JAR files mapped to a single dependency which I could then name / install and link to in one fell swoop would also suffice.

2
  • If you're using Netbeans just follow these steps : [How do I install modules into the maven repository using Netbeans embedded Maven?][1] [1]: stackoverflow.com/a/339874/530153 Commented Dec 16, 2012 at 5:10
  • 1
    I want to point out that this link stackoverflow.com/a/339874/530153 appears to work for installing the jars one at a time. Commented Feb 12, 2014 at 15:55

25 Answers 25

641

Problems of popular approaches

Most of the answers you'll find around the internet will suggest you to either install the dependency to your local repository or specify a "system" scope in the pom and distribute the dependency with the source of your project. But both of these solutions are actually flawed.

Why you shouldn't apply the "Install to Local Repository" approach

When you install a dependency to your local repository it remains there. Your distribution artifact will do fine as long as it has access to this repository. The problem is in most cases this repository will reside on your local machine, so there'll be no way to resolve this dependency on any other machine. Clearly making your artifact depend on a specific machine is not a way to handle things. Otherwise this dependency will have to be locally installed on every machine working with that project which is not any better.

Why you shouldn't apply the "System Scope" approach

The JAR files you depend on with the "System Scope" approach neither get installed to any repository or attached to your target packages. That's why your distribution package won't have a way to resolve that dependency when used. That I believe was the reason why the use of system scope even got deprecated. Anyway you don't want to rely on a deprecated feature.

The static in-project repository solution

After putting this in your POM file:

<repository>
    <id>repo</id>
    <releases>
        <enabled>true</enabled>
        <checksumPolicy>ignore</checksumPolicy>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <url>file://${project.basedir}/repo</url>
</repository>

for each artifact with a group id of form x.y.z Maven will include the following location inside your project directory in its search for artifacts:

repo/
| - x/
|   | - y/
|   |   | - z/
|   |   |   | - ${artifactId}/
|   |   |   |   | - ${version}/
|   |   |   |   |   | - ${artifactId}-${version}.jar

To elaborate more on this, you can read this blog post.

Use Maven to install to project repository

Instead of creating this structure by hand, I recommend to use a Maven plugin to install your JAR files as artifacts. So, to install an artifact to an in-project repository under repo folder execute:

mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]

If you'll choose this approach, you'll be able to simplify the repository declaration in the POM file to:

<repository>
    <id>repo</id>
    <url>file://${project.basedir}/repo</url>
</repository>

A helper script

Since executing installation command for each library is kind of annoying and definitely error-prone, I've created a utility script which automatically installs all the JAR files from a lib folder to a project repository, while automatically resolving all metadata (groupId, artifactId, etc.) from names of files. The script also prints out the dependencies XML file for you to copy-paste in your POM file.

Include the dependencies in your target package

When you'll have your in-project repository created, you'll have solved a problem of distributing the dependencies of the project with its source, but since then your project's target artifact will depend on non-published JAR files, so when you'll install it to a repository, it will have unresolvable dependencies.

To beat this problem, I suggest to include these dependencies in your target package. This you can do with either the Assembly Plugin or better with the OneJar Plugin. The official documentation on OneJar is easy to grasp.

Sign up to request clarification or add additional context in comments.

23 Comments

I always assumed you could create a repository in the project, finally confirmed it, great!
Two things to note: 1) I recommend using "${project.baseUri}repo" instead of "file://${project.basedir}/repo" to get an RFC-compliant url also on Windows. 2) If you structure your project into submodules, this approach seems to fail because ${project.baseUri} gets resolved to the module's subdirectory. Any idea how to resolve this problem?
This nearly got me there - but Nikita's script tried to be too clever with the badly named JAR files I had. So I made a simplified version that does not do any guessing for the groupId: github.com/carchrae/install-to-project-repo
such a brilliant answer!! There are 2 ways of doing something, the right way and the way that works, you sir does it the right way!
here you also find info how to automatically generate the artifact from your jar file: devcenter.heroku.com/articles/local-maven-dependencies
|
508

For throwaway code only

Set scope == system and just make up a groupId, artifactId, and version:

<dependency>
    <groupId>org.swinglabs</groupId>
    <artifactId>swingx</artifactId>
    <version>0.9.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/swingx-0.9.3.jar</systemPath>
</dependency>

Note: System dependencies are not copied into the resulting JAR/WAR file (see How to include system dependencies in a WAR file built using Maven)

21 Comments

Thanks this is really close to what I want. Any way to add them all as a single entry? Say I have /lib with 10 jars, can I add them all somehow, for instance with /some/path/*.jar for the systemPath? or I still have to treat each as a known dependency? Still, really close to what I need, thanks!
use a systemPath like this one: "<systemPath>${basedir}/lib/BrowserLauncher2-1_3.jar</systemPath>" ${basedir} is pointing to your project's root.
It is better to use the project. prefix in your path like so: <systemPath>${project.basedir}/lib/AwesomeLib.jar</systemPath>
While I understand that this is what the OP was asking for, I still want to underline that using a system scope is an horrible practice that is strongly discouraged. See Dependency+Scopes.
@marioosh remember the original intent of the question was for quick experimentation. If you want to do a mvn package, install the jar into the repo.
|
73

You may create a local repository in your project.

For example, if you have libs folder in the project structure

  • In the libs folder, you should create a directory structure like: /groupId/artifactId/version/artifactId-version.jar

  • In your pom.xml file, you should register a repository

     <repository>
         <id>ProjectRepo</id>
         <name>ProjectRepo</name>
         <url>file://${project.basedir}/libs</url>
     </repository>
    
  • and add a dependency as usual

     <dependency>
         <groupId>groupId</groupId>
         <artifactId>artifactId</artifactId>
         <version>version</version>
     </dependency>
    

That is all.

For detailed information: How to add external libraries in Maven (archived)

4 Comments

You answer is almost correct. The groupId should be splitted in serveral subdirectories.
Of course if you have complex groupId like 'com.foo.bar' your directory structure should be /com/foo/bar/artifactId/version/artifactId-verion.jar
Is this significantly different from the answer that is a year earlier?
In the last directory, where the jar file is located, you also need to add the related pom xml file.
31

Note: When using the System scope (as mentioned on this page), Maven needs absolute paths.

If your JAR files are under your project's root, you'll want to prefix your systemPath values with ${basedir}.

Comments

15

This is what I have done, it also works around the package issue and it works with checked out code.

I created a new folder in the project in my case I used repo, but feel free to use src/repo

In my POM I had a dependency that is not in any public maven repositories

<dependency>
    <groupId>com.dovetail</groupId>
    <artifactId>zoslog4j</artifactId>
    <version>1.0.1</version>
    <scope>runtime</scope>
</dependency>

I then created the following directories repo/com/dovetail/zoslog4j/1.0.1 and copied the JAR file into that folder.

I created the following POM file to represent the downloaded file (this step is optional, but it removes a WARNING) and helps the next guy figure out where I got the file to begin with.

<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dovetail</groupId>
    <artifactId>zoslog4j</artifactId>
    <packaging>jar</packaging>
    <version>1.0.1</version>
    <name>z/OS Log4J Appenders</name>
    <url>http://dovetail.com/downloads/misc/index.html</url>
    <description>Apache Log4j Appender for z/OS Logstreams, files, etc.</description>
</project>

Two optional files I create are the SHA1 checksums for the POM and the JAR to remove the missing checksum warnings.

shasum -b < repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.jar \
          > repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.jar.sha1

shasum -b < repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.pom \
          > repo/com/dovetail/zoslog4j/1.0.1/zoslog4j-1.0.1.pom.sha1

Finally I add the following fragment to my pom.xml that allows me to refer to the local repository

<repositories>
    <repository>
        <id>project</id>
        <url>file:///${basedir}/repo</url>
    </repository>
</repositories>

4 Comments

Hi, did you put the pom files in the local repository or next to your jar files?
In the above solution it was next to the JAR files. Mind you I don't like the solution above because it is too much work.
I still prefer the solution I posted here stackoverflow.com/questions/2229757/…
I like this approach, although I used the maven install plugin to automate installing the jar into the local repo.
15

This is how we add or install a local JAR file:

<dependency>
    <groupId>org.example</groupId>
    <artifactId>iamajar</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/iamajar.jar</systemPath>
</dependency>

I gave some default groupId and artifactId, because they are mandatory :)

Comments

14

You really ought to get a framework in place via a repository and identifying your dependencies up front. Using the system scope is a common mistake people use, because they "don't care about the dependency management."

The trouble is that doing this you end up with a perverted Maven build that will not show Maven in a normal condition. You would be better off following an approach like this.

Comments

10

The Maven install plugin has command line usage to install a JAR file into the local repository. POM is optional, but you will have to specify the GroupId, ArtifactId, Version and Packaging (all the POM stuff).

3 Comments

actually, what he ment is that you dont have to create a pom for the library you are importing into your local repository
-1, sometimes you just want to add a jar file without the trouble of installing it.
The question is about adding JAR files without installing and you give an answer with Maven Install Plugin...
9

I found another way to do this, see here from a Heroku post

To summarize (sorry about some copy & paste)

  • Create a repo directory under your root folder:
yourproject
+- pom.xml
+- src
+- repo
  • Run this to install the jar to your local repo directory
mvn deploy:deploy-file -Durl=file:///path/to/yourproject/repo/ -Dfile=mylib-1.0.jar -DgroupId=com.example -DartifactId=mylib -Dpackaging=jar -Dversion=1.0
  • Add this your pom.xml:
<repositories>
    <!--other repositories if any-->
    <repository>
        <id>project.local</id>
        <name>project</name>
        <url>file:${project.basedir}/repo</url>
    </repository>
</repositories>


<dependency>
    <groupId>com.example</groupId>
    <artifactId>mylib</artifactId>
    <version>1.0</version>  
</dependency>

Comments

9

Using <scope>system</scope> is a terrible idea for reasons explained by others. Installing the file manually to your local repository makes the build unreproducible, and using <url>file://${project.basedir}/repo</url> is not a good idea either, because (1) that may not be a well-formed file URL (e.g., if the project is checked out in a directory with unusual characters), (2) the result is unusable if this project’s POM file is used as a dependency of someone else’s project.

Assuming you are unwilling to upload the artifact to a public repository, Simeon’s suggestion of a helper module does the job. But there is an easier way now…

The recommendation

Use non-maven-jar-maven-plugin. It does exactly what you were asking for, with none of the drawbacks of the other approaches.

1 Comment

Also saw maven-external-dependency-plugin though non-maven-jar-maven-plugin seems more straightforward to use.
8

It seems simplest to me just to configure your maven-compiler-plugin to include your custom JAR files. This example will load any JAR files in a lib directory.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <includes>
            <include>lib/*.jar</include>
        </includes>
    </configuration>
</plugin>

3 Comments

If I add this maven says nothing to complile!
It's saying all classes are up to date nothing to compile because it won't look for *.java anymore. You can add them back using <include>**/*.java</include>. Yet no success for me for the jars
@Imiguelmh, any reason why this does not work for jars?
7

After having a really long discussion with CloudBees guys about properly Maven packaging of such kind of JAR files, they made an interesting good proposal for a solution:

Creation of a fake Maven project which attaches a preexisting JAR file as a primary artifact, running into a belonged POM install:install-file execution. Here is an example of such kind of POM file:

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <id>image-util-id</id>
                    <phase>install</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <file>${basedir}/file-you-want-to-include.jar</file>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <version>${project.version}</version>
                        <packaging>jar</packaging>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

But in order to implement it, the existing project structure should be changed. First, you should have in mind that for each such kind of JAR file, there should be created a different fake Maven project (module). And there should be created a parent Maven project, including all sub-modules which are: all JAR file wrappers and existing main project. The structure could be:

root project (this contains the parent POM file includes all sub-modules with module XML element) (POM packaging)

JAR 1 wrapper Maven child project (POM packaging)

JAR 2 wrapper Maven child project (POM packaging)

main existing Maven child project (WAR, JAR, EAR .... packaging)

When the parent is running via mvn:install or mvn:packaging is forced and submodules will be executed. That could be concerned as a minus here, since the project structure should be changed, but offers a nonstatic solution at the end.

2 Comments

Just an observation, but I don't think you need to create a new POM for each JAR you want to add. It should be enough to create a single POM to add all of the JARs providing you have a execution block for each jar you want to add. You just need to make sure that each block has a unique id. The result is a single Maven module that will add all the JARs into the local repo. (Just make sure that the maven coordinates don't clash with anything that might already be there or might be added later!)
Hero. This is EXACTLY what I wanted. Nice one fella. 2013 must have been a good year ;)
5

To install the third-party JAR file which is not in the Maven repository, use maven-install-plugin.

Below are the steps:

  1. Download the JAR file manually from the source (website)
  2. Create a folder and place your JAR file in it
  3. Run the below command to install the third-party JAR file in your local Maven repository

mvn install:install-file -Dfile= -DgroupId= -DartifactId= -Dversion= -Dpackaging=

Below is the e.g. one I used it for simonsite Log4j

mvn install:install-file -Dfile=/Users/athanka/git/MyProject/repo/log4j-rolling-appender.jar -DgroupId=uk.org.simonsite -DartifactId=log4j-rolling-appender -Dversion=20150607-2059 -Dpackaging=jar

  1. In the pom.xml file, include the dependency as below

      <dependency>
            <groupId>uk.org.simonsite</groupId>
            <artifactId>log4j-rolling-appender</artifactId>
            <version>20150607-2059</version>
      </dependency>
    
  2. Run the mvn clean install command to create your packaging

Reference: Guide to installing third-party JAR files

1 Comment

This is a borderline link-only answer. You should expand your answer to include as much information here, and use the link only for reference.
4

If you want a quick-and-dirty solution, you can do the following (though I do not recommend this for anything except test projects, Maven will complain in length that this is not proper).

Add a dependency entry for each JAR file you need, preferably with a Perl script or something similar and copy/paste that into your POM file.

#! /usr/bin/perl

foreach my $n (@ARGV) {

    $n =~ s@.*/@@;

    print "<dependency>
    <groupId>local.dummy</groupId>
    <artifactId>$n</artifactId>
    <version>0.0.1</version>
    <scope>system</scope>
    <systemPath>\${project.basedir}/lib/$n</systemPath>
</dependency>
";

1 Comment

Yes this is exactly what I was looking for. A way to push it through for research test code. Nothing fancy. Yeah I know that that's what they all say :) The various maven plugin solutions seem to be overkill for my purposes. I have some jars that were given to me as 3rd party libs with a pom file. I want it to compile/run quickly. This solution which I trivially adapted to python worked wonders for me. Cut and pasted into my pom.
4

The problem with systemPath is that the dependencies' JAR files won't get distributed along your artifacts as transitive dependencies. Try what I've posted here: Is it best to Mavenize your project JAR files or put them in WEB-INF/lib?

Then declare dependencies as usual.

And please read the footnote.

Comments

4

A quick-and-dirty batch solution (based on Alex's answer):

libs.bat

@ECHO OFF
FOR %%I IN (*.jar) DO (
echo ^<dependency^>
echo ^<groupId^>local.dummy^</groupId^>
echo ^<artifactId^>%%I^</artifactId^>
echo ^<version^>0.0.1^</version^>
echo ^<scope^>system^</scope^>
echo ^<systemPath^>${project.basedir}/lib/%%I^</systemPath^>
echo ^</dependency^>
)

Execute it like this: libs.bat > libs.txt. Then open libs.txt and copy its content as dependencies.

In my case, I only needed the libraries to compile my code, and this solution was the best for that purpose.

Comments

3

A strange solution I found:

Using Eclipse

  • create a simple (non-Maven) Java project
  • add a Main class
  • add all the JAR files to the classpath
  • export Runnable JAR (it's important, because there isn't any other way here to do it)
  • select Extract required libraries into generated JAR
  • decide the licence issues
  • tadammm...install the generated JAR file to your m2repo
  • add this single dependency to your other projects.

Comments

3

Even though it does not exactly fit to your problem, I'll drop this here. My requirements were:

  1. JAR files that can not be found in an online Maven repository should be in the SVN repository.
  2. If one developer adds another library, the other developers should not be bothered with manually installing them.
  3. The IDE (NetBeans in my case) should be able find the sources and javadocs to provide autocompletion and help.

Let's talk about (3) first: Just having the JAR files in a folder and somehow merging them into the final JAR file will not work for here, since the IDE will not understand this. This means all libraries have to be installed properly. However, I don’t want to have everyone installing it using "mvn install-file".

In my project I needed metawidget. Here we go:

  1. Create a new Maven project (name it "shared-libs" or something like that).
  2. Download metawidget and extract the ZIP file into src/main/lib.
  3. The folder doc/api contains the javadocs. Create a ZIP file of the content (doc/api/api.zip).
  4. Modify the POM file like this
  5. Build the project and the library will be installed.
  6. Add the library as a dependency to your project, or (if you added the dependency in the shared-libs project) add shared-libs as dependency to get all libraries at once.

Every time you have a new library, just add a new execution and tell everyone to build the project again (you can improve this process with project hierarchies).

Result

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>shared-libs</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>shared-libs</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.3.1</version>
                <executions>
                    <execution>
                        <id>install metawidget</id>
                        <goals><goal>install-file</goal></goals>
                        <phase>validate</phase>
                        <configuration>
                            <file>${basedir}/src/main/lib/metawidget-0.99/metawidget.jar</file>
                            <groupId>org.metawidget</groupId>
                            <artifactId>metawidget</artifactId>
                            <version>0.99</version>
                            <packaging>jar</packaging>
                            <javadoc>${basedir}/src/main/lib/metawidget-0.99/doc/api/api.zip</javadoc>
                            <!--<sources>no source available</sources>-->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- optional, see comment -->
        <dependency>
            <groupId>org.metawidget</groupId>
            <artifactId>metawidget</artifactId>
            <version>0.99</version>
        </dependency>
    </dependencies>
</project>

4 Comments

You might want to check Maven: add a dependency to a jar by relative path (which is IMHO a better alternative).
It is better if you can ensure that the local repository always has the same relative path to the project. If I have many projects (or different branches) in different locations this will not work.
My answer has a way to tell pom.xml about a jar inside your project. Why not just do that, and point it to jars in ${basedir}/lib?
@Ed Because that's absolutely not what the system scope is for, system scoped dependencies have lots of side effects. This is an horrible practice that should be totally banned.
2

For those that didn't find a good answer here, this is what we are doing to get a JAR file with all the necessary dependencies in it. This answer (Can I add JAR files to Maven 2 build classpath without installing them?) mentions to use the Maven Assembly plugin but doesn't actually give an example in the answer.

And if you don't read all the way to the end of the answer (it's pretty lengthy), you may miss it. Adding the below to your pom.xml fill will generate target/${PROJECT_NAME}-${VERSION}-jar-with-dependencies.jar

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <!-- Get all project dependencies -->
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <!-- MainClass in mainfest make a executable JAR file -->
        <archive>
          <manifest>
            <mainClass>my.package.mainclass</mainClass>
          </manifest>
        </archive>

    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
        <!-- Bind to the packaging phase -->
        <phase>package</phase>
        <goals>
            <goal>single</goal>
        </goals>
      </execution>
    </executions>
</plugin>

Comments

1

I alluded to some Python code in a comment to the answer from Alex Lehmann, so I am posting it here.

def AddJars(jarList):
  s1 = ''
  for elem in jarList:
   s1+= """
     <dependency>
        <groupId>local.dummy</groupId>
        <artifactId>%s</artifactId>
        <version>0.0.1</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/manual_jars/%s</systemPath>
     </dependency>\n"""%(elem, elem)
  return s1

Comments

0

The solution for scope='system' approach in Java:

public static void main(String[] args) {
        String filepath = "/Users/Downloads/lib/";
        try (Stream<Path> walk = Files.walk(Paths.get(filepath))) {

        List<String> result = walk.filter(Files::isRegularFile)
                .map(x -> x.toString()).collect(Collectors.toList());

                String indentation = "    ";
                for (String s : result) {
                    System.out.println(indentation + indentation + "<dependency>");
                    System.out.println(indentation + indentation + indentation + "<groupId>"
                            + s.replace(filepath, "").replace(".jar", "")
                            + "</groupId>");
                    System.out.println(indentation + indentation + indentation + "<artifactId>"
                            + s.replace(filepath, "").replace(".jar", "")
                            + "</artifactId>");
                    System.out.println(indentation + indentation + indentation + "<version>"
                            + s.replace(filepath, "").replace(".jar", "")
                            + "</version>");
                    System.out.println(indentation + indentation + indentation + "<scope>system</scope>");
                    System.out.println(indentation + indentation + indentation + "<systemPath>" + s + "</systemPath>");
                    System.out.println(indentation + indentation + "</dependency>");
                }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Comments

0

This doesn't answer how to add them to your POM file and may be a no-brainer, but would just adding the library directory to your classpath work?

I know that is what I do when I need an external JAR file that I don't want to add to my Maven repositories.

2 Comments

This is what I was doing, and it works, but it also pollutes the global class path, and I'm trying to get away from it. Thanks!
@purple Exactly how did you do that?
0

What Archimedes Trajano wrote works in our project, but we had something like this in our .m2/settings.xml file:

<mirror>
    <id>nexus</id>
    <mirrorOf>*</mirrorOf>
    <url>http://URL_to_our_repository</url>
</mirror>

And the * should be changed to central. So if his answer doesn't work for you, you should check your settings.xml file.

Comments

0

I just wanted a quick-and-dirty workaround... I couldn't run the script from Nikita Volkov: syntax error + it requires a strict format for the JAR file names.

I made this Perl script which works with whatever format for the JAR file names, and it generates the dependencies in an XML file, so it can be copy-pasted directly in a POM file.

If you want to use it, make sure you understand what the script is doing. You may need to change the lib folder and the value for the groupId or artifactId...

#!/usr/bin/perl

use strict;
use warnings;

open(my $fh, '>', 'dependencies.xml') or die "Could not open file 'dependencies.xml' $!";
foreach my $file (glob("lib/*.jar")) {
    print "$file\n";
    my $groupId = "my.mess";
    my $artifactId = "";
    my $version = "0.1-SNAPSHOT";
    if ($file =~ /\/([^\/]*?)(-([0-9v\._]*))?\.jar$/) {
        $artifactId = $1;
        if (defined($3)) {
            $version = $3;
        }
        `mvn install:install-file -Dfile=$file -DgroupId=$groupId -DartifactId=$artifactId -Dversion=$version -Dpackaging=jar`;
        print $fh "<dependency>\n\t<groupId>$groupId</groupId>\n\t<artifactId>$artifactId</artifactId>\n\t<version>$version</version>\n</dependency>\n";
        print " => $groupId:$artifactId:$version\n";
    } else {
        print "##### BEUH...\n";
    }
}
close $fh;

Comments

0

Regarding:

mvn -D"java.class.path=<path to JAR file>" clean install

The above worked for me when adding a JAR file to the classpath with Maven.

Explanation: Maven uses the variable java.class.path to manage classpath. This variable by default only has only one JAR file (classworld-plexus-[version].jar). You can choose to add this JAR file if there is some problem in your project with the above line. The plexus JAR file path would be something like:

C:\Users\UserName\apache-maven-3.9.9\boot\plexus-classworlds-2.8.0.jar

And command would be like:

mvn -D"java.class.path=C:\Users\UserName\apache-maven-3.9.9\boot\plexus-classworlds-2.8.0.jar;C:\Users\UserName\path\to\myjarfile.jar" clean install

Comments