Do you need a java version of Maven dependency?

I developed and built my java application using Maven I need to support Java 1.6, so I use the following properties:

<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.6</maven.compiler.source>

However, when I run the application, I get an "unsupported major.minor version" error. I suspect that one of my dependent jars is a java version that is newer than the version I need to support

My question:

>Is this even possible? I think Maven will take care of this version dependent problem. > Is there a simple way to find minor / major versions of all my dependencies? (it would be great if the tree could be displayed when executing MVN dependencies)

Solution

The problem is that each dependency (maintainer) can decide which java version to compile with (1.5, 1.6, 1.7, 1.8, etc.), so this cannot be solved through Maven But you can make sure you don't use dependencies different from your favorite java version

This can be done by using Maven enforcer plugin and extra enforcer rules:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4.1</version> <!-- find the latest version at http://maven.apache.org/plugins/maven-enforcer-plugin/ -->
        <executions>
          <execution>
            <id>enforce-bytecode-version</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <enforceBytecodeVersion>
                  <maxJdkVersion>1.6</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-5</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

If you compile dependencies using different versions of JDK, this will break your build

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>