【问题标题】:How to build a multi-module Maven project without a main class in one of the modules如何在其中一个模块中构建没有主类的多模块 Maven 项目
【发布时间】:2019-05-28 17:00:42
【问题描述】:

我一直试图在类似的问题中找到这个问题的答案,但我仍然不知道是什么原因造成的。

我有一个多模块 maven 项目,我正在尝试运行 mvn installmvn package,但出现以下错误

[INFO] 应用程序.......................... ........ 成功 [1.025 秒]

[INFO] 模块数据 ..................................... 失败 [ 0.952 秒]

[INFO] 模块应用 .................................... 跳过

[ERROR] 未能执行目标 org.springframework.boot:spring-boot-maven-plugin:2.1.1.RELEASE:repackage (repackage) on project project-data:目标的执行重新打包 org.springframework.boot:spring-boot-maven-plugin:2.1.1.RELEASE:repackage 失败:找不到主类 -> [帮助 1]

project-app 模块在 src 文件夹中有一个 java 类,而project-data 模块没有主类。

父 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>
    <packaging>pom</packaging>
    <modules>
        <module>project-data</module>
        <module>project-app</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>app</name>
    <description></description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

项目数据 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>app</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>project-data</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

项目应用 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>app</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>project-app</artifactId>

    <dependencies>
        <dependency>
            <artifactId>project-data</artifactId>
            <groupId>com.example</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

我有一些与this project (sfg-pet-clinic) 的结构非常相似的东西,但是,在下载并运行mvn package 之后,它工作正常。我看不出我的项目的 pom 文件和这个文件有什么区别,所以我可能缺少什么?

【问题讨论】:

标签: java spring maven multi-module


【解决方案1】:

配置适用于旧版本的 spring。

删除:

    <configuration>
       <skip>true</skip>
    </configuration>

相反,在 pom artifactId

下方添加此标志
   <properties>
        <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
    </properties>

像这样:

 <parent>
        <artifactId>mc-pet-clinic</artifactId>
        <groupId>guru.springframework</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>pet-clinic-data</artifactId>

    <properties>
        <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
    </properties>

...

【讨论】:

    【解决方案2】:

    我会移动整个

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    

    从父模块到模块应用模块的部分,因为那个是使用重新打包功能的单个模块。

    我也会从模块数据中删除插件部分,因为不需要重新打包该模块。

    【讨论】:

      【解决方案3】:

      感谢 cmets 部分的 @wemu。

      添加这个:

      <configuration>
          <mainClass> ${your.start.Class}</mainClass>
      </configuration>
      

      spring-boot-starter-parent 依赖项的 2.1.1.RELEASE 版本需要插件部分,只要您的类在另一个模块中。

      我在问题中提到的项目sfg-pet-clinic 使用的是不需要&lt;mainClass&gt; 配置的旧版本

      【讨论】:

      • 不知道是谁投了反对票。感谢您记录您对问题的回答。如果这回答了您的问题,请继续接受它:)
      • 不确定为什么正确答案被否决,但再次感谢@wemu ;) 我刚刚接受了它
      【解决方案4】:

      好的,这就是我解决这个问题的方法。

      我的步骤是:

      1. 将项目数据的 pom.xml 文件的打包更改为 pom 从 jar(我使用了 Eclipse 的概述)
      2. 在项目应用程序的pom.xml文件中添加&lt;type&gt;pom&lt;/type&gt;以获取模块数据的依赖关系
              <dependency>
                  <groupId>samee.springframework</groupId>
                  <artifactId>project-data</artifactId>
                  <version>0.0.1-SNAPSHOT</version>
                   <type>pom</type>
              </dependency>  
      

      然后你可以运行你的清理和打包/安装

      【讨论】:

        【解决方案5】:

        我刚刚将父 pom.xml 中的 Spring Boot 版本从 2.1.1 更改为 2.0.3,它可以工作。

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
        

        虽然让它在 2.1.1 中工作会更好,但这是一个快速简单的解决方案。

        【讨论】:

          猜你喜欢
          • 2014-03-03
          • 1970-01-01
          • 2021-03-23
          • 1970-01-01
          • 2014-05-22
          • 1970-01-01
          • 2018-09-17
          • 1970-01-01
          • 2019-05-21
          相关资源
          最近更新 更多