tags:

views:

96

answers:

2

How do I install a specific package, like derbytools, with Maven without specifying it as a dependency of project?

+4  A: 

Here is a sample using the mvn install goal. I used windows style env vars in place of parameters you will need to provide.

mvn install:install-file -DgroupId=%DERBYTOOLS_GROUP_ID% \ 
    -DartifactId=%DERBYTOOLS_ARTIFACT_ID% \
    -Dversion=%DERBYTOOLS_VERSION% \
    -Dpackaging=jar \
    -Dfile=%DERBYTOOLS_FILE_PATH%
rich
What is the file argument? I don't have to specify it when I'm defining dependencies.
J. Pablo Fernández
The file argument is the artifact you want to install, derbytools.jar or something like that here.
Pascal Thivent
+1  A: 

For Maven to be able to use a jar, the jar needs to be declared as a dependency.

If you have a jar that doesn't already exist on a Maven repository you can install it to your local repository using the install-plugin's install-file goal (as rich's answer says). This generates a pom using the values you provide and installs the pom and the jar to the local repository. Once that is done you would then add the dependency to your project's pom and use it as normal.

In this case the dependency does exist on the central Maven repository (you can simply search for artifacts using the Sonatype public repository btw), so you can simply add this dependency to your POM:

<dependency>
  <groupId>org.apache.derby</groupId>
  <artifactId>derbytools</artifactId>
  <version>10.4.2.0</version>
</dependency>

If you do not want to install a dependency for whatever reason, you can alternatively use the system scope to reference a jar by it's absolute file system path. This approach is not recommended though as it obviously affects portability.

From the documentation:

Dependencies with the scope system are always available and are not looked up in repository. They are usually used to tell Maven about dependencies which are provided by the JDK or the VM.

You could reference your derbytools jar as a system-scoped dependency like this:

<dependency>
  <groupId>org.apache.derby</groupId>
  <artifactId>derbytools</artifactId>
  <version>10.4.2.0</version>
  <scope>system</scope>
  <systemPath>/path/to/derbytools.jar</systemPath>
</dependency>
Rich Seller