4

i have third part jar file which doesn't exist remotely the file located inside the project directory , i want to add this jar into the local repository when i execute mvn install, my current code for doing that

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>myJar1.0</groupId>
                <artifactId>myJar1.0</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
                <file>myJar1.0.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>
deHaar
  • 11,298
  • 10
  • 32
  • 38
Melad Basilius
  • 2,691
  • 5
  • 29
  • 55
  • maybe better to take a different approach, see http://stackoverflow.com/questions/2229757/maven-add-a-dependency-to-a-jar-by-relative-path/2230464#2230464 – Redlab Jan 26 '17 at 15:51
  • Do you got some error? Please report the command line too. – Mario Santini Jan 26 '17 at 15:56
  • Best is to start using a repository manager and install that JAR there and use afterwards as a usual dependency..That's it... – khmarbaise Jan 26 '17 at 15:56
  • @Redlab this solution will require every developer to run 'install-file' command at their machines, what i want is to fix this problem entirely without any intervention from any developer – Melad Basilius Jan 26 '17 at 15:58
  • @Mario Santini, no errors – Melad Basilius Jan 26 '17 at 15:59
  • @khmarbaise what i want is to fix this problem entirely without any intervention from any developer, what you suggested will requires every developer to add the jar into the local repo – Melad Basilius Jan 26 '17 at 16:01
  • Perhaps clarify your real question in the question also. It is not fully clear what you want – Redlab Jan 26 '17 at 16:10
  • If you do not want any intervention you can do it like this http://stackoverflow.com/a/2229775/299787 – Redlab Jan 26 '17 at 16:11
  • @MeladEzzat You missed my point. Using a repository manager will solve that for all developers and needed only a single task to solve the problem for all of them... – khmarbaise Jan 27 '17 at 09:53
  • @ khmarbaise ok i missed your point because i don't know how to use repository manager for now, but i will do more studies about it ,Thanks – Melad Basilius Jan 27 '17 at 11:06

2 Answers2

6

Actually its not so complicated.

There are 2 cases that you need to issue Maven’s command to include a jar into the Maven local repository manually.

  • The jar you want to use doesn’t exist in the Maven center repository.
  • You created a custom jar, and need to use for another Maven project.

Steps:

1. mvn install:

put your jar somewhere, lets assume its under c:\:

 mvn install:install-file -Dfile=c:\myJar{version}.jar 
 -DgroupId=YOUR_GROUP -DartifactId=myJar -Dversion={version} -Dpackaging=jar

Now, the "myJar" jar is copied to your Maven local repository.

2. pom.xml:

After installed, just declares the myJar coordinate in pom.xml.

<dependency>
     <groupId>YOUR_GROUP</groupId>
     <artifactId>myJar</artifactId>
     <version>{version}</version>
</dependency>

3. Done

Build it, now the "myJar" jar is able to retrieve from your Maven local repository.


NOTE: this example was based on the another example which I encourage you to read for further information.

Gal Dreiman
  • 3,749
  • 2
  • 18
  • 34
5

Using a repository manager is the best solution. However, here's a solution that can be setup entirely through pom.xml configuration (without developer interventions):

Add a local repository pointing to your jar location:

<repositories>
  <repository>
    <id>project-local-repo</id>
    <url>file://${project.basedir}/src/lib/</url>
  </repository>
</repositories>

Move/rename your library to ${project.basedir}/src/lib/some-group-name/myJar-1.0.jar

And then you can include the dependency:

<dependency>
    <groupId>some-group-name</groupId>
    <artifactId>myJar</artifactId>
    <version>1.0</version>
</dependency>

The dependency will be picked up at every build. I wrote a similar answer here: running Spring Boot for REST

Community
  • 1
  • 1
alexbt
  • 14,285
  • 6
  • 67
  • 82