Kim
Kim

Reputation: 5425

<Maven>How to add class files as dependency (not jar) in maven project?

I have two modules, A and B, they are under the same parent. Now A is requiring for B as dependency. I can't just use jar as dependency type because module B is using spring-boot-maven-plugin, so I was wondering how can I set A's pom configuration to make A depend on B's compiled classes not jar?

- root
  - A
    - src         # module A's source code
    - target
      - classes   # module A's compiled classes
      - A.jar     # module A's compiled jar output
    - pom.xml     # module A's mvn config
  - B
    - src         # module B's source code
    - target
      - classes   # module B's compiled classes, HOW CAN I IMPORT THESE TO A?
      - B.jar     # module B's mvn config
    - pom.xml     # module B's mvn config
  - pom.xml       # parent mvn config

parent mvn config

...
<modules>
  <module>A</module>
  <module>B</module>
</modules>
...

module A mvn config

...
<parent>
  <!- pointed to parent ->
</parent>
<dependencies>
  <dependency>
    <groupId></groupId>
    <artifactId>B</artifactId>
    <scope>??????????????</scope>   # WHAT SHOULD I PUT HERE?
    <type>???????????????</type>    # WHAT SHOULD I PUT HERE?
  </dependency>
<dependencies>
...

Upvotes: 6

Views: 8712

Answers (2)

J Fabian Meier
J Fabian Meier

Reputation: 35785

First of all: When A depends on the classes of some other module, it necessarily depends on the jar. You cannot depend on parts of a module or just the classes.

So let me sketch a solution:

  1. If B is of <packaging>jar</packaging> (standard, also true if no packaging is given), then you can just use it as dependency. You do not need a scope or type entry.

  2. If B is of some other packaging (including spring-defined packaging types, which I am no expert of), then you should not define a dependency from A on B. Instead, you define a third module C, which includes the classes that are used from A and from B and let both of them have a dependency on C.

Don't try to construct dependencies to non-jars. If you need classes, define a module with these classes and use it when the classes are needed.

Upvotes: 6

nagendra547
nagendra547

Reputation: 6302

Here is good information about maven dependency scope and type. https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

This should work fine for you. (Not tested)

<scope>compile</scope>
<type>jar</type>

Upvotes: -2

Related Questions