父 pom 中的 maven-compiler-plugin
maven-compiler-plugin in parent pom
我在为父子 pom 文件设置 java 编译器版本时遇到问题。我在 1.7 版的子 pom 中添加了 maven-compiler-plugin,并注意到子模块的 java 版本从默认的 1.5 更改为 1.7,而父项目仍然是 1.5。然后,我将编译器插件从子 pom 移到了父 pom。预计父和子 Maven 模块的编译器版本将更改为 1.7。但奇怪的是没有观察到变化,子模块仍然是 1.7,父项目是 1.5。我从 eclipse IDE 执行了 'maven- > update project'。但没有用。仅供参考:构建父子项目工作正常,没有任何问题。有什么帮助吗?这是我的父子 POM
父 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>
儿童 POM
<?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
pluginManagement: is an element that is seen along side plugins.
Plugin Management contains plugin elements in much the same way,
except that rather than configuring plugin information for this
particular project build, it is intended to configure project builds
that inherit from this one
<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>
您应该检查项目的合规性。在Eclipse的Project Explorer中右击项目,然后到Properties,如图设置(如果安装了合适的JDK也可以设置为1.7版本):
进一步研究后,我了解到父 pom 中的 maven-compiler-plugin 中的 java 版本适用于子 POM,但不适用于父 POM 本身。基本上,大多数时候,不建议在父项目中保留任何源代码,勉强处理子模块的所有构建配置。这是更新的 POMS :
父 POM
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<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>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>ModuleOne</module>
</modules>
儿童 POM
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">
4.0.0
<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>
<packaging>jar</packaging>
<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>
我在为父子 pom 文件设置 java 编译器版本时遇到问题。我在 1.7 版的子 pom 中添加了 maven-compiler-plugin,并注意到子模块的 java 版本从默认的 1.5 更改为 1.7,而父项目仍然是 1.5。然后,我将编译器插件从子 pom 移到了父 pom。预计父和子 Maven 模块的编译器版本将更改为 1.7。但奇怪的是没有观察到变化,子模块仍然是 1.7,父项目是 1.5。我从 eclipse IDE 执行了 'maven- > update project'。但没有用。仅供参考:构建父子项目工作正常,没有任何问题。有什么帮助吗?这是我的父子 POM
父 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>
儿童 POM
<?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
pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one
<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>
您应该检查项目的合规性。在Eclipse的Project Explorer中右击项目,然后到Properties,如图设置(如果安装了合适的JDK也可以设置为1.7版本):
进一步研究后,我了解到父 pom 中的 maven-compiler-plugin 中的 java 版本适用于子 POM,但不适用于父 POM 本身。基本上,大多数时候,不建议在父项目中保留任何源代码,勉强处理子模块的所有构建配置。这是更新的 POMS :
父 POM
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<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>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>ModuleOne</module>
</modules>
儿童 POM
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"> 4.0.0
<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>
<packaging>jar</packaging>
<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>