Question

I am trying to build a Hudson plugin I've modified and it requires jdk1.6. This is fine, but I don't see how I can tell maven where the different jdk is. I've found few mentions on the internet but they don't seem to apply to me. Some suggest adding some config to .m2/settings.xml but I don't have a settings.xml. Plus, I don't want to use 1.6 for all maven builds.

One kink is I am using mvn in cygwin, if that matters at all. It appears I should be able to make the specification in the project pom file, but the existing pom is pretty bare.

So bottom line is, is there a way to specify a jdk for a single invocation of maven?

Was it helpful?

Solution

So bottom line is, is there a way to specify a jdk for a single invocation of maven?

Temporarily change your JAVA_HOME.

OTHER TIPS

Seems that maven now gives a solution here : Compiling Sources Using A Different JDK

Let's say your JAVA_HOME points to JDK7 (which will run maven processes)

Your pom.xml could be :

<build>
    <plugins>
        <!-- we want JDK 1.6 source and binary compatiblility -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <!-- ... -->
        <!-- we want sources to be processed by a specific 1.6 javac -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <verbose>true</verbose>
              <fork>true</fork>
              <executable>${JAVA_1_6_HOME}/bin/javac</executable>
              <compilerVersion>1.3</compilerVersion>
            </configuration>
        </plugin>
    </plugins>
</build>

If your developpers just add (and customize) the following lines in their settings.xml, your pom will be platform independant :

<settings>
  [...]
  <profiles>
    [...]
    <profile>
      <id>compiler</id>
        <properties>
          <JAVA_1_4_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_4_HOME>
          <JAVA_1_6_HOME>C:\Program Files\Java\j2sdk1.6.0_18</JAVA_1_6_HOME>
        </properties>
    </profile>
  </profiles>
  [...]
  <activeProfiles>
    <activeProfile>compiler</activeProfile>
  </activeProfiles>
</settings>

compile:compile has a user property that allows you to specify a path to the javac.

Note that this user property only works when fork is true which is false by default.

$ mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable=/path/to/the/javac compile

You might have to double quote the value if it contains spaces.

> mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable="C:\...\javac" compile

See also Maven custom properties precedence.

As u said "Plus, I don't want to use 1.6 for all maven builds."....So better I will say modify your pom file and specify which jdk version to use.

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

It will ensure that your particular project uses that version of jdk.

I say you setup JAVA_HOME like Pascal is saying: In cygwin if you use bash as your shell should be "export JAVA_HOME=/cygdrive/c/pathtothejdk" And it never harms to also export the java bin dir to the PATH with "export PATH=${JAVA_HOME}/bin:${PATH}"

and also add maven-enforce-plugin to make sure the right jdk is used. This is a good practice for your pom.

<build>
 <plugins>
   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <executions>
        <execution>
          <id>enforce-versions</id>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requireJavaVersion>
                <version>1.6</version>
              </requireJavaVersion>
            </rules>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

http://maven.apache.org/plugins/maven-enforcer-plugin/usage.html

I know its an old thread. But I was having some issues with something similar to this in Maven for Java 8 compiler source. I figured this out with a quick fix mentioned in this article thought I can put it here and maybe can help others:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Maven uses variable $JAVACMD as the final java command, set it to where the java executable is will switch maven to different JDK.

Hudson also allows you to define several Java runtimes, and let you invoke Maven with one of these. Have a closer look on the configuration page.

For Java 9 :

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>9</source>
                <target>9</target>
            </configuration>
        </plugin>
    </plugins>
</build>

I had build problem with maven within Eclipse on Windows 7.

Though I observed mvn build was running just fine from command line.

mvn -T 5 -B -e -X -U -P test clean install -Dmaven.surefire.debug  --settings ..\..\infra-scripts\maven-conf\settings.xml   > output.log

Eclipse was considering as default JVM a JRE installation instead of JDK so it was failing on compilation.

I added to eclipse.ini following line:

-vm
C:\Program Files (x86)\Java\jdk1.8.0_25\bin

Also when starting from eclipse I used in "Goals" section following list:

-T 5 -B -e -X -U -P test clean install -Dmaven.surefire.debug  --settings ..\..\infra-scripts\maven-conf\settings.xml

Compilation error got solved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top