Skip to content

Instantly share code, notes, and snippets.

@LIttleAncientForestKami
Created June 7, 2018 14:17
Show Gist options
  • Save LIttleAncientForestKami/46351176bc76538324f1809db0f2f79d to your computer and use it in GitHub Desktop.
Save LIttleAncientForestKami/46351176bc76538324f1809db0f2f79d to your computer and use it in GitHub Desktop.
Maven and Java solutions

Maven and Java:

Diagnosis first:

echo "Path (since javac should be on it): $PATH"
echo "JAVA_HOME (since it overrides path): $JAVA_HOME"
  # consider also 
which java
  # and follow the links to find which Java you are using
java -version
mvn -v

Solution 0: install / correct installation

If javac or java aren't on path, consider checking if they are - at all - installed. Perhaps adding symlinks to /usr/bin will be enough.

If path doesn't have binaries from Java bin directory, javac cannot execute (command not found).

Solution 1: set $JAVA_HOME

Most Java apps expect $JAVA_HOME and will check for it. Enabling it allows them to pick which Java they should use. Also, this overrides path, so if you have more than one Java installed...

  1. export JAVA_HOME=/usr/lib/jvm/... if this works in terminal...
  2. set in in ~/.mavenrc
  3. set it in your shell profile: ~/.bash_profile or ~/.zprofile for ZSH
  4. set it in /etc/environment or other system-wide place

Drawback: other apps (except for 2.) will be affected as well.

To make things clear, this is extracted from mvn script:

  if [ -f /etc/mavenrc ] ; then
    . /etc/mavenrc
  fi

  if [ -f "$HOME/.mavenrc" ] ; then
    . "$HOME/.mavenrc"
  fi

So, now you know the order and what overrides what.

Solution 2: compiler

Set compiler properly, so it uses Java you want it to use. This can be used to compile for lower Java than you use!

<properties>
    <maven.compiler.target>1.9</maven.compiler.target>
    <maven.compiler.source>1.9</maven.compiler.source>
</properties>

check parent POM - if applies.

Solution 2a: set compiler directly:

You can set this in plugin as well.

  <build>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
          <configuration>
            <source>1.6</source>
            <target>1.6</target>

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

See Examples here: https://maven.apache.org/plugins/maven-compiler-plugin/

Solution 3: switch links

Use jenv or update-java-alternatives or for MacOS CurrentJDK

Solution? - Enforce it

https://maven.apache.org/enforcer/enforcer-rules/requireJavaVersion.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment