【发布时间】:2019-09-19 22:13:45
【问题描述】:
我目前正在尝试使用多个 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 中
【问题讨论】:
标签: java spring maven spring-boot