具有同级项目依赖项的多模块Maven Spring启动项目

时间:2019-09-10 16:10:13

标签: java maven spring-boot maven-3 multi-module

我正在尝试创建一个多模块Maven Spring Boot项目。我的项目层次结构如下:

Parent-Project: (packaging type: pom)
  |
  |==> Commons-Project (packaging type: jar)
  |==> Child-Project (packaging type: jar | Has Commons-Project dependency)

当我尝试编译父项目时,它会成功编译Parent-Project和Commons-Project。但是,Child-Project的构建失败!

以下是代码:

父pom:

\begin{thebibliography}{}

\bibitem[\protect\citeauthoryear{{Allen}}{{Allen}}{1973}]{1973asqu.book.....A}
{Allen}, C.~W. (1973).
\newblock {\em {Astrophysical quantities}}.

\end{thebibliography}

公用pom:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

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

    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.1.0</version>

    <name>parent</name>
    <packaging>pom</packaging>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>commons</artifactId>
                <version>${commons.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

    <modules>
        <module>commons</module>
        <module>child</module>
    </modules>
</project>

公用程序类:

<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>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent</artifactId>
        <version>1.1.0</version>
    </parent>

    <artifactId>commons</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
</project>

儿童pom:

package commons.utils;

public class CommonUtility {
    //Common Utility methods go here
}

儿童主班:

<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>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent</artifactId>
        <version>1.1.0</version>
    </parent>

    <artifactId>child</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>commons</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

当我在STS中运行Child's Main类时,一切正常。但是当我尝试运行package child; import javax.annotation.PostConstruct; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import commons.utils.CommonUtility; @SpringBootApplication public class ChildApplication { public static void main(String[] args) throws Exception { SpringApplication.run(ChildApplication.class, args); } @PostConstruct public void init() { // For test System.out.println(CommonUtility.class.getName()); } } mvn clean package时,构建失败并显示以下消息:

mvn clean install

我不确定,我在做什么错?

我已将此项目上传到GitHub,以防万一有人想看一下代码。如果有人能够找到解决方案。请让我知道。

2 个答案:

答案 0 :(得分:0)

请尝试以下更改。

在Commons pom中添加以下内容。

<groupId>commons</groupId>

最后,常见的pom.xml如下所示。

<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>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>commons</groupId>
    <artifactId>commons</artifactId>
</project>

现在将以下内容添加到子pom.xml中。

<groupId>commons</groupId>

最后一个子pom.xml看起来像这样。

<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>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>child</artifactId>

    <dependencies>
        <dependency>
            <groupId>commons</groupId>
            <artifactId>commons</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

答案 1 :(得分:0)

尝试如下更新pom。 完整的示例已上传到GitHub

父pom:

git fetch --prune

常见pom:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

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

<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.1.0</version>

<name>parent</name>
<packaging>pom</packaging>
<description>Demo project for Spring Boot</description>

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

<dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
</dependencyManagement>

<modules>
    <module>commons</module>
    <module>child</module>
</modules>

儿童pom:

<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>com.example</groupId>
<artifactId>commons</artifactId>
<packaging>jar</packaging>

<parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.1.0</version>
</parent>