maven-compiler-plugin in parent pom

19,250

Solution 1

After researching further, I understood that the java version in maven-compiler-plugin in parent pom applies to the child POMs but not to the parent itself. Basically, most of the time, it is not recommend to keep any source code in parent project, it is barely to handle all the build configuration for the child modules. Here are the updated POMS :


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


CHILD 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

Solution 2

In the parent you need to define it in <pluginManagement/> rather than <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>

As mentioned in the docs, 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>
Share:
19,250
Johnyzhub
Author by

Johnyzhub

Software Programmer. Certified Java developer. Java and Jakarta EE enthusiast.

Updated on June 05, 2022

Comments

  • Johnyzhub
    Johnyzhub almost 2 years

    I am facing issue in setting java compiler version for the parent-child pom files. I added maven-compiler-plugin in child pom with version 1.7 and noticed that the java version changed from default 1.5 to 1.7 for child module and the parent project is still with 1.5. Then, I moved the compiler plugin from child pom to parent pom. Expected that the compiler version will be changed to 1.7 for both parent and child maven modules. But strangely no changes observed, the child module is still with 1.7 and the parent project is 1.5. I performed 'maven- > update project' from eclipse IDE. but no use. FYI : Building the parent-child projects is working fine with no issues. Any help? Here are my parent and child POMs


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

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

    IDE Image

  • Alan Hay
    Alan Hay almost 8 years
    Update your question to show the new content of both poms.
  • Johnyzhub
    Johnyzhub almost 8 years
    Updated my question. Please take a look.
  • Johnyzhub
    Johnyzhub almost 8 years
    I want to control this through pom. I believe, this is the purpose of using maven-compiler-plugin after all.
  • Alan Hay
    Alan Hay almost 8 years
    So you actually have code in the parent pom. This is not normally the case. You need then to add the <plugins/> block in parent as well. However I would suggest that you do not add code in the parent. Note that it has packaging type POM as opposed to JAR so nothing will get compiled on maven build.
  • Johnyzhub
    Johnyzhub almost 8 years
    My idea is to keep the maven-compiler-plugin in one pom, so it applies to all the modules and eliminate the duplication. If there are multiple modules , what is the best way of handling this situation?
  • Alan Hay
    Alan Hay almost 8 years
    The only way to do it is as I have suggested as clearly stated in the documentation.As mentioned in the docs, the child project also needs to reference the plugin:
  • Johnyzhub
    Johnyzhub almost 8 years
    Thanks for your prompt answer. I appreciate it alan.
  • SubOptimal
    SubOptimal almost 8 years
    I hardly believe that you can compile the sources in ParentPOMProj/src/main/java with mvn compile. As the packaging is <packaging>**pom**</packaging> Maven won't compile them. Have you done the right click on Project -> Maven -> Update project in Eclipse? In Eclipse Luna you are not even able to change the Java compiler settings for the ParentPOMProj.
  • Johnyzhub
    Johnyzhub almost 8 years
    I am using eclipse MARS v4.5.2. I did 'update project' on parent project and could even perform mvn compile with build success. As I stated in my response, it is not recommended to keep java source files in the parent pom. Parent project is to keep the build configuration that is needed for the child modules. You can't compile sources in ParentPOMProj/src/main/java. I hope this clears you doubt.
  • SubOptimal
    SubOptimal almost 8 years
    You can't compile sources in ParentPOMProj/src/main/java. that's what I said. :-)
  • Johnyzhub
    Johnyzhub almost 8 years
    @SubOptimal.:: Agreed ... :)