1

I have an application that depends on 2 jar file :

  • OperatorInterface.jar
  • Operator.jar

I want to build my project using Maven structure. I've followed the Maven site tutorial to create the structure like so:

mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=source.app -DartifactId=project 

I've put my source file FileProcess.java in E:\project\src\main\java\source\app.

FileProcess has dependency in 2 external .jar files but I don't know how to define this dependency in pom.xml.

Here is the content of the pom.xml so far:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>source.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Can anyone help me?

Aurasphere
  • 3,841
  • 12
  • 44
  • 71
soha
  • 115
  • 1
  • 12
  • http://stackoverflow.com/questions/2229757/maven-add-a-dependency-to-a-jar-by-relative-path – Vovka Sep 28 '15 at 11:53
  • Are those jars thisrd-party? Or your own? –  Sep 28 '15 at 12:33
  • @ Arkadiy they are my own – soha Sep 28 '15 at 12:36
  • @soha is this for a company project with multiple people working on the application, or for a school/home-grown project? If it is for corporate usage then the suggestions I'd make are entirely different than what I'd suggest for a project that is "just you". – Gimby Sep 28 '15 at 13:29
  • @ Gimby this is for company – soha Sep 29 '15 at 05:41
  • @soha Since its for corporate use, I'm adding my own answer that goes a little further than what has been suggested so far. – Gimby Sep 29 '15 at 07:43

4 Answers4

2

First thing you need to install your jars in your maven local repository. You can follow the official tutorial here:

https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

During the installation you will have to choose your own artifactId, groupId and version. You can choose whatever value you want but remember them because you will need those to include your jars in the pom.

After that you can include them in your pom.xml adding these lines under the tag dependencies for each library to include:

<dependency>
  <groupId>your_group_id</groupId>
  <artifactId>your_artifact_id</artifactId>
  <version>your_version</version>
</dependency>
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
2

In the case when the JARs in question are your own code rather than third-party, the best approach is to "mavenize" the projects that build them as well. After you do that, running mvn install on those projects will place them in your Maven local repository where they will be available to other local Maven projects who declare them as dependencies.

1

Avoid adding them to yourpom.xml file straight. When you add .jars using this process, they will automatically reflect in your pom.xml Steps are - 1. Right click on your project in the file explorer in your eclipse. 2. Go to build Path option. 3. Select configure build path 4. Chose the Libraries tab in the window that appears. 5. Add your .jar file externally. 6. Click ok and come back to your project interface

Update your maven project by pressing Alt+F5 and restart eclipse. Your problem should be solved.

  • 1
    Every time you run "right click - Maven - Update Project" or change your POM or add another Maven project your claspath in Eclipse Build Path gets overwritten by M2E. –  Sep 28 '15 at 12:33
  • Even if Eclipse were used, this is the worst advice you could possibly give. If you use Maven then Maven determines the project dependencies - period. If you do manual overrides in the IDE then your IDE build works and the Maven build fails, plus the mentioned project synchronization problem where the pom will undo your manual changes. – Gimby Sep 28 '15 at 12:56
1

I would take the following route since this is for corporate use. This is the hard and ultimately portable way that sets you up for future Maven usage as it is intended to be done.

1) make those dependent jars Maven projects (because then you can easily version-manage them too using Maven)

2) use a local repository manager and deploy your own projects to it using Maven release management through either the mvn:release plugin, or use a build server such as Hudson to automate the release process with a simple button press which I can highly recommend setting up.

https://maven.apache.org/repository-management.html

http://maven.apache.org/maven-release/maven-release-plugin/

3) mvn:release the dependency jars to your local repository manager so they will be available for other Maven projects

4) you're actually done, when you have a local repository where your deploy your own snapshot and release artifacts to, then your maven build can find your own maven modules and include them in the application dependencies - if you don't forget to configure the repository in the project's pom of course. And your build server if you have one can find them too.


The easy/lazy route is as suggested to manually install the jars in your local .m2 folder where Maven caches dependencies that it downloads, but it is absolutely not a portable solution that will stand the test of time. It won't work when somebody else needs to work on this project until they too install the jars locally. Or if its only you, you need to redo it every time you checkout the project on another computer / as another user. Also you need to update the jars each and every time you make changes to them, everywhere the project is checked out. You may need to do specific setup steps to get it working in an IDE, should you inevitably choose to start to use one.

However if you are having a time-pressure problem, then I would certainly go ahead and do that as a temporary workaround solution to be able to get going.

Gimby
  • 5,095
  • 2
  • 35
  • 47