我在为父子pom文件设置java编译器版本时遇到问题。我在子pom的1.7版中添加了maven-compiler-plugin,并注意到子模块的Java版本从默认的1.5更改为1.7,而父项目仍使用1.5。然后,我将编译器插件从子pom移到了父pom。预期父maven模块和子maven模块的编译器版本都将更改为1.7。但是奇怪的是,没有观察到任何变化,子模块仍为1.7,而父项目为1.5。我从Eclipse IDE执行了“maven->更新项目”。但没有用。仅供引用:构建父子项目运行正常,没有任何问题。有什么帮助吗?这是我的 parent 和 child 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.mymaven.parentpom.example</groupId>
<artifactId>ParentPOMProj</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ParentPOMProj</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ParentPOMProj</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<compilerVersion>1.7</compilerVersion>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>ModuleOne</module>
</modules>
</project>
child 聚甲醛
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mymaven.parentpom.example</groupId>
<artifactId>ParentPOMProj</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.mymaven.parentpom.example.module</groupId>
<artifactId>ModuleOne</artifactId>
<version>0.0.2-SNAPSHOT</version>
<name>ModuleOne</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>ModuleOne</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
-------------------------------------------------- ---
最佳答案
在父级中,您需要使用<pluginManagement/>
而不是<plugins/>
定义它
https://maven.apache.org/pom.html#Plugin_Management
<build>
<finalName>ParentPOMProj</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
如文档中所述,子项目还需要引用插件:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- inherits config from parent: can override if required -->
</plugin>
</plugins>
</build>
关于java - 父pom中的maven-compiler-plugin,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37350273/