Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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>
...
like image 660
Kim Avatar asked Sep 02 '17 07:09

Kim


People also ask

How do you add dependencies in the Maven project?

Add a Java Maven Dependency to the Utility ProjectRight-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.

How do I run a class file in Maven?

In order to do this, we can use the exec-maven-plugin. To be more specific, the exec:java goal from this plugin executes the supplied Java class with the enclosing project's dependencies as the classpath. As shown above, we're using the exec. mainClass system property to pass the fully qualified class name.

Are Maven dependencies included in jar?

Apache Maven Shade Plugin provides the capability to package the artifact in an uber-jar, which consists of all dependencies required to run the project.

Does Maven add dependencies to classpath?

Maven includes a dependency with this scope in the runtime and test classpaths, but not the compile classpath. This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.


1 Answers

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.

like image 82
J Fabian Meier Avatar answered Oct 21 '22 01:10

J Fabian Meier