Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Add local dependencies to jar

Tags:

java

maven

I have a dependency

    <dependency>
        <groupId>de.matthiasmann</groupId>
        <artifactId>twl</artifactId>
        <version>1.0.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/TWL.jar</systemPath>
    </dependency>

Then I execute mvn assembly:assembly. All natives files and remote maven libs are added, but there is no this jar.

UPDATE

When I am trying to run app by java -jar myjar.jar. It returns an error that there is no class from the above dependency (NoClassDefFoundError : de.matthiasmann.twl.ForExample). I want to add classes from this jar to myjar.jar (the same what maven does with remote dependencies). How I can configure maven to do that?

like image 208
user14416 Avatar asked Nov 13 '12 01:11

user14416


2 Answers

See Maven 2 assembly with dependencies: jar under scope "system" not included for why system dependencies are not included and how you can work around it, specifically the mvn install:install-file code is what you want.

like image 171
JohnKlehm Avatar answered Sep 21 '22 15:09

JohnKlehm


You cannot use systemPath, unless your Java EE server/container has that jar configured.

Remember that maven is development and compile time only. Once the war file is built, maven has no effect except for having placed all the desired jars into the WEB-INF/lib folder.

When you specify system scope, it means that it is your responsibility to ensure that the jar is present when the war is deployed. You already have a framework to do that and you do not wish to encumber your build dependency with that jar, but you have to make it available thro Maven only during development.

The other similar scope is "provided". e.g., JBoss or your corporate common deployment Tomcat framework already provides many of the jars like Spring and Hibernate that are loaded by the server startup and common to all apps in the server. Therefore, you would not want maven build to include those into the war file.

The right way, Maven gurus would tell you. is to have your own maven server and build whatever artefacts you need into that server. However, occasionally that is not possible.

Therefore, on such occasions, I create project level repository that is distributed with the project and checked into version control. I run the command mvn install to create a project level directory called, say, "project-repo".

http://maven.apache.org/plugins/maven-install-plugin/examples/specific-local-repo.html (Due to familiarity, most of the time, I build the repo by hand rather than run mvn install).

Then in the POM, I specify file://${project.basedir}/project-repo as one of the repositories. The caveat with this is that in Windows, the slashes other than the pair after "file://" has to be back-slashes when referring to Windows file system paths.

  <repositories>
    <repository>
      <id>my-repo1</id>
      <name>my custom repo</name>
      <url>http://ho.ho.ho</url>
    </repository>
    <repository>
      <id>project-repo</id>
      <name>my project repo</name>
      <url>file://${project.basedir}\project-repo</url>
    </repository>
  </repositories>
like image 43
Blessed Geek Avatar answered Sep 21 '22 15:09

Blessed Geek