Por qué Maven usa JDK 1.6 pero mi versión de Java es 1.7


Soy nuevo en maven, y también en macOS.

Tengo configurado maven en mi terminal, y al obtener la configuración de la versión (usando mvn -v) parece que usa JDK 1.6, mientras que tengo instalado JDK 1.7. ¿Pasa algo malo?

Los comandos que entro son estos:

blues:helloworld Ninja$ java -version
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b06)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)`
blues:helloworld Ninja$ mvn -v
Apache Maven 3.1.0 (893ca28a1da9d5f51ac03827af98bb730128f9f2; 2013-06-28 10:15:32+0800)
Maven home: /usr/local/Cellar/maven/3.1.0/libexec
Java version: 1.6.0_51, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: zh_CN, platform encoding: EUC_CN
OS name: "mac os x", version: "10.8.4", arch: "x86_64", family: "mac"
Author: Lii, 2013-09-15

9 answers

Añade lo siguiente a tu ~/.mavenrc:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/{jdk-version}/Contents/Home

Segunda solución:

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile

 219
Author: Gaurav Vaish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-28 13:50:09

Entrar en

/System/Library/Frameworks/JavaVM.framework/Versions

Y actualizar el CurrentJDK enlace simbólico para apuntar a

/Library/Java/JavaVirtualMachines/YOUR_JDK_VERSION/Contents/

Por ejemplo

cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo rm CurrentJDK
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/ CurrentJDK

Ahora funcionará inmediatamente.

 45
Author: Oskar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-17 12:35:55

También puedes hacer,

<properties>
      ...  

      <!-- maven-compiler-plugin , aka JAVA Compiler 1.7 -->
      <maven.compiler.target>1.7</maven.compiler.target>
      <maven.compiler.source>1.7</maven.compiler.source>

      ...  
</properties>
 38
Author: DEREK LEE,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-16 17:37:16

También puede indicarle explícitamente a maven para qué versión de java compilar. Puedes intentar agregar el maven-compiler-plugin a tu pom.

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Si importó un proyecto maven en un IDE, entonces probablemente haya una configuración de maven en su IDE para el compilador predeterminado que su maven runner está usando.

 26
Author: redFIVE,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-15 15:24:49

Me ayudó. Sólo tienes que añadir en su pom.XML.

Por defecto maven compiler plugin utiliza Java 1.5 o 1.6, usted tiene que redefinir en su pom.XML.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
 8
Author: Sergey Luchko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-24 15:31:02

Para Usuarios de Eclipse. Si tiene una configuración de ejecución que hace paquete limpio, por ejemplo.

En el panel de configuración Run hay una pestaña JRE donde puede especificar en qué tiempo de ejecución debe ejecutarse. Tenga en cuenta que esta configuración anula lo que está en el pom.XML.

 3
Author: Yonatan Abrego,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-11 19:18:35

Llego tarde a esta pregunta, pero creo que la mejor manera de manejar las versiones JDK en macOS es usando el script descrito en: http://www.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick /

 2
Author: Yuri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-04 09:11:11

Compruebe la compatibilidad. Luché con mvn 3.2.1 y jdk 1.6.0_37 durante muchas horas. Todas las variables fueron establecidas pero no estaba funcionando. Finalmente actualizé jdk to 1.8.0_60 y mvn 3.3.3 y eso funcionó. Variables de entorno como sigue:

JAVA_HOME=C:\ProgramFiles\Java\jdk1.8.0_60 
MVN_HOME=C:\ProgramFiles\apache-maven\apache-maven-3.3.3 
M2=%MVN_HOME%\bin extend system level Path- ;%M2%;%JAVA_HOME%\bin;
 2
Author: Vikas Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-27 00:35:59

La solución de@MasterGaurav está funcionando perfectamente,

Normalmente pongo el interruptor de java en una función zsh:

alias java_ls='/usr/libexec/java_home -V 2>&1 | grep -E "\d.\d.\d[,_]" | cut -d , -f 1 | colrm 1 4 | grep -v Home'

function java_use() {
    export JAVA_HOME=$(/usr/libexec/java_home -v $1)
    echo export "JAVA_HOME=$(/usr/libexec/java_home -v $1)" > ~/.mavenrc
    export PATH=$JAVA_HOME/bin:$PATH
    java -version
}

Luego puede ejecutar java_ls dos obtener toda la jvm disponible en su máquina, ejecute java_use 1.7 para usar la versión 1.7 tanto para java como para maven.

 1
Author: Wei Qiu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-18 16:44:46