13

Is it possible to define a dependency in the pom such that it has the scope of system but points to multiple jars?

I'm sure this is quite unorthodox, however, I was just wondering whether this was even possible. So something like:

<dependency>
  <groupId>foo</groupId>
  <artifactId>foo</artifactId>
  <version>1.0</version>
  <scope>system</scope>
  <systemPath>${basedir}/lib/foo/*.jar</systemPath>
</dependency>
Pascal Thivent
  • 535,937
  • 127
  • 1,027
  • 1,106
digiarnie
  • 20,378
  • 29
  • 75
  • 124

4 Answers4

14

As far as I understand, you are looking for a simple way to manage dependencies to local jar files (located in '${basedir}/lib/foo/' folder in your case). Using addjars-maven-plugin that's simple. Just add the following declaration to your pom:

<plugin>
  <groupId>com.googlecode.addjars-maven-plugin</groupId>
  <artifactId>addjars-maven-plugin</artifactId>
  <version>1.0.2</version>
  <executions>
    <execution>
        <goals>
            <goal>add-jars</goal>
        </goals>
        <configuration>
            <resources>
                <resource>
                    <directory>${basedir}/lib/foo</directory>
                </resource>
            </resources>
        </configuration>
    </execution>
  </executions>
</plugin>
James McMahon
  • 45,276
  • 62
  • 194
  • 274
user1195526
  • 502
  • 7
  • 8
8

First (and I'll never repeat it enough), using system scoped dependencies is discouraged unless you know exactly what you're doing. From Dependency Scopes:

system: This dependency is required in some phase of your project's lifecycle, but is system-specific. Use of this scope is discouraged: This is considered an "advanced" kind of feature and should only be used when you truly understand all the ramifications of its use, which can be extremely hard if not actually impossible to quantify. This scope by definition renders your build non-portable. It may be necessary in certain edge cases. The system scope includes the <systemPath> element which points to the physical location of this dependency on the local machine. It is thus used to refer to some artifact expected to be present on the given local machine an not in a repository; and whose path may vary machine-to-machine. The systemPath element can refer to environment variables in its path: ${JAVA_HOME} for instance.

Now, to strictly answer your question, declaring a dependency with a system scope that would point on several jars is "possible" IF the dependency has a MANIFEST.MF listing other JARs relatively in its Class-Path entry. Something like this (assuming the "root" dependency is in lib):

Class-Path: ../lib/bar.jar ../lib/foo.jar

But I do NOT recommend this approach, especially in your particular case. Instead, have a look at this previous answer where I describe how to setup a file-based repository.

Community
  • 1
  • 1
Pascal Thivent
  • 535,937
  • 127
  • 1,027
  • 1,106
3

I've never done this but according to maven's core concepts, I think it may not be possible because every artifact is represented by a single entity (a jar, zip, tar, etc.). Hence it may not be possible to have multiple jars representing a single artifact.

Morever system scope dependencies are assumed always available and not looked up in the repo. These should be only limited to jvm or jdk related dependencies (which are now provided by the jdk but earlier were available as separate downloads)

naikus
  • 23,523
  • 4
  • 39
  • 43
0

I have a similar problem; the proprietary software I am using does not have a public Maven repository, but is packaged with close to 200 jars. I have approached the problem as follows:

cd /folder
ls *.jar > folder.txt

Then replace each line of folder.txt

^(.+)$

with

<dependency><groupId>folder</groupId><artifactId>$1</artifactId><version>1.0</version><scope>system</scope><systemPath>${folder.root}/folder/$1</systemPath></dependency>

Copy the results to your pom.xml.

Smart Manoj
  • 3,837
  • 2
  • 24
  • 45
aleksander_si
  • 451
  • 5
  • 15