Manish Rajput
Manish Rajput

Reputation: 19

changing java source compiler in maven

I am using spring-boot-maven-plugin to maven compile my source code as I want maven dependency jars to be part of my jar. (suggested by someone). I have written code according to java 1.7 but maven compiler is trying to compile it with 1.5 (default for maven). To change it I defined source as 1.7 but it is still compiling it with 1.5 Here is my pom.xml example.

<build>
    <sourceDirectory>src</sourceDirectory>
    <pluginManagement>
    <plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.5.2.RELEASE</version>
        <configuration>
            <finalName>BAU_Report</finalName>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
                <configuration>
                  <transformers>
                     <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                     <mainClass>com.bau.report.MainClass</mainClass>
                 </transformer>
                 </transformers>
             </configuration>
         </execution>
        </executions>

      </plugin>
      </plugins>
    </pluginManagement>
  </build>

Upvotes: 1

Views: 760

Answers (3)

Saveendra Ekanayake
Saveendra Ekanayake

Reputation: 3313

Add org.apache.maven.plugins for the pom.xml file.

<build>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
    </plugin>
</build>

Maven properties for the compiler version can be set as following.

Method 1

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Method 2

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Upvotes: 0

mekazu
mekazu

Reputation: 2705

If you’re using the spring boot parent pom you can use this property shortcut instead in your pom.xml

<properties>
    <java.version>7</java.version>
</properties>

Upvotes: 0

lakshman
lakshman

Reputation: 2741

You should use maven compiler plugin

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
      <source>1.7</source>
      <target>1.7</target>
    </configuration>
  </plugin>

Add this to build -> plugins section.

Upvotes: 1

Related Questions