0

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.

bloub
  • 510
  • 1
  • 4
  • 21
  • Where is the library coming from? Another Maven project? – khmarbaise Mar 17 '20 at 19:55
  • What OS do you use? if it works by running `mvn clean` and `mvn install` separately, and you just want a single command to do both, you can just create an alias for it, like: `alias mvnci='mvn clean; mvn install'` – M Imam Pratama Mar 17 '20 at 19:57
  • The library cames from an other company, it's a maven project but they are delivering the library under a .jar format only. – bloub Mar 17 '20 at 20:24
  • I am using ubuntu. But i can't make an alias like that. If I want this problem to be solve it's because I can't explain to some other people that will exploit the project that they have to do theses both step separately. – bloub Mar 17 '20 at 20:30
  • 1
    If you are working in a corporate environment it's usually the case to have a repository manager. Then you can simply put that jar file into your repository manager and reuse as a usual dependency. Anything else makes it more complex; more error prone etc. – khmarbaise Mar 18 '20 at 07:20

2 Answers2

1

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.

Nicolas Bousquet
  • 3,990
  • 1
  • 16
  • 18
0

Your solution cannot work with just one Maven command.

If you start Maven with mvn something, the dependencies are resolved first, so anything you install by a plugin during the build won't be found.

The solution, as khmarbaise already said, is to put the artifact into a repository first. The usual solution for that is your company Nexus/Artifactory (if you do serious Maven depevelopment, there should be one).

If this is really not possible, you can use a directory as Maven repository as described here: https://stackoverflow.com/a/28762617/927493

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142