Java 不能使用来自不同 Maven 模块的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21006565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't use classes from different Maven modules
提问by amentheitroade
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.
需要创建一个由注册模块和主项目组成的多模块maven项目。问题是不可能使用在不同模块中声明的类。
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.
例如:我ParentClaz
在我父母的 src/main/java 目录和ChildClaz
孩子的 src/main/java 目录中有一个。现在既不能使用ParentClaz
inChildClaz
也不能使用in ,反之亦然。
The project's structure looks like this:
该项目的结构如下所示:
+-- AdminPortal <- parent root
+-- registration <- child root
-- pom.xml <- child pom
-- pom.xml <- parent pom
my AdminPortal POM:
我的 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:
这是子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?
如何解决这个问题?
采纳答案by MariuszS
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.
你的父 pom 有包装类型pom
,这不是jar
。这是特殊的聚合器模块。所有 java 代码都应该位于jar
模块中。
Module with packaging type pom
cant generate artifacts like jar, war or ear.
带有包装类型的模块pom
不能生成像 jar、war 或 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.
父项目不像我们以前的项目那样创建 JAR 或 WAR;相反,它只是一个引用其他 Maven 项目的 POM。
To use Classes from one module in other module use maven dependency.
要在另一个模块中使用一个模块中的类,请使用 maven 依赖项。
Typical project looks like this:
典型的项目是这样的:
* administration project (pom)
* registration (jar)
* portal (war)
回答by Evgeniy Dorofeev
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.
你会看到它编译。