简体   繁体   中英

Managing dependencies in a Maven Project

I have 2 JARs that are supposed to be imported into a maven project. I followed this tutorial (click here ) and imported those JARs into my maven project. Basically, I executed this code in the terminal: mvn install:install-file -Dfile=myfile.jar -DgroupId=mygroup -DartifactId=com.mygroup.project -Dversion=1.0 -Dpackaging=jar -DlocalRepositoryPath=lib -DcreateChecksum=true and then I imported the library into my Maven project.

All this works fine. However, the JARs I am importing are supposed to have few dependencies themselves. As I understand, Maven handles the internal dependencies automatically. Now I have a list of dependencies (with group ID, artefact ID and version) but I don't understand where do I write those. In the folder 1.0 of the library, there is a file called myjar-1.0.pom. I tried writing the dependencies there but it was of no use.

Could you tell me a way of manually telling Maven to load up a few dependencies?

I also tried specifying these dependencies in the main pom.xml but it results in errors - saying the repo-url/dependency/file.pom was not found. So I guess it needs to be mentioned in internal dependency only - but I can't figure out a way of manually defining them. Will I need to create a pom.xml within those libraries, or is there something that I am missing out?

You can do this:

  1. write the pom for you jar, and declare the dependencies in the pom.
  2. user mvn install:install-file -Dfile=path-to-your-artifact-jar -DpomFile=path-to-pom to install you jar.

link: http://maven.apache.org/plugins/maven-install-plugin/examples/custom-pom-installation.html

Let's say you have the following JARs and dependencies:

myMavenProject
--> library1.jar
    --> library2.jar
    --> library3.jar

(I'm assuming that your library1 , library2 and library3 are not Maven projects)

For each library1 , library2 and library3 , perform mvn install:install-file like what you've did before:

mvn install:install-file -Dfile=library1.jar -DgroupId=com.mygroup.project -DartifactId=library1 -Dversion=1.0 -Dpackaging=jar

(Note that I swap your groupId and artifactId here)

Then in your Maven project, update your pom.xml to have following:

<dependency>
    <groupId>com.mygroup.project</groupId>
    <artifactId>library1</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>com.mygroup.project</groupId>
    <artifactId>library2</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>com.mygroup.project</groupId>
    <artifactId>library3</artifactId>
    <version>1.0</version>
</dependency>

Unless you have a very specific requirement, I'm not sure why you want to have -DlocalRepositoryPath=lib extra options for Maven and not using your local M2 Repository.

The tutorial you are following might be working, but it is not The Maven Way! If you will follow this path, you will have troubles all along the way!

You'd better read some tutorial from the maven user center or the maven books and resources page. Maven: the definitve guide for example is available online for free.

Why the approach you are currently following is not good and how you'd do this better is mentioned by Stephen Connolly on a very good blog post of him (Stephen is an active maven developer).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM