1

I have a project that is run by customers using a Java 7 runtime.

I added a dependency in an artifact that compiles against Java 8 - uses Java 8 features, like lambdas and streams.

When compiling my project, I don't get any error that something is wrong.

In runtime though, I get a bunch of error, not recognizing stuff, obviously.

Is there a way to protect my project from using artifact depending on higher level Java? Thanks.

* This is a general question for an artifact depending on some low level Java API that tries to add a dependency on another artifact that depends on higher level Java.

* I guess this is because Java is not part of the explicit dependency definition but rather implicit. The question is how to make it more explicit and protected.

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
  • 1
    I don't think that there is a general solution. What I can think of is to fix your dependency definition in your POM to the last known working version. Did you configure your POM to use a real Java7 compiler installed on your PC to compile your project? – Timothy Truckle Dec 21 '16 at 11:29

1 Answers1

4

You can use the maven-enforcer-plugin in combination with the extra-enforcer-rule which looks like this:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4.1</version>
        <executions>
          <execution>
            <id>enforce-bytecode-version</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <enforceBytecodeVersion>
                  <maxJdkVersion>1.7</maxJdkVersion>
                  <excludes>
                    <exclude>org.mindrot:jbcrypt</exclude>
                  </excludes>
                </enforceBytecodeVersion>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>extra-enforcer-rules</artifactId>
            <version>1.0-beta-6</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

You can make it more safe to use another enforcer-rule which you can use to force to use the correct JDK

khmarbaise
  • 92,914
  • 28
  • 189
  • 235