add jar to the maven local repository before build

2020-06-21 06:45发布

问题:

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>

回答1:

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



回答2:

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.



标签: java maven