Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven2: use ${basedir} in jar path

Tags:

java

maven-2

I am using an external, proprietary jar in my project. When I hard-code the path as follows in my pom.xml, it works fine:

<dependency>
  <groupId>com.foo.bar</groupId>
  <artifactId>bar</artifactId>
  <version>5.2</version>
  <scope>system</scope>
  <type>jar</type>
  <systemPath>D:\workspace\myproj\external\companyname\lib\proprietary_api.jar</systemPath>
</dependency>

However, when I try to use the ${basedir} variable, maven can't find the jar:

<dependency>
  <groupId>com.foo.bar</groupId>
  <artifactId>bar</artifactId>
  <version>5.2</version>
  <scope>system</scope>
  <type>jar</type>
  <systemPath>${basedir}\external\companyname\lib\proprietary_api.jar</systemPath>
</dependency>

The pom is located in D:\workspace\myproj

This also needs to be cross-platform compatible (dev on Windows, deploy on Linux).

Thanks!

like image 541
Clayton Avatar asked Nov 23 '10 03:11

Clayton


People also ask

What is ${ Basedir in Maven?

project. basedir : The directory that the current project resides in. This means this points to where your Maven projects resides on your system. It corresponds to the location of the pom. xml file.

Should use a variable instead of a hard coded path?

A general advantage of using path variables instead of hard-coded paths is that if the source files are moved to a different directory on the development system, you need to change only the value of the path variable. That is, you do not need to update each hard-coded path to all of your source files.

How do you add a project dependency in POM xml?

Add a Java Maven Dependency to the Utility Project field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK. Expand the utility project, right-click the pom. xml file, and select Run As>Maven Install to install the file into the local repository.


2 Answers

It is wrong to use system scope for your proprietary JARs. You should deploy or install it into the local/central repository.

like image 125
lexicore Avatar answered Oct 09 '22 20:10

lexicore


I'm not sure this will help, but try using forward (/) instead of backward (\) slashes. Also, try running it with mvn -e and mvn -X (the latter will produce a lot of debugging lines) - this might help you pinpoint the problem.

Here's an example:

  • http://jmonkeyengine.org/groups/contribution-depot-jme3/forum/topic/maven-2-pomxml

of using ${basedir} in the same way you want.

Btw, why don't you mvn install:install-file the dependency instead of using systemPath? See:

  • http://maven.apache.org/plugins/maven-install-plugin/usage.html
like image 39
icyrock.com Avatar answered Oct 09 '22 20:10

icyrock.com