Friday, February 19, 2016

Add compiled Java Classes to a Maven Project

There are several ways to add compiled external classes to a maven project. This method describes the local repository based method.

  • In the project directory, create a folder called repo, which we'll use as a folder based Maven repository.
  • Create a jar file from the compiled java classes with the following command.
jar cf jar-file input-file(s)


     Ex:

 
jar cf zql.jar .


  • Deploy the .jar to the file repository, with the following command:
mvn deploy:deploy-file
-Durl=file:///absolute/path/to/your-project/repo \
-DrepositoryId=file.repo \
-Dfile=path-to-your.jar \
-DgroupId=some.external.project.group \
-DartifactId=the-artifact-name \
-Dversion=1.0 \
-Dpackaging=jar;

 
Ex: 
 
mvn deploy:deploy-file -Durl=file:///home/TestProjects/MyImpl/repo -DrepositoryId=file.repo -Dfile=/home/TestProducts/ExternalProject/classes/zql.jar -DgroupId=zql -DartifactId=zql -Dversion=1.0 -Dpackaging=jar;
 
  • After adding the jar file to repository it looks as follows.

 
  • Finally add the file repository details and the dependency to your project pom.xml
<dependency>
     <groupId>zql</groupId>
     <artifactId>zql</artifactId>
     <version>1.0</version>
</dependency>

<repositories>
    <repository>
        <id>file.repo</id>
        <url>file://${project.basedir}/repo</url>
    </repository>
</repositories>