在使用IntelliJ IDEA运行多模块项目时出现错误。

huangapple go评论65阅读模式
英文:

Getting error on running a multi-module project in IntelliJ IDEA

问题

我正在IntelliJ IDEA Ultimate 2020.2中创建一个Maven项目,这是一个多模块项目。当我从其中一个模块运行一个类时,我会得到一个错误,但如果我从打包的JAR运行,则可以正常运行。以下是应用程序的结构:

C:.
| pom.xml
|
+---DatabaseTier
| | pom.xml
| |
| +---src
| | ---main
| | +---java
| | | | module-info.java
| | | |
| | | ---org
| | | ---Geek8080
| | | | DatabaseTest.java
| | | |
| | | ---service
| | | ---database
| | | | Database.java
| | | |
| | | ---entities
| | | JournalPage.java
| | |
| | ---resources
| | log4j2.xml
| | table.sql
| |
| ---target
| +---classes
| | | log4j2.xml
| | | module-info.class
| | | table.sql
| | |
| | ---org
| | ---Geek8080
| | | DatabaseTest.class
| | |
| | ---service
| | ---database
| | | Database.class
| | |
| | ---entities
| | JournalPage.class
| |
| ---generated-sources
| ---annotations
---Reports
| pom.xml
|
+---src
| +---main
| | +---java
| | | | module-info.java
| | | |
| | | ---org
| | | ---Geek8080
| | | | ReportTest.java
| | | |
| | | ---service
| | | ---report
| | | ExcelReports.java
| | | PDFReports.java
| | |
| | ---resources
| | log4j2.xml
| | MTCORSVA.TTF
| |
| ---test
| ---java
---target
+---classes
| | log4j2.xml
| | module-info.class
| | MTCORSVA.TTF
| |
| ---org
| ---Geek8080
| | ReportTest.class
| |
| ---service
| ---report
| ExcelReports.class
| PDFReports.class
|
---generated-sources
---annotations

主pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.Geek8080</groupId>
    <artifactId>Journal</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>

    <modules>
        <module>DatabaseTier</module>
        <module>Reports</module>
    </modules>

    <name>Journal Daily</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <!-- ... 其他配置 ... -->

</project>

DatabaseTier模块中的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Journal</artifactId>
        <groupId>org.Geek8080</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>DatabaseTier</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <!-- ... 其他依赖 ... -->

    <!-- ... 构建配置 ... -->

</project>

Reports模块中的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Journal</artifactId>
        <groupId>org.Geek8080</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Reports</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <!-- ... 其他依赖 ... -->

    <!-- ... 构建配置 ... -->

</project>

错误信息:

Error occurred during initialization of boot layer
java.lang.LayerInstantiationException: Package org.Geek8080 in both
module Reports and module DatabaseTier

Process finished with exit code 1
英文:

I am making a maven project in IntelliJ IDEA Ultimate edition 2020.2. It is a multi-module project. When I run a class from one of the module I get an error but it runs fine if I run from the packaged jar. Here is the Structure of application:

C:.
| pom.xml
|
+---DatabaseTier
|   |   pom.xml
|   |
|   +---src
|   |   \---main
|   |       +---java
|   |       |   |   module-info.java
|   |       |   |
|   |       |   \---org
|   |       |       \---Geek8080
|   |       |           |   DatabaseTest.java
|   |       |           |
|   |       |           \---service
|   |       |               \---database
|   |       |                   |   Database.java
|   |       |                   |
|   |       |                   \---entities
|   |       |                           JournalPage.java
|   |       |
|   |       \---resources
|   |               log4j2.xml
|   |               table.sql
|   |
|   \---target
|       +---classes
|       |   |   log4j2.xml
|       |   |   module-info.class
|       |   |   table.sql
|       |   |
|       |   \---org
|       |       \---Geek8080
|       |           |   DatabaseTest.class
|       |           |
|       |           \---service
|       |               \---database
|       |                   |   Database.class
|       |                   |
|       |                   \---entities
|       |                           JournalPage.class
|       |
|       \---generated-sources
|           \---annotations
\---Reports
    |   pom.xml
    |
    +---src
    |   +---main
    |   |   +---java
    |   |   |   |   module-info.java
    |   |   |   |
    |   |   |   \---org
    |   |   |       \---Geek8080
    |   |   |           |   ReportTest.java
    |   |   |           |
    |   |   |           \---service
    |   |   |               \---report
    |   |   |                       ExcelReports.java
    |   |   |                       PDFReports.java
    |   |   |
    |   |   \---resources
    |   |           log4j2.xml
    |   |           MTCORSVA.TTF
    |   |
    |   \---test
    |       \---java
    \---target
        +---classes
        |   |   log4j2.xml
        |   |   module-info.class
        |   |   MTCORSVA.TTF
        |   |
        |   \---org
        |       \---Geek8080
        |           |   ReportTest.class
        |           |
        |           \---service
        |               \---report
        |                       ExcelReports.class
        |                       PDFReports.class
        |
        \---generated-sources
            \---annotations

