-1

I have a local jar that I need to add it as a dependency to my maven project to be included in the published jar of the project.

It is placed in the project at "my_project/lib/external1.jar" , at first I added it to dependencies as follow:

<dependency>
    <groupId>installExternalJars11</groupId>
    <artifactId>externalJar11</artifactId>
    <version>3.1.14</version>
    <systemPath>${project.basedir}//lib/external1.jar</systemPath>
    <scope>system</scope>
</dependency>

It compiled successfully, but the jar content is not included in the resulting project jar, hence, when I use the project jar I get NoClassDefFoundError for these classes, although I am using the following plugins which shall package all the dependencies in the output jar

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>

I found several posts recommending to use the maven install plugin to install it, then add it as a dependency, so I followed this approach as follow:

<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</groupId>
    <artifactId>waheed</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>waheed</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.maven-install-plugin>2.5.2</version.maven-install-plugin>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>${version.maven-install-plugin}</version>
                <configuration>
                    <repositoryLayout>default</repositoryLayout>
                    <groupId>installExternalJars</groupId>
                    <artifactId>externalJar1</artifactId>
                    <version>3.1.14</version>
                    <file>${project.basedir}/lib/external1.jar</file>
                    <packaging>jar</packaging>
                    <generatePom>true</generatePom>
                </configuration>
                <executions>
                    <execution>
                        <id>install-ibm-foundation</id>
                        <phase>validates</phase>

                        <goals>
                            <goal>install-file</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>

    <dependencies>
        <dependency>
            <groupId>installExternalJars11</groupId>
            <artifactId>externalJar11</artifactId>
            <version>3.1.14</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

In eclipse I get the dependency highlighted with following error:

Missing artifact installExternalJars11:externalJar11:jar:3.1.14

and when I run maven build (from eclipse: right click pom.xml > run as > maven build) I get the following error (apparently maven is looking for the jar in my nexus repository although it is referred as local file in the install section):

Failure to find installExternalJars11:externalJar11:jar:3.1.14 
in http://ur_to_nexus/nexus/content/repositories/central/ was cached in 
the local repository, resolution will not be reattempted until the 
update interval of ubknexus has elapsed or updates are forced

Tried the fixes proposed here https://stackoverflow.com/a/6112344/458999 but did not work

P.S : When I add the Jars to the build path in eclipse (Java build Path > Libraries > Add Jars) and run the project from eclipse (or export from eclipse and run the exported jar) it works

Ahmed Waheed
  • 1,281
  • 5
  • 21
  • 39
  • Install the jar file into your repository manager that's the solution... – khmarbaise Jun 27 '18 at 17:56
  • @khmarbaise is not that what the install plugin part should do? shall I do it in a different way? – Ahmed Waheed Jun 28 '18 at 06:50
  • Calling maven-install-plugin to install a jar is simply the wrong way...better go via Repository Manager which makes your build easier and will remove complexity in your pom file and furthermore will prevent you by committing binary artifacts in your version control system...another things is that this jar might have transitive dependencies which will never work this way... – khmarbaise Jun 28 '18 at 06:58
  • @khmarbaise Ok, I see your point, but aside from that, is not it possible to use local jars as dependencies? using the maven install plugin? I want to know why it is not working for me. – Ahmed Waheed Jun 28 '18 at 09:18
  • Possible duplicate of [Local jars are not included in class path (\`system\`)](https://stackoverflow.com/questions/3280834/local-jars-are-not-included-in-class-path-scopesystem-scope) –  Jun 28 '18 at 16:28
  • you should **never** be using `system` it is well know that they regret adding the ability to do that to being with because of all the tight coupling and pain that it eventually causes everyone. Read the documentation on `system`and you will see why it does not work, it is explained clearly in the documents or see the duplicate(s) why. –  Jun 28 '18 at 16:29

1 Answers1

0

The system scope subtly makes Maven behave different which bites you now.

Install the artifact in your local Maven repository server (Nexus or Artifactory) or identify an equivalent artifact on Maven Central. If commercial software, ask your vendor for access to their repository.

If you cannot do that you can install locally as part of your build. See how it can be done at Multiple install:install-file in a single pom.xml. This should be considered a short term solution though.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347