我有一个多模块项目,我正在尝试根据选择的配置文件更改子项目中构建的依赖项。在我的“dev”和“prod”配置文件中,我需要maven包含mysql库,而在我的“本地”配置文件中,我需要maven包含h2库。
父POM
<profile>
<id>local</id>
<properties>
<!-- Defines the environment variable -->
<build.env>local</build.env>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<!-- Defines the environment variable -->
<build.env>dev</build.env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<!-- Defines the environment variable -->
<build.env>production</build.env>
</properties>
</profile>
数据模块POM
<!-- Local specific dependencies -->
<profile>
<id>local</id>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${version.h2}</version>
</dependency>
</dependencies>
<activation>
<property>
<name>build.env</name>
<value>local</value>
</property>
</activation>
</profile>
<!-- Dev specific dependencies -->
<profile>
<id>dev</id>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${version.mysql}</version>
</dependency>
</dependencies>
<activation>
<property>
<name>build.env</name>
<value>dev</value>
</property>
</activation>
</profile>
<!-- Production specific dependencies -->
<profile>
<id>prod</id>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${version.mysql}</version>
</dependency>
</dependencies>
<activation>
<property>
<name>build.env</name>
<value>production</value>
</property>
</activation>
</profile>
问题是,从根项目构建时,切换配置文件似乎不会影响子pom,并且数据模块jar文件中不包含任何依赖项(没有配置文件继承?)。如何触发子配置文件以包含上面列出的依赖项?
答案 0 :(得分:1)
父POM:
<profile>
<id>local</id>
<properties>
<!-- Defines the environment variable -->
<build.env>local</build.env>
<!-- Define DB dependency type/version -->
<dbGroup>com.h2databse</dbGroup>
<dbArtifact>h2</dbArtifact>
<dbVersion>${version.h2}</dbVersion>
</properties>
</profile>
[...]
模块POM:
<dependency>
<groupId>${dbGroup}</groupId>
<artifactId>${dbArtifact}</artifactId>
<version>${dbVersion}</version>
</dependency>
您必须将您的version.h2 / version.mysql定义移动到您的父POM,但这不应该是一个问题。