具有配置文件的Spring Boot Maven多模块项目-找不到其他模块的软件包

时间:2019-05-01 10:13:32

标签: java spring maven spring-boot

我目前正在尝试使用多个pom文件构造Spring Boot Maven应用程序。我有两个需要共享很多类的应用程序。我有以下结构:

.
├── application1
|   |── src
│   └── pom.xml
|── application2
|   |── src
│   └── pom.xml
|── shared 
|   └── src
|   └── pom.xml
|
└── pom.xml

根目录中的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 
         http://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.3.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>nl.example.parent</groupId>
    <artifactId>example-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>

    <!-- sub modules -->
    <modules>
        <module>application1</module>
        <module>application2</module>
        <module>shared</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <maven.plugins.version>2.22.1</maven.plugins.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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${maven.plugins.version}</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.plugins.version}</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>application1</id>
            <modules>
                <module>shared</module>
                <module>application1</module>
            </modules>
        </profile>
        <profile>
            <id>application2</id>
            <modules>
                <module>shared</module>
                <module>application2</module>
            </modules>
        </profile>
  </profiles>

</project>

application1和application2目录中的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 
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>nl.example.parent</groupId>
        <artifactId>example-parent</artifactId>
        <version>1.0</version>
    </parent>

    <groupId>nl.example.application1</groupId>
    <artifactId>example-child</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>

    <dependencies>
        <!-- Is this the right way to import the shared module into application1 or application2? -->
        <dependency>
            <groupId>nl.example.shared</groupId>
            <artifactId>example-shared</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

</project>

然后我有一个共享模块,其中大多数类将在application1和application2之间共享。共享目录的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 
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>nl.example.parent</groupId>
        <artifactId>example-parent</artifactId>
        <version>1.0</version>
    </parent>

    <groupId>nl.example.shared</groupId>
    <artifactId>example-shared</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>

    <dependencies>
    <!-- Shared dependencies here --> 
    </dependencies>

</project>

但是,当我尝试为两个应用程序之一构建特定的配置文件时,我尝试从共享模块进行的每次导入都会出错。显然我做错了,但是我无法弄清楚为什么从application1调用时共享文件夹中的类无法访问?我确实在application1和application2中都包含了共享模块作为依赖项

1 个答案:

答案 0 :(得分:2)

您的父级POM具有Spring Boot作为父级。但这是行不通的。您不会在模块中获得依赖关系。

您必须配置一个不继承自Spring Boot Parent的项目,并将Spring Boot依赖项添加到依赖项管理中。

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-maven-without-a-parent