pom configuration to force usage of jvm 7 with scala maven plugin

3.6k views Asked by At

Because of an incompatibility between a scala 2.9.2 project and the java 8 version, i need to manually specify jvm usage in my maven project.

The pom.xml i make, using documentation here :

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.netlogo.extension</groupId>
    <packaging>jar</packaging>
    <artifactId>rungekuta</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${lib.org.scala-lang.scala.version}</version>
        </dependency>
        <dependency>
           <groupId>org.nlogo</groupId>
           <artifactId>netlogo</artifactId>
            <version>5.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <properties>
        <lib.org.scala-lang.scala.version>2.9.3</lib.org.scala-lang.scala.version>
        <maven.scala.version>${lib.org.scala-lang.scala.version}</maven.scala.version>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>3.2.1</version>
                </plugin>
                <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>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Extension-Name>rungekuta</Extension-Name>
                            <Class-Manager>org.netlogo.extension.rungeKuta.RungeKutaExtension</Class-Manager>
                            <NetLogo-Extension-API-Version>5.0</NetLogo-Extension-API-Version>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <verbose>true</verbose>
                    <fork>true</fork>
                    <executable>/home/reyman/Logiciels/jdk1.7.0_80/bin/javac</executable>
                    <compilerVersion>1.3</compilerVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <name>${project.artifactId} ${project.version}</name>


    <repositories>
        <repository>
            <id>snapshots.scala-tools.org</id>
            <name>Scala snapshots repository</name>
            <url>http://scala-tools.org/repo-snapshots/</url>
        </repository>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala repository</name>
            <url>http://scala-tools.org/repo-releases/</url>
        </repository>
    </repositories>

</project>

I try this without success, maven continue to use my current jvm 8 and not the jvm given in maven-compiler-plugin : <executable>/home/reyman/Logiciels/jdk1.7.0_80/bin/javac</executable>

How can i force usage of the jvm 7 during mvn compile of my mixed scala/java sources project ?

4

There are 4 answers

1
AudioBubble On

This worked for me:

<plugin>
    <inherited>true</inherited>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>
2
Irakli On

Use Something like this:

<plugin> 
 <groupId>net.alchim31.maven</groupId>
 <artifactId>scala-maven-plugin</artifactId> 
 <version>3.2.1</version>
 <configuration>
    <source>${java.version}</source>
    <target>${java.version}</target> 
 </configuration>
</plugin>

modify it per your need.

Here is my complete Build , I don't know if it will help you. I'm pasting it here because it's too long for comment:

    <build>
    <finalName>KAIBICHI</finalName>
    <pluginManagement>
        <plugins>
          <plugin>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <additionalProjectnatures>
                    <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                </additionalProjectnatures>
                <additionalBuildcommands>
                    <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                </additionalBuildcommands>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>org.test.int1.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>
1
jabisu On

I had the same problem trying to use minify-maven-plugin 1.7 with java6. It needs Java7. The problem is that Maven needs to run with the proper JVM to use the plugins, so the only solution I found was to export JAVA_HOME before running maven:

ORIGINAL_JAVA_HOME=$JAVA_HOME
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_79
mvn jetty:run
export JAVA_HOME=$ORIGINAL_JAVA_HOME

if you need to do this frequently maybe you can add to your bashrc an alias like

alias mvn7='ORIGINAL_JAVA_HOME=$JAVA_HOME; export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_79; mvn jetty:run; export JAVA_HOME=$ORIGINAL_JAVA_HOME'
0
Rai On

This worked for me, but I had to allow Eclipse (Mars) to adjust plugin configuration. I tested my pom.xml with an empty project and it seems to build without incident.

I am using jenv to manage my Java environment outside of Eclipse which is much easier when you have many Java versions installed.

Here is the pom.xml I am using:

  <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>org.netlogo.extension</groupId>
        <packaging>jar</packaging>
        <artifactId>rungekuta</artifactId>
        <version>1.0-SNAPSHOT</version>

        <dependencies>
            <dependency>
                <groupId>org.scala-lang</groupId>
                <artifactId>scala-library</artifactId>
                <version>${lib.org.scala-lang.scala.version}</version>
            </dependency>
            <!--  
            <dependency>
               <groupId>org.nlogo</groupId>
               <artifactId>netlogo</artifactId>
                <version>5.2</version>
                <scope>provided</scope>
            </dependency>
            -->
        </dependencies>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <lib.org.scala-lang.scala.version>2.9.3</lib.org.scala-lang.scala.version>
            <maven.scala.version>${lib.org.scala-lang.scala.version}</maven.scala.version>
        </properties>

        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>net.alchim31.maven</groupId>
                        <artifactId>scala-maven-plugin</artifactId>
                        <version>3.2.1</version>
                    </plugin>
                    <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>
                    <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
                    <plugin>
                        <groupId>org.eclipse.m2e</groupId>
                        <artifactId>lifecycle-mapping</artifactId>
                        <version>1.0.0</version>
                        <configuration>
                            <lifecycleMappingMetadata>
                                <pluginExecutions>
                                    <pluginExecution>
                                        <pluginExecutionFilter>
                                            <groupId>
                                                net.alchim31.maven
                                            </groupId>
                                            <artifactId>
                                                scala-maven-plugin
                                            </artifactId>
                                            <versionRange>
                                                [3.2.1,)
                                            </versionRange>
                                            <goals>
                                                <goal>testCompile</goal>
                                            </goals>
                                        </pluginExecutionFilter>
                                        <action>
                                            <ignore></ignore>
                                        </action>
                                    </pluginExecution>
                                </pluginExecutions>
                            </lifecycleMappingMetadata>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <id>scala-compile-first</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>add-source</goal>
                                <goal>compile</goal>
                                <goal>testCompile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <archive>
                            <manifestEntries>
                                <Extension-Name>rungekuta</Extension-Name>
                                <Class-Manager>org.netlogo.extension.rungeKuta.RungeKutaExtension</Class-Manager>
                                <NetLogo-Extension-API-Version>5.0</NetLogo-Extension-API-Version>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>


        <name>${project.artifactId} ${project.version}</name>


        <repositories>
            <repository>
                <id>snapshots.scala-tools.org</id>
                <name>Scala snapshots repository</name>
                <url>http://scala-tools.org/repo-snapshots/</url>
            </repository>
            <repository>
                <id>scala-tools.org</id>
                <name>Scala repository</name>
                <url>http://scala-tools.org/repo-releases/</url>
            </repository>
        </repositories>

    </project>