Issue
My problem is that I want to have an external lib imported into the local maven repository of the user automatically.
Here is how I get it working using maven-install-plugin :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-external</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/myjar.jar>
<repositoryLayout>default</repositoryLayout>
<groupId>mygroupid</groupId>
<artifactId>myartefactid</artifactId>
<version>myversion</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
But with this in pom, in order to have it working I have to execute in two different commands :
mvn clean
mvn install
If I am just runing
mvn clean install
It's failling not resolving my dependency during the install phase (except if I have done a mvn clean alone and I havn't clean the local repository of course). Clean seems to call the pluging only if I run 'mvn clean' alone.
What I would like is to have my dependency automatically imported when runing 'mvn install'. Or at least while runing 'mvn clean install' in a single command.
Solution
This type of solution work well in a multiple pom project:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>
[...]
<modules>
<module>dependency-installer</module>
<module>need-dependency</module>
</modules>
</project>
The module dependency-installer has its own pom and perform the installation of the dependency:
<artifactId>dependency-installer</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>lib1</id>
<configuration>
<file>${project.basedir}/libs/lib1/lib1.jar</file>
<pomFile>${project.basedir}/libs/lib1/lib1.pom</pomFile>
<repositoryLayout>default</repositoryLayout>
<groupId>mylibGroupId</groupId>
<artifactId>mylibArtifactId</artifactId>
<version>mylibVersion</version>
<packaging>jar</packaging>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
<phase>install</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then the module need-dependency just depend on the lib and the dependencies-installer:
</dependencies>
<dependency>
<groupId>lib group Id</groupId>
<artifactId>dependency-installer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>mylibGroupId</groupId>
<artifactId>mylibArtifactId</artifactId>
<version>mylibVersion</version>
</dependency>
</dependencies>
Maven will always execute build in correct order. Because you depends on dependency-installer it will be cleaned and built first and will put the dependency on the local repo. Then the maven module needing the dependency will run and will use the freshly added jar from the local repo.
This solution is much better than the command line version because it is part of a standard maven build. Doesn't require anybody to install anything manually before and work perfectly well locally or in CI/CD.
In advanced corner case you can even provide a patched version of a lib this way.
Answered By - Nicolas Bousquet
Answer Checked By - Marilyn (JavaFixing Volunteer)