Home >Java >javaTutorial >How to Add JARs to a Maven 2 Build Classpath Without Installing Them?
Maven 2 can be frustrating during experimentation and quick prototyping. Creating pom.xml files and installing 3rd party libraries for each dependency is a tedious process. This article explores how to include jars located in a specific directory (/lib) in the build classpath without explicit installation.
Various online solutions suggest installing the dependency to the local repository or specifying a "system" scope in the pom. However, both approaches have drawbacks:
By adding a repository to the pom.xml with a specific structure, Maven will search for jars within a directory in the project:
<repository> <id>repo</id> <releases> <enabled>true</enabled> <checksumPolicy>ignore</checksumPolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <url>file://${project.basedir}/repo</url> </repository>
Instead of manually creating the directory structure, use the following command to install jars as artifacts:
mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]
To ensure the target package includes all dependencies, use the Assembly or OneJar plugins. OneJar simplifies this process with its straightforward documentation.
The above is the detailed content of How to Add JARs to a Maven 2 Build Classpath Without Installing Them?. For more information, please follow other related articles on the PHP Chinese website!