Can't use classes from different Maven modules

核能气质少年 提交于 2019-12-12 08:53:54

问题


Need to create a multi module maven project which consist of a registration module and main project. The problem is that it's impossible to use classes declared in different modules.

e.g.: I have a ParentClaz in my parent's src/main/java dir and ChildClaz in child's src/main/java dir. Right now it's not possible to use neither ParentClaz in ChildClaz nor vice versa.

The project's structure looks like this:

+-- 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 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 can this problem be solved?


回答1:


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

Module with packaging type pom cant generate artifacts like jar, war or ear.

Maven by Example - The Simple Parent Project

The parent project doesn’t create a JAR or a WAR like our previous projects; instead, it is simply a POM that refers to other Maven projects.

To use Classes from one module in other module use maven dependency.

Typical project looks like this:

* administration project (pom)
   * registration (jar)
   * portal (war)



回答2:


Child can use parent dependencies, try to add this to parent

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

and make this class in 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.



来源:https://stackoverflow.com/questions/21006565/cant-use-classes-from-different-maven-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!