5

I have a Java project using Maven. In my project I have a folder called libs that contains all the JARs that I can't load from internet/external repository.

My issue is how to specify to Maven to include those JARs during packaging, building etc.?

New to Maven, sorry if it is a stupid question.

EDIT : I have an automatic tool that will look up at my pom.xml on my Git to build & deploy my project on different environments. So adding it in local Maven repo, as suggested here will not help since it will work only on my PC. If I add a libs folder with my JARs it will work wherever I am.

Ask me in comments if it's not clear or if I'm mistaken.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Logan Wlv
  • 3,274
  • 5
  • 32
  • 54
  • 1
    Possible duplicate of [How to add local jar files to a Maven project?](https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project) – Ali Akbarpour Feb 07 '18 at 14:10
  • This is not a job that Maven should do. Just include these JARs in your classpath. – Mick Feb 07 '18 at 14:10
  • @Generic this will add local JARs into your maven local repository but that's not what I want to do here. – Logan Wlv Feb 07 '18 at 14:32
  • @Mick I agree, but then for example I put my code on GIT with my extern JARs in lib, if someone take the code he'll have to set the classpath again right ? I have a automatic tool that use Maven to build&deploy my project, setting the classpath will be good only on local using Eclipse if I'm not wrong ? – Logan Wlv Feb 07 '18 at 14:33
  • Please have a look at https://stackoverflow.com/a/4491469/5006866, or better https://stackoverflow.com/a/29272852/5006866 – thokuest Feb 07 '18 at 17:28
  • @thokuest Re your first link: [`system`](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#System_Dependencies) is deprecated. – Gerold Broser Feb 07 '18 at 18:44
  • @LoganWlv: just work with relative file paths and you will be fine (?) – Mick Feb 08 '18 at 12:41
  • @Mick hmm not sure about what you mean – Logan Wlv Feb 08 '18 at 12:49
  • @Mick Re "_This is not a job that Maven should do. Just include these JARs in your classpath._" – Can it be that Maven builds up a classpath at `compile` and `testCompile` from its local repository, depending on the POM's dependency declarations, behind the scenes? Did you ever run a `mvn compile -X`? If yes, have you ever seen the property`classpathElements` there? – Gerold Broser Feb 08 '18 at 13:35

1 Answers1

4

Contrary to your EDIT adding to the local Maven repo will help and it can be automated as follows:

  • See jtahlborn's answer to Multiple install:install-file in a single pom.xml. (The current version of the maven-install-plugin is 2.5.2. I'd use that rather than the default.)
  • The <configuration>s should look like:

      <configuration>
        <file>${project.basedir}/path/to/your/libs/lib-X.jar</file>
        <repositoryLayout>default</repositoryLayout>
    
        <!-- match the dependency declaration for this artifact -->
        <groupId>logan.wlv</groupId>
        <artifactId>lib-X</artifactId>
        <version>x.y.z</version> 
        <packaging>jar</packaging>
        <!-- -------------------------------------------------- -->
      </configuration>
    
  • Put the install-plugin declaration into a build profile, e.g. lib.

  • Run mvn initialize -P lib once on every new PC (and once after the contents of libs, and hence the install-plugin declaration, changed) before invoking any phase that resolves dependencies first, e.g. compile.

or

  • Automate this even further with:

      <profile>
        <id>lib</id>
        <activation>
          <file>
            <missing>${settings.localRepository}/logan/wlv/lib-X/x.y.z/lib-X-x.y.z.jar</missing>
          </file>
        </activation>
        ...
      <profile>
    

    Such being able to run just mvn initialize without the explicit profile activation the very first time.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • You know that `...` may point elsewhere? – Thorbjørn Ravn Andersen Dec 27 '18 at 00:34
  • Also a shell script doing the `install:install-file` commands once on every new PC is quite similar to running `mvn initialize -P lib` once on every new PC. – Thorbjørn Ravn Andersen Dec 27 '18 at 00:56
  • @ThorbjørnRavnAndersen I know. Though not very common to be changed from the default (_Convention over configuration!_ ;) you're right. `${settings.localRepository}` should work (I never used or tried that) too and is more generic. Thanks for the hint. – Gerold Broser Jan 23 '19 at 19:26
  • @ThorbjørnRavnAndersen What's the point of introducing an extra script file that contains [all the `install-file` arguments](https://maven.apache.org/plugins/maven-install-plugin/usage.html#The_install:install-file_goal) compared to having all the relevant information in the very same POM and being able to execute it with a well-known `mvn ...` command? – Gerold Broser Jan 23 '19 at 19:39