简体   繁体   中英

Java project dependency on another project

I have a maven project (Proj1). In my java class I have to call method of another maven proj (proj2).

Here I get various compile time errors as that method is not available in my current project (proj1). In order to resolve that I took whole method code and paste that method in my current java class. Now that method is available in my project. But then that method again calls few other code from other classes which are again not available in my current project. Again I needed to copy and paste all the dependent code from other classes. This goes on and on and circular dependency is there so just wondered how I can resolve this error. I have couple of options in order to resolve this.

  1. Take source of all the classes from proj2 make a jar out of that and include that jar in my classpath This solved few errors but then there are some classes from third party jars which still giving errors.
  2. Now I am planning for 2nd option in which I have pom.xml for proj2. I create build out of that. So that all the necessary jars will get downloaded from repository. Output of this proj is again jar named proj2.jar.

Now my question is

  1. If I include only proj2.jar in my code then will it resolve all the dependency?
  2. OR I need to manually add all the jars in proj1 class-path which are there in proj2
  3. OR I need to add src of proj2 as well as jars from proj2.jar?
  4. OR is there any better way of doing this than the above mentioned options?

Make all you projects use maven. Add pom.xml to the root of your project with following header(I don't know your packaging structure so com.sachin is placeholder. You can ask IDE to add maven support for you):

<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>com.sachin</groupId>
    <artifactId>Proj2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>My Proj2</name>

Add same header for Proj1 and add dependency from Proj1 to Proj2 :

<dependency>
    <groupId>com.sachin</groupId>
    <artifactId>Proj2</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

After maven build Proj2 will be added as jar dependency to Proj1 .

if I include only proj2.jar in my code then will it resolve all the dependency?

If your proj2.jar has pom.xml with dependencies that proj2.jar use - then yes they will be fetched during building of proj1 using maven.

Official documentation about Maven Dependency Mechanism

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