You cannot use classes from different Maven modules

It is necessary to create a multi-mode maven project, consisting of a registration module and a main project. The problem is that it is not possible to use classes declared in different modules.

For example: I have ParentClaz in the parent src / main / java dir and ChildClaz in the child src / main / java directory. Itโ€™s not possible right now to use either ParentClaz in ChildClaz , or vice versa.

The structure of the project is as follows:

 +-- AdminPortal <- parent root +-- registration <- child root -- pom.xml <- child pom -- pom.xml <- parent pom 

my AdminPortal POM:

 <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>AdminPortal</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>AdminPortal</name> <url>http://maven.apache.org</url> <modules> <module>registration</module> </modules> 

Here's the child POM:

 <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example</groupId> <artifactId>AdminPortal</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.example.AdminPortal</groupId> <artifactId>registration</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>registration</name> <url>http://maven.apache.org</url> 

How to solve this problem?

+6
source share
3 answers

Your parent pom has a pom packaging type, this is not a jar . This is a special aggregator module. All java codes must be located in jar modules.

A module with a pom packaging type cannot generate artifacts such as jar, war or ear.

Example Maven - A Simple Parent Project

A parent project does not create a JAR or WAR like our previous projects; instead, it is just a POM, which applies to other Maven projects.

To use Classes from one module in another module, use the maven dependency.

A typical project is as follows:

 * administration project (pom) * registration (jar) * portal (war) 
+6
source

The child can use parental dependencies, try adding it to the parent

 <dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> </dependencies> 

and make this class a child

 import org.apache.log4j.Logger; public class Test { public static void main(String[] args) { Logger.getLogger(Test.class); } } 

and you will see that it compiles.

0
source

My pom.xml has only 1 dependency -

 <dependencies> <dependency> <groupId>com.shubham.TestNexus</groupId> <artifactId>TestNexus</artifactId> <version>1.0</version> </dependency> </dependencies> 

This is a test jar that I created and uploaded to my local Nexus repository. I could see that when I build the project, maven loads the jar and places it in the Maven Dependencies directory. I could see the same jar added to the class path.

This jar has nothing but a simple helloWorld printing method.

Now, when I use this jar file in my project, it does not allow me to create a class object inside this jar file.

And when I tried to make a non-maven Java project and added jar manually to the classpath, I was able to create an object of this class. Can someone please help here.

0
source

All Articles