首页 文章

父pom中的maven-compiler-plugin

提问于
浏览
2

我在设置父子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' . 但没用 . 仅供参考: Build 亲子项目工作正常,没有任何问题 . 有帮助吗?这是我的父母和孩子的POM


父母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>
                    <compilerVersion>1.7</compilerVersion>
                    <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>
<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>

------------------------------------------------- ----

IDE Image

3 回答

  • 1

    在父级中,您需要在 <pluginManagement/> 而不是 <plugins/> 中定义它

    https://maven.apache.org/pom.html#Plugin_Management

    pluginManagement:是一个沿侧插件看到的元素 . 插件管理以大致相同的方式包含插件元素,除了不是为这个特定的项目构建配置插件信息,它旨在配置从这个继承的项目构建

    <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>
    

    如文档中所述, the child project also needs to reference the plugin

    <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>
    
  • 7

    在进一步研究之后,我了解到父pom中的maven-compiler-plugin中的java版本适用于子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>
    

    enter image description here

  • 1

    您应该检查项目的合规性 . 在Eclipse的Project Explorer中右键单击该项目,然后转到Properties,并按照图片所示进行设置(如果安装了正确的JDK,也可以将其设置为1.7版):

    enter image description here

相关问题