1

I have some local jar files from a non-maven project which I wish to include in my maven-based eclipse project.

These jar files are undergoing a lot of change as me and my project buddy attempt to 'fix' them, so I'd prefer not to upload them to a repository to avoid making a maven version of this non-maven project if this is possible.

Of course, the jar files need to be embedded in the resulting deployment jar. We did this before using Ant which let us specify that those jar files should be included.

How do you do the same thing in maven? Take into consideration that we do have maven dependencies too which all work fine and aren't required in the deployment. Some answers I've seen don't allow for this requirement.

Here's one of my attempts - the problem is that the jar does not get embedded:

<dependency>
    <groupId>se.krka.kahlua</groupId>
    <artifactId>kahlua-core</artifactId>
    <version>5.1_2.1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/kahlua-5.1_2.1.0-core.jar</systemPath>
</dependency>
Chris Watts
  • 6,197
  • 7
  • 49
  • 98
  • The maven [assembly plugin](http://maven.apache.org/plugins/maven-assembly-plugin/) might be the way to go. – Boris the Spider Jun 23 '13 at 14:03
  • See http://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them – brabec Jun 23 '13 at 14:31
  • How do you build those jars? With Ant? Consider automating uploading them to your shared/corporate repository as SNAPSHOT (if you have one). It would save you a lot of time, specially considering that they are "undergoing a lot of change". – adrianboimvaser Jun 23 '13 at 23:57
  • I would have to rent a server for this! – Chris Watts Jun 24 '13 at 07:24

2 Answers2

2

System paths are a very bad idea. When anybody else checks out your projects, he cannot build it anymore. (I always see such crap in many companies). The right solution would be to install the jar into the local repository:

$ mvn install:install-file -Dfile=[JAR NAME] -DgroupId=[GROUPID OF JAR] -DartifactId=[ARTIFACT OF JAR] -Dversion=[VERSION OF JAR] -Dpackaging=jar

In your project, you just add the dependency as usual after you installed the jar into the local repository.

<dependency>
    <groupId>[GROUPID OF JAR]</groupId>
    <artifactId>[ARTIFACT OF JAR]</artifactId>
    <version>[VERSION OF JAR]</version>
</dependency>
Bevor
  • 8,396
  • 15
  • 77
  • 141
  • Something happened around about the install part. I was doing it from the eclipse Build Configurations menu, and it seemed to go through okay, but maven complains it cant find `se.krka.kahlua:kahlua-core:jar:5.1_2.1.0` when it compiles. I had a look, and while the directories have been created, there are no jar files in the local repository. Is this supposed to happen or am I doing it wrong? – Chris Watts Jun 23 '13 at 14:40
  • @CJxD I'm not sure if Eclipse is doing this right. Please try it to execute the command in the console. I'm sure it will work then. – Bevor Jun 23 '13 at 15:15
  • It still doesn't work =[ I'm tempted just to give up and use Ant again – Chris Watts Jun 24 '13 at 08:08
  • I did add it relatively I suppose. It knew where it was supposed to look though when it added it in. I'll try again absolutely. – Chris Watts Jun 24 '13 at 14:52
  • I always do it relatively, I always change into the jar directory and never had problems. What exactly doesn't work? Here is one example of mine: `mvn install:install-file -Dfile=GoogleAdMobAdsSdk-6.4.1.jar -DgroupId=com.google.ads -DartifactId=googleadmobadssdk -Dversion=6.4.1 -Dpackaging=jar` – Bevor Jun 24 '13 at 17:05
  • Do I need to panic? Here's one I did: `mvn install:install-file -Dfile=lib\kahlua-5.1_2.1.0-core.jar -DgroupId=se.krka.kahlua -DartifactId=kahlua-core -Dversion=5.1_2.1.0 -Dpackaging=jar`. The files in `.m2\repository\se\krka\kahlua\kahlua-core\5.1_2.1.0` are `_maven.repositories`, `kahlua-core-5.1_2.1.0.jar.lastUpdated` and `kahlua-core-5.1_2.1.0.pom`, but there is no 105KB jar file in sight. – Chris Watts Jun 24 '13 at 22:52
  • Does Maven tell you "BUILD SUCCESS" after you execute this command or "BUILD ERROR"? If error, what is the error message? – Bevor Jun 25 '13 at 06:30
  • BUILD SUCCESS. Okay, I just semi-fixed it as I realised I didn't put `.jar` on the end of the `file` flag. Now I have jar files in the repository! Now the project compiles successfully, but m2eclipse is still complaining `Missing artifact se.krka.kahlua:kahlua-core:jar:5.1_2.1.0`. Furthermore, the library is not embedded in the deployment! – Chris Watts Jun 25 '13 at 10:08
  • Right click on the project: Maven -> Update Project should do it. Maybe you have to hit F5 on the project too (not sure atm). – Bevor Jun 25 '13 at 10:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32329/discussion-between-cjxd-and-bevor) – Chris Watts Jun 25 '13 at 10:18
  • Eclipse Users: You can create a new Run Configuration with the goal set as `install:install-file`, and add the rest of the flags as 'parameters'. – Chris Watts Jun 25 '13 at 11:01
1

You can use maven-install-plugin to install kahlua-5.1_2.1.0-core.jar into the local repository then this dependency will behave as any other, see http://maven.apache.org/plugins/maven-install-plugin/usage.html. Or make a remote repository in a location shared with your buddy and let him upload his jar there with maven-deploy-plugin:deploy-file (http://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html) each time he changes it and add this repository to your pom. You can use SNAPSHOT version if this jar changes often

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275