This is the main pom.xml:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
             xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
             xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
        &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    
        &lt;groupId&gt;org.Geek8080&lt;/groupId&gt;
        &lt;artifactId&gt;Journal&lt;/artifactId&gt;
        &lt;packaging&gt;pom&lt;/packaging&gt;
        &lt;version&gt;1.0&lt;/version&gt;
    
        &lt;modules&gt;
            &lt;module&gt;DatabaseTier&lt;/module&gt;
            &lt;module&gt;Reports&lt;/module&gt;
        &lt;/modules&gt;
    
        &lt;name&gt;Journal Daily&lt;/name&gt;
    
        &lt;properties&gt;
            &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
            &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
            &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
        &lt;/properties&gt;
    
        &lt;dependencies&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
                &lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
                &lt;version&gt;2.13.3&lt;/version&gt;
            &lt;/dependency&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
                &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
                &lt;version&gt;2.13.3&lt;/version&gt;
            &lt;/dependency&gt;
        &lt;/dependencies&gt;
    
        &lt;build&gt;
            &lt;plugins&gt;
                &lt;plugin&gt;
                    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                    &lt;version&gt;3.8.0&lt;/version&gt;
                    &lt;configuration&gt;
                        &lt;source&gt;11&lt;/source&gt;
                        &lt;target&gt;11&lt;/target&gt;
                    &lt;/configuration&gt;
                &lt;/plugin&gt;
                &lt;plugin&gt;
                    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                    &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
                    &lt;version&gt;3.1.1&lt;/version&gt;
                    &lt;executions&gt;
                        &lt;execution&gt;
                            &lt;id&gt;copy-dependencies&lt;/id&gt;
                            &lt;phase&gt;package&lt;/phase&gt;
                            &lt;goals&gt;
                                &lt;goal&gt;copy-dependencies&lt;/goal&gt;
                            &lt;/goals&gt;
                            &lt;configuration&gt;
                                &lt;outputDirectory&gt;${project.build.directory}/lib&lt;/outputDirectory&gt;
                                &lt;overWriteReleases&gt;false&lt;/overWriteReleases&gt;
                                &lt;overWriteSnapshots&gt;false&lt;/overWriteSnapshots&gt;
                                &lt;overWriteIfNewer&gt;true&lt;/overWriteIfNewer&gt;
                            &lt;/configuration&gt;
                        &lt;/execution&gt;
                    &lt;/executions&gt;
                &lt;/plugin&gt;
            &lt;/plugins&gt;
        &lt;/build&gt;
    &lt;/project&gt;

The pom.xml in DatabaseTier module:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;parent&gt;
        &lt;artifactId&gt;Journal&lt;/artifactId&gt;
        &lt;groupId&gt;org.Geek8080&lt;/groupId&gt;
        &lt;version&gt;1.0&lt;/version&gt;
    &lt;/parent&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;artifactId&gt;DatabaseTier&lt;/artifactId&gt;
    &lt;version&gt;1.0&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.derby&lt;/groupId&gt;
            &lt;artifactId&gt;derby&lt;/artifactId&gt;
            &lt;version&gt;10.15.2.0&lt;/version&gt;
            &lt;scope&gt;compile&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;archive&gt;
                        &lt;manifest&gt;
                            &lt;mainClass&gt;org.Geek8080.DatabaseTest&lt;/mainClass&gt;
                        &lt;/manifest&gt;
                    &lt;/archive&gt;
                    &lt;descriptorRefs&gt;
                        &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                    &lt;/descriptorRefs&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;make-assembly&lt;/id&gt; &lt;!-- this is used for inheritance merges --&gt;
                        &lt;phase&gt;package&lt;/phase&gt; &lt;!-- bind to the packaging phase --&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;single&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;


&lt;/project&gt;

The pom.xml in Reports module:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;parent&gt;
        &lt;artifactId&gt;Journal&lt;/artifactId&gt;
        &lt;groupId&gt;org.Geek8080&lt;/groupId&gt;
        &lt;version&gt;1.0&lt;/version&gt;
    &lt;/parent&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;artifactId&gt;Reports&lt;/artifactId&gt;
    &lt;version&gt;1.0&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.Geek8080&lt;/groupId&gt;
            &lt;artifactId&gt;DatabaseTier&lt;/artifactId&gt;
            &lt;version&gt;1.0&lt;/version&gt;
            &lt;scope&gt;compile&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.pdfbox&lt;/groupId&gt;
            &lt;artifactId&gt;pdfbox&lt;/artifactId&gt;
            &lt;version&gt;2.0.21&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;archive&gt;
                        &lt;manifest&gt;
                            &lt;mainClass&gt;org.Geek8080.ReportTest&lt;/mainClass&gt;
                        &lt;/manifest&gt;
                    &lt;/archive&gt;
                    &lt;descriptorRefs&gt;
                        &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                    &lt;/descriptorRefs&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;make-assembly&lt;/id&gt; &lt;!-- this is used for inheritance merges --&gt;
                        &lt;phase&gt;package&lt;/phase&gt; &lt;!-- bind to the packaging phase --&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;single&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

&lt;/project&gt;

This is the error I am getting:

> Error occurred during initialization of boot layer
> java.lang.LayerInstantiationException: Package org.Geek8080 in both
> module Reports and module DatabaseTier
>
> Process finished with exit code 1

答案1

得分: 1

"它几乎是不言而喻的

> 在模块Reports和模块DatabaseTier中都有org.Geek8080

这样,有了模块系统并且在模块路径上有明确的模块,就不允许两个模块导出相同的包。这个改变是为了朝着可靠的配置和更好的可访问性方向迈进的。关于这些概念的更多信息可以在模块系统的状态中找到。

作为解决方案,您可以在这些模块中分别将包重命名为org.report.Geek8080org.database.Geek8080。"

英文:

It speaks for itself pretty much

> Package org.Geek8080 in both module Reports and module DatabaseTier

such that with the module system in place and explicit modules on the modulepath, no two modules can export the same package. This change was brought in the light of moving towards reliable configuration and better accessibility. More to read about those concepts could be found in The State of the Module System.

As a solution, you can rename the packages to org.report.Geek8080 and org.database.Geek8080 respectively in those modules.

huangapple
  • 本文由 发表于 2020年9月4日 19:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63740603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定