4

I know there are some related topics but still I can't understand how to do it.

I'm learning maven and currently in process of creating build profiles. I want maven to auto detect the currently installed java version on my machine. Let's say I'm working in our office which uses (jdk7) or home (jdk8), I want the <source> and <target> elements in maven-compiler-plugin pom.xml to auto detect the java -version regardless of environment (office / home). I've read about activation but can't perfectly understand the purpose.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>   
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>    
        </plugin>
    </plugins>
  </build>
1
  • Not questioning your intention here, but why would you work against java7? Last public update was in 2015, its time to move on.
    – Scorpio
    Commented Jan 29, 2018 at 9:40

3 Answers 3

5

Hi I don't think you can have your build, environment aware automatically, just using the defaults, you need some help of profiles and their activation capability see here. What you can do is introduce 2 different profiles, in each profile you can define the JDK you want, and it will be activated for your if present, then you can configure either the compiler plugin with different source /target or, just set different values for a a property that is going to indicate the java version. Example:

<profiles>
 <profile>
     <id>java8</id>
     <activation>
       <jdk>1.8</jdk>
     </activation>
   <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>   
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>    
        </plugin>
    </plugins>
  </build>
   </profile>
<profile>
     <id>java7</id>
     <activation>
       <jdk>1.7</jdk>
     </activation>
<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>
   </profile>
</profiles>

Hope that helps :)

5

For Java 9 the following profiles can be used.

    <profiles>
        <profile>
            <id>java8</id>
            <activation>
                <jdk>1.8</jdk>
            </activation>
            (...)
        </profile>
        <profile>
            <id>java9</id>
            <activation>
                <jdk>9</jdk>
            </activation>
            (...)
        </profile>
    </profiles>
1
  • 2
    Or for 8 and post-8: <jdk>1.8</jdk> and <jdk>[9,)</jdk> Commented Feb 13, 2022 at 10:54
0

You will want to declare two mutually exclusive profiles, one for jdk7 builds and one for jdk8 builds. Each profile declaration has two important parts:

The activation element is like a conditional, and is nested within . Is your profile on or off? You will want to set up the jdk based activation. Example for your jdk7 profile:

<activation>
  <jdk>1.7</jdk>
</activation>

Next you'll want to define the property or properties that get set when your profile is active. Example for jdk7:

<properties>
    <jdk.version>1.7<jdk.version>
</properties>

Both of those sections get combined with an id element (e.g. <id>jdk7</id>) and nested within a profile element.

If you have any problems getting the jdk detection activation to work, I'd suggest experimenting with an explicit trigger, such as invoking maven with mvn -P jdk7.

From the sounds of it, you want a jdk7 and a jdk8 profile to be defined within your pom.xml. But another option is to have a single profile which is always on, but define the jdk.version property for the profile differently inside of your ~/.m2/settings.xml file. The settings.xml file at the office would have the jdk7 properties; at home it would have jdk8.

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

2
  • I've tried to changed the activation jdk value to 1.4 just to test and why i still get the value of jdk.version which is 1.7 ? Commented Jan 5, 2016 at 9:09
  • If you set the activation value to 1.4, then the property won't get set unless your jdk version is 1.4. And if it does get set, it will just be set to the property value defined within the <properties> element. What was the actual jdk version used to run the test? What was the value of your <properties> element?
    – Ken Koster
    Commented Jan 28, 2016 at 6:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.