Guilherme de Maio Software Developer

Using local jars in a maven project

Maven is great, really, but sometimes you just have to use that obscure jar library that isn’t in any maven repository. You know, not everyone uses maven.

So, how do you handle this type of dependencies? This is what I do, and you might find it useful: I create a local repository within the project. This way you can put everything in you version control system, and have maven deal with everything. Also, if you have several subprojects using the same library, you can create the local repository in the toplevel.

OK. Sounds good, right? Here’s how I do it:

  1. Create a directory under the project. I always use "lib"
  2. Install the jar libs to this directory using:
    mvn install:install-file -Dfile=<jar_file>  -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=jar -DlocalRepositoryPath=<path_to_lib_dir>
  3. Add the repository to you POM file:
    <repositories>
        <repository>
            <id>lib</id>
            <url>file://${project.basedir}/lib</url>
        </repository>
    </repositories>
  4. Add the dependencies for whatever jars you've added to your local repo
  5. Joy.

And that’s it. If you want to share a different solution, please do.

(PT-BR version below)


Usando jars locais num projeto maven

Maven é legal,  mesmo, mas as vezes você tem que usar aquela biblioteca jar obscura que não está em nenhum repositório. Você sabe, né, nem todo mundo usa maven.

Entao, como você lida com essas dependências? Eu faço assim, e talvez alguém acha útil: eu crio um repositório local dentro do projeto. Assim dá pra colocar tudo no git (ou em qualquer outro VCS de sua preferência) e deixar o maven cuidar de tudo. E se você tiver vários subprojetos dependendo ds mesmas bibliotecas, pode criar o repositório dentro do projeto "guarda-chuva".

OK. Parece legal? Como fazer:

  1. Crie um diretório dentro do projeto. Eu uso sempre "lib"
  2. Instale os  jars nesse diretório usando o maven:
    mvn install:install-file -Dfile=<jar_file>  -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=jar -DlocalRepositoryPath=<path_to_lib_dir>
  3. Adicione o repositório ao seu POM:
    <repositories>
        <repository>
            <id>lib</id>
            <url>file://${project.basedir}/lib</url>
        </repository>
    </repositories>
  4. Adicione ao pom as dependências para os jars que você instalou
  5. Felicidade.

É isso! Se alguém quiser compartilhar uma solução melhor, fique à vontade.